配置通知的套路主要分成三步:
- 配置被代理对象(目标对象)
- 配置通知
- 配置代理对象,在该配置中完成以下三步:
a.配置要代理的接口(proxyInterfaces)
b.配置通知(interceptorNames)
c.配置目标对象(被代理对象)
1. 对应的类
要实现的接口Test1SeviceInter
package com.qjg.aop;
public interface Test1ServiceInter {
void sayHello();
}
要实现的接口Test2ServiceInter
package com.qjg.aop;
public interface Test2ServiceInter {
void sayBye();
}
实现类:
package com.qjg.aop;
public class TestService implements Test1ServiceInter,Test2ServiceInter {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void sayHello() {
System.out.println(" sayHello ------"+name);
}
@Override
public void sayBye() {
System.out.println(" sayBye ---------"+name);
// int i = 8/0;
}
}
配置前置通知
- 配置代理对象
<bean id="testService" class="com.qjg.aop.TestService">
<property name="name" value="qjg"/>
</bean>
- 配置通知
package com.qjg.aop;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("-------recode log----------");
}
}
<!--配置前置通知-->
<bean id="myMethodBeforeAdvice" class="com.qjg.aop.MyMethodBeforeAdvice"></bean>
- 配置代理对象
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置要代理的接口-->
<property name="proxyInterfaces">
<list>
<value>com.qjg.aop.Test1ServiceInter</value>
<value>com.qjg.aop.Test2ServiceInter</value>
</list>
</property>
<!--配置通知-->
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
</list>
</property>
<!--配置需要代理的目标对象-->
<property name="target" ref="testService"/>
</bean>
配置后置通知
- 配置代理对象
<bean id="testService" class="com.qjg.aop.TestService">
<property name="name" value="qjg"/>
</bean>
- 配置后置通知
对应的java后置通知实现类,该类实现了AfterReturningAdvice
package com.qjg.aop;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class MyMethodAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println(" ************after advice*********");
}
}
<!--配置后置通知-->
<bean id="myMethodAfterAdvice" class="com.qjg.aop.MyMethodAfterAdvice"></bean>
- 配置代理对象
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置要代理的接口-->
<property name="proxyInterfaces">
<list>
<value>com.qjg.aop.Test1ServiceInter</value>
<value>com.qjg.aop.Test2ServiceInter</value>
</list>
</property>
<!--配置通知-->
<property name="interceptorNames">
<list>
<value>myMethodAfterAdvice</value>
</list>
</property>
<!--配置需要代理的目标对象-->
<property name="target" ref="testService"/>
</bean>
配置环绕通知
- 对应的java环绕通知实现类,该类实现了 MethodInterceptor
package com.qjg.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("--++++++++++before++++-MethodInterceptor-+++++++++++++-");
Object object = methodInvocation.proceed();//必须调用proceed方法,否则被包裹的方法不能执行
System.out.println("-+++++++++after+++++-MethodInterceptor-+++++++++++++-");
return object;
}
}
- 配置环绕通知
<!--配置环绕通知-->
<bean id="myMethodInterceptor" class="com.qjg.aop.MyMethodInterceptor"/>
- 配置代理对象
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置要代理的接口-->
<property name="proxyInterfaces">
<list>
<value>com.qjg.aop.Test1ServiceInter</value>
<value>com.qjg.aop.Test2ServiceInter</value>
</list>
</property>
<!--配置通知,属性是拦截器名-->
<property name="interceptorNames">
<list>
<value>myMethodInterceptor</value>
</list>
</property>
<!--配置需要代理的目标对象-->
<property name="target" ref="testService"/>
</bean>
配置异常通知
(需要在TestService类的sayBye方法内,实现一个异常,如:添加int i = 9/0;)
- 对应的java异常通知实现类,该类实现了 ThrowsAdvice
package com.qjg.aop;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.asm.commons.Method;
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Method m, Object[] os, Object target, Exception e) {
System.out.println("出异常了..."+e.getMessage());
}
}
- 配置异常通知
<!--配置异常通知-->
<bean id="myThrowsAdvice" class="com.qjg.aop.MyThrowsAdvice"/>
- 配置代理对象
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置要代理的接口-->
<property name="proxyInterfaces">
<list>
<value>com.qjg.aop.Test1ServiceInter</value>
<value>com.qjg.aop.Test2ServiceInter</value>
</list>
</property>
<!--配置通知,属性是拦截器名-->
<property name="interceptorNames">
<list>
<value>myThrowsAdvice</value>
</list>
</property>
<!--配置需要代理的目标对象-->
<property name="target" ref="testService"/>
</bean>
配置引入通知
引入通知没有实现类
配置引入通知
<!--引入通知-->
<bean id="myMethodBeforeAdviceFilter" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<--配置要引入的通知-->
<property name="advice" ref="myMethodBeforeAdvice"/>
<property name="mappedNames">
<--要映射的接口名-->
<list>
<value>sayBye</value>
</list>
</property>
</bean>
- 配置代理对象
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置要代理的接口-->
<property name="proxyInterfaces">
<list>
<value>com.qjg.aop.Test1ServiceInter</value>
<value>com.qjg.aop.Test2ServiceInter</value>
</list>
</property>
<!--配置通知,属性是拦截器名-->
<property name="interceptorNames">
<list>
<--可以将其理解成过滤器-->
<value>myMethodBeforeAdviceFilter</value>
</list>
</property>
<!--配置需要代理的目标对象-->
<property name="target" ref="testService"/>
</bean>
总结:对于框架的使用是有套路可寻的