AOP中关键性概念
连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑
通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
注2:完成切面编程
代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
例子:外科医生+护士
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
如何实现AOP
目标对象只负责业务逻辑代码
通知对象负责AOP代码,这二个对象都没有AOP的功能,只有代理对象才有
1. AOP
即面向切面编程
2. AOP带来的好处
让我们可以 “专心做事”
案例:
public void doSameBusiness (long lParam,String sParam){
// 记录日志
log.info("调用 doSameBusiness方法,参数是:"+lParam);
// 输入合法性验证
if (lParam<=0){
throws new IllegalArgumentException("xx应该大于0");
}
if (sParam==null || sParam.trim().equals("")){
throws new IllegalArgumentException("xx不能为空");
}
// 异常处理
try{ ...
}catch(...){
}catch(...){
}
// 事务控制
tx.commit();
}
3. 前置通知(org.springframework.aop.MethodBeforeAdvice):在连接点之前执行的通知()
案例:在购书系统当中使用AOP方式实现日志系统
4. 后置通知(org.springframework.aop.AfterReturningAdvice):在连接点正常完成后执行的通知
案例:在线购书系统中,要求不修改BookBizImpl代码的情况下增加如下功能:对买书的用户进行返利:每买本书返利3元。(后置通知)
即:每调用一次buy方法打印:“[销售返利][时间]返利3元。”
5. 环绕通知(org.aopalliance.intercept.MethodInterceptor):包围一个连接点的通知,最大特点是可以修改返回值,由于它在方法前后都加入了自己的逻辑代码,因此功能异常强大。
它通过MethodInvocation.proceed()来调用目标方法(甚至可以不调用,这样目标方法就不会执行)
案例:修改日志系统不光要输出参数,还要输出返回值(环绕通知)
# 这个接口里面没有定义方法,我们要求我们的类必须实现afterThrows这个方法
# public void afterThrowing( [Method method,] [Object args,] [Object target,] Throwable throwable );
# 前面三个参数都是可选的,只有第三个参数是必须的,同时我们还可以在同一个类中定义这个方法的多个版本,如:
# public void afterThrowing( MyException1 ex ) {}
# public void afterThrowing( MyException2 ex ) {}
# 具体那个方法被调用则根据具体的Exception来判断,由AOP容器自动识别 执行
6. 异常通知(org.springframework.aop.ThrowsAdvice):这个通知会在方法抛出异常退出时执行
案例: 书本价格为负数时抛出一个异常,通过异常通知取消此订单
7. 适配器(org.springframework.aop.support.RegexpMethodPointcutAdvisor) 适配器=通知(Advice)+切入点(Pointcut)
案例:通过适配器解决发书评时也返利的问题
.*buy
通知
前置通知
实现org.springframework.aop.MethodBeforeAdvice接口
买书、评论前加系统日志后置通知
实现org.springframework.aop.AfterReturningAdvice接口
买书返利(存在bug)环绕通知
org.aopalliance.intercept.MethodInterceptor
类似过滤器,会包括切入点,目标类前后都会执行代码。
异常通知
org.springframework.aop.ThrowsAdvice
出现异常执行系统提示,然后进行处理。价格异常为例
过滤通知(适配器)
org.springframework.aop.support.RegexpMethodPointcutAdvisor
处理买书返利的bug
xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
default-autowire="byName"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 目标对象 -->
<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz"></bean><!-- 前置通知 -->
<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="com.zking.aop.advice.MyThrowsAdvice" id="myThrowsAdvice"></bean><!-- 过滤通知 -->
<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myAfterReturningAdvice2">
<property name="advice" ref="myAfterReturningAdvice"></property>
<!-- .buy:[.*]任意字符0~n个,以buy结尾的方法 -->
<!-- <property name="pattern" value=".*buy"></property> -->
<property name="patterns">
<list>
<value>.*buy</value>
</list>
</property>
</bean><!-- 生成代理(目标对象——通知) -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="proxyFactoryBean">
<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>myAfterReturningAdvice2</value>
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value>
</list>
</property>
</bean></beans>