码迷,mamicode.com
首页 > 其他好文 > 详细

笔记13 AOP中After和AfterReturning的区别

时间:2018-04-22 14:49:29      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:AC   --   interface   aop   nes   ntc   ota   die   one   

AOP中 @Before @After @AfterThrowing@AfterReturning的执行顺序

 1 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 2    Object result;
 3    try {
 4        //@Before
 5        result = method.invoke(target, args);
 6        //@After
 7        return result;
 8    } catch (InvocationTargetException e) {
 9        Throwable targetException = e.getTargetException();
10        //@AfterThrowing
11        throw targetException;
12    } finally {
13        //@AfterReturning
14    }
15 }

以Audience为例,代码如下:

 1 package concert;
 2 
 3 import org.aspectj.lang.annotation.After;
 4 import org.aspectj.lang.annotation.AfterReturning;
 5 import org.aspectj.lang.annotation.AfterThrowing;
 6 import org.aspectj.lang.annotation.Aspect;
 7 import org.aspectj.lang.annotation.Before;
 8 import org.aspectj.lang.annotation.Pointcut;
 9 import org.springframework.stereotype.Component;
10 
11 @Aspect
12 @Component
13 public class Audience {
14     @Pointcut("execution(* concert.Performance.perform(..))")
15     public void performs() {
16 
17     }
18 
19     @Before("performs()")
20     public void silenceCellPhones() {
21         System.out.println("Silencing cell phone");
22     }
23 
24     @Before("performs()")
25     public void takeSeats() {
26         System.out.println("Taking seats");
27     }
28 
29     @After("performs()")
30     public void after() {
31         System.out.println("after");
32     }
33 
34     @AfterReturning("performs()")
35     public void applause() {
36         System.out.println("CLAP CLAP CLAP");
37     }
38 
39     @AfterThrowing("performs()")
40     public void demandRefund() {
41         System.out.println("Demanding a refund");
42     }
43 }

执行结果:

技术分享图片

注入AspectJ切面 (新)

1.将原来的观众类定义为一个真正的切面,Audience.java 将观众的行为都放在这个切面中,然后在spring中引用这个切面,并且为这个切面注入新的方法,具体可参照笔记12。

 1 package concert4;
 2 
 3 public aspect Audience {
 4     public Audience(){}
 5     pointcut performance():execution(* perform(..));
 6     
 7     before():performance(){
 8         System.out.println("Silencing cell phone");
 9     }
10     before():performance(){
11         System.out.println("Taking seats");
12     }
13     after():performance(){
14         System.out.println("CLAP CLAP CLAP");
15     }
16     after()returning :performance(){
17         System.out.println("--------------评论----------------");
18         System.out.println(criticismEngine.getCriticism());
19         System.out.println("----------------------------------");
20     }
21     
22     private CriticismEngine criticismEngine;
23     
24     public void setCriticismEngine(CriticismEngine criticismEngine){
25         this.criticismEngine=criticismEngine;
26     }
27 }

2.修改XML配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xmlns:c="http://www.springframework.org/schema/c"
 8     xsi:schemaLocation="
 9    http://www.springframework.org/schema/beans
10    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
11    http://www.springframework.org/schema/aop
12    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
13    http://www.springframework.org/schema/tx
14    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
15    http://www.springframework.org/schema/context     
16    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
17            
18            <bean class="concert4.Classcial"></bean>
19         <bean id="criticismEngine" class="concert4.CriticismEngineTmpl">
20             <property name="criticismPool">
21                 <list>
22                     <value>不好</value>
23                     <value>很不好</value>
24                     <value>非常不好</value>
25                 </list>
26             </property>
27         </bean>
28         <bean id="audience" class="concert4.Audience" factory-method="aspectOf">
29             <property name="criticismEngine" ref="criticismEngine"></property>
30         </bean>
31             <aop:config>
32                    <aop:aspect ref="audience">                                         
33                        <aop:declare-parents types-matching="concert4.Performance+" 
34                                             implement-interface="concert4.Encoreable" 
35                                             default-impl="concert4.DefaultEncoreable"/>
36                    </aop:aspect>
37            </aop:config>
38              
39    </beans>

3.结果

技术分享图片

 

笔记13 AOP中After和AfterReturning的区别

标签:AC   --   interface   aop   nes   ntc   ota   die   one   

原文地址:https://www.cnblogs.com/lyj-gyq/p/8907132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!