SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-006-定义切面使用xml

一、

 

you can also define pointcuts that can be used across multiple aspects by placing the <aop:pointcut> elements within the scope of the <aop:config> element.

 

 

1.其他类和上个例子一样,观众类比使用@AspectJ方式的少了@AspectJ标签及pointcut,before、after等定义

 1 package com.springinaction.springidol;
 2 
 3 public class Audience {
 4   public void takeSeats() { //<co id="co_takeSeats"/>
 5     System.out.println("The audience is taking their seats.");
 6   }
 7 
 8   public void turnOffCellPhones() { //<co id="co_turnOffCellPhones"/>
 9     System.out.println("The audience is turning off their cellphones");
10   }
11 
12   public void applaud() { //<co id="co_applaud"/>
13     System.out.println("CLAP CLAP CLAP CLAP CLAP");
14   }
15 
16   public void demandRefund() { //<co id="co_demandRefund"/>
17     System.out.println("Boo! We want our money back!");
18   }
19 }

 

2.xml(由于直接在xml定义切面,所以不用写<AOP:ASPECTJ-AUTOPROXY>)

(1)

 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  xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6      http://www.springframework.org/schema/beans/spring-beans.xsd
 7      http://www.springframework.org/schema/aop
 8      http://www.springframework.org/schema/aop/spring-aop.xsd">
 9 
10   <bean id="eddie"
11       class="com.springinaction.springidol.Instrumentalist">
12     <property name="instrument">
13       <bean class="com.springinaction.springidol.Guitar" />
14     </property>
15   </bean>
16 
17   <!--<start id="audience_bean" />--> 
18   <bean id="audience" 
19       class="com.springinaction.springidol.Audience" />
20   <!--<end id="audience_bean" />-->
21 
22   <!--<start id="audience_aspect" />--> 
23 <aop:config>
24   <aop:aspect ref="audience"><!--<co id="co_refAudienceBean"/>-->
25 
26     <aop:before pointcut=
27          "execution(* com.springinaction.springidol.Performer.perform(..))"
28       method="takeSeats" /> <!--<co id="co_beforePointcut"/>-->
29        
30     <aop:before pointcut=
31          "execution(* com.springinaction.springidol.Performer.perform(..))"
32       method="turnOffCellPhones" /> <!--<co id="co_beforePointcut2"/>-->
33        
34     <aop:after-returning pointcut=
35          "execution(* com.springinaction.springidol.Performer.perform(..))" 
36       method="applaud" /> <!--<co id="co_afterPointcut"/>-->
37        
38     <aop:after-throwing pointcut=
39          "execution(* com.springinaction.springidol.Performer.perform(..))" 
40       method="demandRefund" /> <!--<co id="co_afterThrowingPointcut"/>-->
41        
42  </aop:aspect>
43 </aop:config>
44   <!--<end id="audience_aspect" />-->
45 
46 </beans>

 织入的图解

(2)或

 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  xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6      http://www.springframework.org/schema/beans/spring-beans.xsd
 7      http://www.springframework.org/schema/aop
 8      http://www.springframework.org/schema/aop/spring-aop.xsd">
 9 
10   <bean id="eddie"
11       class="com.springinaction.springidol.Instrumentalist">
12     <property name="instrument">
13       <bean class="com.springinaction.springidol.Guitar" />
14     </property>
15   </bean>
16 
17   <!--<start id="audience_bean" />--> 
18   <bean id="audience" 
19       class="com.springinaction.springidol.Audience" />
20   <!--<end id="audience_bean" />-->
21 
22 <!--<start id="audience_aspect" />--> 
23 <aop:config>
24   <aop:aspect ref="audience">
25     <aop:pointcut id="performance" expression=
26         "execution(* com.springinaction.springidol.Performer.perform(..))" 
27         /> <!--<co id="co_defPointcut"/>-->
28         
29     <aop:before 
30         pointcut-ref="performance"
31         method="takeSeats" /> <!--<co id="co_refPointcut"/>-->
32     <aop:before 
33         pointcut-ref="performance"
34         method="turnOffCellPhones" /> <!--<co id="co_refPointcut"/>-->
35     <aop:after-returning
36         pointcut-ref="performance" 
37         method="applaud" /> <!--<co id="co_refPointcut"/>-->
38     <aop:after-throwing 
39         pointcut-ref="performance" 
40         method="demandRefund" /> <!--<co id="co_refPointcut"/>-->
41   </aop:aspect>
42 </aop:config>
43 <!--<end id="audience_aspect" />-->
44 
45 <!--<start id="audience_around_advice" />--> 
46 <aop:config>
47   <aop:aspect ref="audience">
48     <aop:pointcut id="performance2" expression=
49         "execution(* com.springinaction.springidol.Performer.perform(..))" 
50         />
51         
52     <aop:around 
53         pointcut-ref="performance2"
54         method="watchPerformance()" /> <!--<co id="co_around"/>-->
55   </aop:aspect>
56 </aop:config>
57 <!--<end id="audience_around_advice" />-->
58 </beans>

 

posted @ 2016-03-03 17:53  shamgod  阅读(395)  评论(0编辑  收藏  举报
haha