美文网首页
同一切面内含有before、around、after-retur

同一切面内含有before、around、after-retur

作者: wbpailxt | 来源:发表于2019-05-06 10:03 被阅读0次

    响应顺序和xml声明通知的顺序有关,但由于响应顺序没有规律(或者说目前我还没找出它的规律),故先将现象列出,并且为编码时规定一个适合理解的约束。
    目标对象:

    public class AspectBiz {
        public void biz() {
            System.out.println("AspectBiz biz.");
            //throw new RuntimeException();
        }
    }
    

    切面:

    public class MoocAspect {
        
        public void before() {
            System.out.println("MoocAspect before.");
        }
        
        public void afterReturning() {
            System.out.println("MoocAspect afterReturning.");
        }
        
        public void afterThrowing(Throwable throwing) {
            System.out.println("MoocAspect afterThrowing.");
            /*throwing.printStackTrace();*/
            System.out.println("--------------------------------");
        }
        
        public void after() {
            System.out.println("MoocAspect after.");
        }
        
        public void around(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("MoocAspect around 1.");
            pjp.proceed();
            System.out.println("MoocAspect around 2.");
        }
    }
    

    测试:

    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestAOPSchemaAdvice extends UnitTestBase {
    
        public TestAOPSchemaAdvice() {
            super("classpath:spring-aop-schema-advice.xml");
        }
    
        @Test
        public void testBiz() {
            AspectBiz biz = super.getBean("aspectBiz");
            biz.biz();
        }
    }
    

    xml通知声明顺序1:

    <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"
        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.0.xsd">
    
        <bean id="moocAspect" class="com.groupaop.artifactaop.schema.advice.MoocAspect"></bean>
        
        <bean id="aspectBiz" class="com.groupaop.artifactaop.schema.advice.biz.AspectBiz"></bean>
        
        <aop:config>
            <aop:aspect id="moocAspectAOP" ref="moocAspect">
                <aop:pointcut expression="execution(* com.groupaop.artifactaop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
                <aop:around method="around" pointcut-ref="moocPiontcut"/>
                <aop:before method="before" pointcut-ref="moocPiontcut"/>
    
                <aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/>
    
                <aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut" throwing="throwing"/>
    
                <aop:after method="after" pointcut-ref="moocPiontcut"/>
            </aop:aspect>
        </aop:config>
     </beans>
    
    图片.png

    xml声明顺序2:

    <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"
        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.0.xsd">
    
        <bean id="moocAspect" class="com.groupaop.artifactaop.schema.advice.MoocAspect"></bean>
        
        <bean id="aspectBiz" class="com.groupaop.artifactaop.schema.advice.biz.AspectBiz"></bean>
        
        <aop:config>
            <aop:aspect id="moocAspectAOP" ref="moocAspect">
                <aop:pointcut expression="execution(* com.groupaop.artifactaop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
    
                <aop:before method="before" pointcut-ref="moocPiontcut"/>
                <aop:around method="around" pointcut-ref="moocPiontcut"/>
                <aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/>
    
                <aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut" throwing="throwing"/>
    
                <aop:after method="after" pointcut-ref="moocPiontcut"/>
            </aop:aspect>
        </aop:config>
     </beans>
    
    图片.png

    xml声明顺序3:

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
        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.0.xsd">
    
        <bean id="moocAspect" class="com.groupaop.artifactaop.schema.advice.MoocAspect"></bean>
        
        <bean id="aspectBiz" class="com.groupaop.artifactaop.schema.advice.biz.AspectBiz"></bean>
        
        <aop:config>
            <aop:aspect id="moocAspectAOP" ref="moocAspect">
                <aop:pointcut expression="execution(* com.groupaop.artifactaop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
    
                <aop:before method="before" pointcut-ref="moocPiontcut"/>
    
                <aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/>
                <aop:around method="around" pointcut-ref="moocPiontcut"/>
                <aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut" throwing="throwing"/>
    
                <aop:after method="after" pointcut-ref="moocPiontcut"/>
            </aop:aspect>
        </aop:config>
     </beans>
    
    图片.png

    xml声明顺序4:

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
        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.0.xsd">
    
        <bean id="moocAspect" class="com.groupaop.artifactaop.schema.advice.MoocAspect"></bean>
        
        <bean id="aspectBiz" class="com.groupaop.artifactaop.schema.advice.biz.AspectBiz"></bean>
        
        <aop:config>
            <aop:aspect id="moocAspectAOP" ref="moocAspect">
                <aop:pointcut expression="execution(* com.groupaop.artifactaop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
    
                <aop:before method="before" pointcut-ref="moocPiontcut"/>
    
                <aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/>
    
                <aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut" throwing="throwing"/>
                <aop:around method="around" pointcut-ref="moocPiontcut"/>
                <aop:after method="after" pointcut-ref="moocPiontcut"/>
            </aop:aspect>
        </aop:config>
     </beans>
    
    图片.png

    xml声明顺序5:

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
        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.0.xsd">
    
        <bean id="moocAspect" class="com.groupaop.artifactaop.schema.advice.MoocAspect"></bean>
        
        <bean id="aspectBiz" class="com.groupaop.artifactaop.schema.advice.biz.AspectBiz"></bean>
        
        <aop:config>
            <aop:aspect id="moocAspectAOP" ref="moocAspect">
                <aop:pointcut expression="execution(* com.groupaop.artifactaop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
    
                <aop:before method="before" pointcut-ref="moocPiontcut"/>
    
                <aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/>
    
                <aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut" throwing="throwing"/>
    
                <aop:after method="after" pointcut-ref="moocPiontcut"/>
                <aop:around method="around" pointcut-ref="moocPiontcut"/>
            </aop:aspect>
        </aop:config>
     </beans>
    
    图片.png

    自己总结的一个约束

    以“xml顺序声明2”作为以后通知声明顺序的规范,即before->around->after-returning/after-throwing->after。这样around通知就和代码逻辑一样紧绕执行对象前后,after-returning和after通知的响应顺序和xml响应顺序一样。

    相关文章

      网友评论

          本文标题:同一切面内含有before、around、after-retur

          本文链接:https://www.haomeiwen.com/subject/wiiloqtx.html