转载自:http://blog.csdn.net/mutouyihao/article/details/6655996
定义
- 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
- 连接点(Joinpoint) :程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
- 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
- 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
- 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
- AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config> 的 proxy-target-class 属性设为true
通知(Advice)类型
- 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法
- 后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,TestAspect中的doAfter方法,所以AOPTest中调用BServiceImpl.barB抛出异常时,doAfter方法仍然执行
- 返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
- 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,TestAspect中的doAround方法。
- 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,TestAspect中的doThrowing方法。
配置文件:
- <?xml version="1.0"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <bean id="aopHandler1" class="as.huateng.bo.test.AopHandler1"/>
- <aop:config>
- <aop:pointcut id="poicut"expression="execution(* as.huateng.bo.impl.test.*.*(..))" />
- <aop:aspect ref="aopHandler1">
- <aop:before method="before"pointcut-ref="poicut" />
- <aop:after method="after"pointcut-ref="poicut" />
- <aop:around method="around"pointcut-ref="poicut" />
- <aop:after-throwing method="doThrowing" pointcut-ref="poicut"/>
- <aop:after-returning method="doReturning" pointcut-ref="poicut"/>
- </aop:aspect>
- </aop:config>
- </beans>
切面类:
- public class AopHandler1 {
- public void before(JoinPoint joinPoint) {
- // 获取方法名
- System.out.println("-----方法:" + joinPoint.getSignature().getName());
- // 获取参数信息
- Object[] args = joinPoint.getArgs();
- for (int i = 0; i < args.length; i++) {
- System.out.println("-------参数" + args[i]);
- }
- }
- public Object around(ProceedingJoinPoint p) throws Throwable{
- System.out.println("-----------comeinto around");
- Object retVal = p.proceed();
- System.out.println("-----------leave around");
- return retVal;
- }
- public void after(JoinPoint joinPoint) {
- System.out.println("-----------inafter");
- }
- public void doThrowing(JoinPoint jp) {
- System.out.println("--------in exception");
- }
- public void doReturning(JoinPoint jp) {
- System.out.println("--------in return");
- }
- }
执行顺序
当有异常抛出时,执行顺序为:
Before—around—after—exception
无异常的执行顺序:
Before—around—after—around---return
PS:当切面中的方法定义错误时,尤其around方法,就会产生一些其他方法调用不生效的莫名其妙错误
参考链接:
http://www.iteye.com/topic/336873