一、aop
即面向切面编程
二、aop的五种通知
1、前置通知
实现org.springframework.aop.MethodBeforeAdvice接口
案例:买书、评论前加系统日志
/**
- 前置通知
- @author 钟sir
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "【系统日志】:"+targetName+"."+methodName+",携带的参数为:"+params;
System.out.println(msg);
}
}
在目标方法前调用
2、后置通知
实现org.springframework.aop.AfterReturningAdvice接口
案例:买书返利(存在bug)
/**
- 买书返利
- @author 钟sir
*/
public class MyAfterReturningAdvice implements AfterReturningAdvice{
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "【买书返利日志】:"+targetName+"."+methodName+",携带的参数为:"+params+",目标对象的返回值:"+returnValue;
System.out.println(msg);
}
}
在目标方法后调用。
3、环绕通知
实现org.aopalliance.intercept.MethodInterceptor接口
类似过滤器,会包括切入点,目标类前后都会执行代码。
public class MyMethodInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
Object target = invocation.getThis();
Method method = invocation.getMethod();
Object[] args = invocation.getArguments();
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "【环绕通知日志】:"+targetName+"."+methodName+",携带的参数为:"+params;
System.out.println(msg);
Object returnValue = invocation.proceed();
String msg2 = "【环绕通知日志】:"+returnValue;
System.out.println(msg2);
return returnValue;
}
}
将 Before 和 After合到一起
4、异常通知
org.springframework.aop.ThrowsAdvice
public void afterThrowing( MyException1 ex ) {}
出现异常执行系统提示,然后进行处理。价格异常为例
5、过滤通知
org.springframework.aop.support.RegexpMethodPointcutAdvisor
案例:处理买书返利的bug
只需要配置一下就行
如下就是我的所有配置信息:
**************************** aop***************
<!-- 前置通知 -->
<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myMethodBeforeAdvice"></bean>
<!-- 后置通知 -->
<bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfterReturningAdvice"></bean>
<!-- 环绕通知 -->
<bean class="com.zking.aop.advice.MyMethodInterceptor" id="myMethodInterceptor"></bean>
<!-- 过滤通知 -->
<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myAfterReturningAdvice2">
<property name="advice" ref="myAfterReturningAdvice"></property>
<!-- [.*]buy代表以buy结尾的方法都会切入后置通知, .*代表任意字符0-n -->
<!-- <property name="pattern" value=".*buy"></property> -->
<property name="patterns">
<list>
<value>.*buy</value>
</list>
</property>
</bean>
<!-- 生成代理 -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="myproxyFactory">
<property name="target" ref="bookBiz"></property>
<!-- 生产出的代理必定要实现目标对象所实现的接口-->
<property name="proxyInterfaces">
<list>
<value>com.zking.aop.biz.BookBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
<value>myAfterReturningAdvice</value> --后置通知
<value>myMethodInterceptor</value>
<value>myAfterReturningAdvice2</value>--过滤通知 强化后置通知
</list>
</property>
</bean>