今天又看了下韩顺平的springaop的讲解,讲解的很透彻。仿照视频自己使用下前置通知。
有个接口testserviceinter,有两个实现方法testservice和test2service。他们都有sayhello();我们的需求是在调用这两个方法之前,要先完成写日志的功能;
我在各个实现类的sayhello()方法里面写上写日志的功能就是了。
package com.jdc.aop; /** * @author dellcomputer * @title: testservice * @projectname springaop * @description: * @date 2018/12/251:38 pm */ public interface testservice { /** * @description: * @param ${tags} * @return ${return_type} * @throws * @author jdc * @date 2018/12/25 1:39 pm */ void sayhi(string name); }
public class testserviceimpl implements testservice { private string name; /** * @description: say hi * @param ${tags} * @return ${return_type} * @throws * @author jdc * @date 2018/12/25 1:40 pm */ @override public void sayhi(string name ) { system.out.println("hi:" + name); } public string getname() { return name; } public void setname(string name) { this.name = name; } }
/** * @author dellcomputer * @title: mybeforeadvice * @projectname springaop * @description: 写日志前置通知 * @date 2018/12/251:42 pm */ public class mybeforeadvice implements methodbeforeadvice { /** * @description: 写日志的功能 * @param ${tags} * @return ${return_type} * @throws * @author jdc * @date 2018/12/25 1:44 pm */ @override public void before(method method, object[] objects, object o) throws throwable { system.out.println("我是写日志的功能。"); } }
前置通知需要实现methodbeforeadvice接口,前置通知是在目标方法调用之前调用;
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置前置通知 --> <bean id="mybeforeadvice" class="com.jdc.aop.advice.mybeforeadvice"/> <!-- 配置被代理对象 --> <bean id="logtestserviceimpl" class="com.jdc.aop.testserviceimpl"> <property name="name" value="test"></property> </bean> <!-- 配置代理对象 --> <bean id="proxyfactorybean" class="org.springframework.aop.framework.proxyfactorybean"> <!-- 代理接口集 配置哪些接口要被代理 --> <property name="proxyinterfaces"> <list> <value>com.jdc.aop.testservice</value> </list> </property> <!-- 把通知织入代理对象 --> <property name="interceptornames"> <value>mybeforeadvice</value> </property> <!-- 配置被代理的对象 --> <property name="target" ref="logtestserviceimpl"/> </bean> </beans>
proxyfactorybean是一个代理对象,如果我们被代理的对象实现了接口,spring使用的是jdk动态代理技术实现的动态代理;所以我们要告诉代理对象,我们的哪些接口需要被代理,然后哪个对象需要被代理,我的增强实现应该怎么被织入到代理对象(前置,后置,环绕....)
public class testmain { public static void main(string[] args) { applicationcontext ac = new classpathxmlapplicationcontext("beans.xml"); //获取代理对象 testservice log = (testservice) ac.getbean("proxyfactorybean"); log.sayhi("哈哈"); } }
注意这里,我们需要获取代理对象,而不是目标对象,不然不能调用前置通知的代码。
如对本文有疑问, 点击进行留言回复!!
浅谈Java如何实现一个基于LRU时间复杂度为O(1)的缓存
JDK1.6“新“特性Instrumentation之JavaAgent(推荐)
before社区电量是什么意思 Before社区电量获得方法
网友评论