Spring AOP执行顺序

来源:互联网 发布:苹果5s数据用不了 编辑:程序博客网 时间:2024/06/05 23:45

不同通知的执行顺序:

@Before前置通知

@Around环绕通知,进入方法...

执行对象方法...

@AfterReturning后置通知

@After最终通知 执行...

@AfterThrowing异常通知,程序出现异常了吗?

退出方法...

相同通知的执行顺序是:

从上向下



1:AfterReturning与AfterThrowing不同时出现

2:AOP只管理Bean,需要注册bean,才能被AOP拦截,因此AOP不拦截静态方法

2:AOPBean的依赖注入,都需要事先定义接口


不同配置方式切面的执行顺序:

XML配置的切面 优先于 自动代理配置的切面

同一种配置方式中的切面的执行顺序:

XML从上向下


下面是一段配置文件和一段输出:



<!--开启aop注解  -->

<aop:aspectj-autoproxy/>

 

<!--切面交由Spring管理 -->

<beanid="copyAop" class="com.newer.test.CopyOfTestAOP" />

<beanid="testAop" class="com.newer.test.TestAOP" />

<!--xml方式配置aop -->

<beanid="testAopXml" class="com.newer.test.TestAOPXml" />

<beanid="copyAopXml" class="com.newer.test.CopyOfTestAOPXml"/>

<aop:config>

<aop:aspectid="asp" ref="testAopXml">

<!--定义切面 -->

<aop:pointcutid="mycut" expression="execution(*com.newer.service.impl.PersonServiceImpl.*(..))" />

<!--定义前置通知,关联切面和拦截方法doAccessCheck() -->

<aop:beforepointcut-ref="mycut" method="doAccessCheck"/>

<!--定义后置通知 -->

<aop:after-returningpointcut-ref="mycut" method="doAccessReturning"/>

<!--异常通知 -->

<aop:after-throwingpointcut-ref="mycut" method="doAfterThrowing"/>

<!--最终通知 -->

<aop:afterpointcut-ref="mycut" method="doAfter"/>

<!--环绕通知 -->

<aop:aroundpointcut-ref="mycut" method="doAround"/>

</aop:aspect>

<aop:aspectid="asp1" ref="copyAopXml">

<!--定义切面 -->

<aop:pointcutid="mycut2" expression="execution(*com.newer.service.impl.PersonServiceImpl.*(..))" />

<!--定义前置通知,关联切面和拦截方法doAccessCheck() -->

<aop:beforepointcut-ref="mycut2" method="doAccessCheck"/>

<!--定义后置通知 -->

<aop:after-returningpointcut-ref="mycut2" method="doAccessReturning"/>

<!--异常通知 -->

<aop:after-throwingpointcut-ref="mycut2" method="doAfterThrowing"/>

<!--最终通知 -->

<aop:afterpointcut-ref="mycut2" method="doAfter"/>

<!--环绕通知 -->

<aop:aroundpointcut-ref="mycut2" method="doAround"/>

</aop:aspect>

</aop:config>






%%%%执行public finalstatic void createExamQst() throws Exception方法...

*****************************************************************

xml-----------Before前置通知,是在方法前执行吗?

xml-----------doAround环绕通知执行,进入方法...

CopyOfTestAOPXml-----------Before前置通知,是在方法前执行吗?

CopyOfTestAOPXml-----------doAround环绕通知执行,进入方法...

>>CopyOfTestAOP>>        @Before("anyMethod()")

>>CopyOfTestAOP>>        环绕通知@Around("anyMethod()"),进入方法...

##TestAOP##        @Before("anyMethod()")

##TestAOP##        环绕通知@Around("anyMethod()"),进入方法...

%%%%执行public voidupdate(String name, Integer id)...

##TestAOP##        @AfterReturning("anyMethod()")

##TestAOP##        @AfterReturning(pointcut="anyMethod()",returning="result"),返回值result:null

##TestAOP##        @After("anyMethod()")...

##TestAOP##        环绕通知@Around("anyMethod()")退出方法...

>>CopyOfTestAOP>>        @AfterReturning("anyMethod()")

>>CopyOfTestAOP>>        @AfterReturning(pointcut="anyMethod()",returning="result"),返回值result:null

>>CopyOfTestAOP>>        @After("anyMethod()")...

>>CopyOfTestAOP>>        环绕通知@Around("anyMethod()")退出方法...

CopyOfTestAOPXml-----------doAccessReturning后置通知,是在方法后执行吗?

CopyOfTestAOPXml-----------doAfter最终通知执行...

CopyOfTestAOPXml-----------doAround环绕通知退出方法...

xml-----------doAccessReturning后置通知,是在方法后执行吗?

xml-----------doAfter最终通知执行...

xml-----------doAround环绕通知退出方法...

*****************************************************************

xml-----------Before前置通知,是在方法前执行吗?

xml-----------doAround环绕通知执行,进入方法...

CopyOfTestAOPXml-----------Before前置通知,是在方法前执行吗?

CopyOfTestAOPXml-----------doAround环绕通知执行,进入方法...

>>CopyOfTestAOP>>        @Before("anyMethod()")

>>CopyOfTestAOP>>        环绕通知@Around("anyMethod()"),进入方法...

##TestAOP##        @Before("anyMethod()")

##TestAOP##        环绕通知@Around("anyMethod()"),进入方法...

%%%%执行public StringgetPersonName(Integer id) ...

##TestAOP##        @AfterReturning("anyMethod()")

##TestAOP##        @AfterReturning(pointcut="anyMethod()",returning="result"),返回值result:AAAAAA

##TestAOP##        @After("anyMethod()")...

##TestAOP##        环绕通知@Around("anyMethod()")退出方法...

>>CopyOfTestAOP>>        @AfterReturning("anyMethod()")

>>CopyOfTestAOP>>        @AfterReturning(pointcut="anyMethod()",returning="result"),返回值result:AAAAAA

>>CopyOfTestAOP>>        @After("anyMethod()")...

>>CopyOfTestAOP>>        环绕通知@Around("anyMethod()")退出方法...

CopyOfTestAOPXml-----------doAccessReturning后置通知,是在方法后执行吗?

CopyOfTestAOPXml-----------doAfter最终通知执行...

CopyOfTestAOPXml-----------doAround环绕通知退出方法...

xml-----------doAccessReturning后置通知,是在方法后执行吗?

xml-----------doAfter最终通知执行...

xml-----------doAround环绕通知退出方法...

*****************************************************************

xml-----------Before前置通知,是在方法前执行吗?

xml-----------doAround环绕通知执行,进入方法...

CopyOfTestAOPXml-----------Before前置通知,是在方法前执行吗?

CopyOfTestAOPXml-----------doAround环绕通知执行,进入方法...

>>CopyOfTestAOP>>        @Before("anyMethod()&& args(name)"),传入参数name:save()你好!

>>CopyOfTestAOP>>        @Before("anyMethod()")

>>CopyOfTestAOP>>        环绕通知@Around("anyMethod()"),进入方法...

##TestAOP##        @Before("anyMethod()&& args(name)"),传入参数name:save()你好!

##TestAOP##        @Before("anyMethod()")

##TestAOP##        环绕通知@Around("anyMethod()"),进入方法...

%%%%执行public void...        name:save()你好!

##TestAOP##        @After("anyMethod()")...

##TestAOP##        异常通知@AfterThrowing("anyMethod()")

##TestAOP##        异常@AfterThrowing(pointcut="anyMethod()",throwing="e"),异常:java.lang.RuntimeException: save()异常...

>>CopyOfTestAOP>>        @After("anyMethod()")...

>>CopyOfTestAOP>>        异常通知@AfterThrowing("anyMethod()")

>>CopyOfTestAOP>>        异常@AfterThrowing(pointcut="anyMethod()",throwing="e"),异常:java.lang.RuntimeException: save()异常...

CopyOfTestAOPXml-----------doAfterThrowing异常通知,程序出现异常了吗?

CopyOfTestAOPXml-----------doAfter最终通知执行...

xml-----------doAfterThrowing异常通知,程序出现异常了吗?

xml-----------doAfter最终通知执行...