筆記13 AOP中After和AfterReturning的區別

AOP中 @Before @After @AfterThrowing@AfterReturning的執行順序java

 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爲例,代碼以下:spring

 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 }

執行結果:app

注入AspectJ切面 (新)this

1.將原來的觀衆類定義爲一個真正的切面,Audience.java 將觀衆的行爲都放在這個切面中,而後在spring中引用這個切面,而且爲這個切面注入新的方法,具體可參照筆記12。spa

 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配置文件code

 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.結果orm

相關文章
相關標籤/搜索