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

Posted shamgod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-006-定义切面使用xml相关的知识,希望对你有一定的参考价值。

一、

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

 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>

 

以上是关于SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-006-定义切面使用xml的主要内容,如果未能解决你的问题,请参考以下文章

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

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

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-007-定义切面的around advice

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2 配置文件为XML

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@ScopeProxyMode