spring 切面 前置后置通知 环绕通知demo

环绕通知:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xmlns:aop="http://www.springframework.org/schema/aop"
 6         xmlns:tx="http://www.springframework.org/schema/tx"
 7         xmlns:p="http://www.springframework.org/schema/p"
 8         xsi:schemaLocation="
 9             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12     
13 
14     <bean id="instrumentalist" class="imple.Instrumentalist" >
15         <property name="song" value="Jingle Bells" />
16         <property name="instrument" >
17             <bean class="imple.Saxophone"/>
18         </property>
19     </bean>
20     
21     <bean id="saxophone" class="imple.Saxophone" />
22     
23     
24     <!-- aop -->
25     <bean id="audience" class="AopTest.Audience"/>
26     
27     <aop:config>
28         <aop:aspect ref="audience"> 
29 
30              <aop:pointcut expression="execution(* imple.Saxophone.play(..))" id="performance"/>
31              <aop:around pointcut-ref="performance" method="watchPerformance"/>
32         </aop:aspect>
33         
34     </aop:config>
35     
36 
37 
38 </beans>

Saxophone:

 1 package imple;
 2 
 3 import inter.Instrument;
 4 
 5 public class Saxophone implements Instrument{
 6     
 7     private String song = "a";
 8 
 9     public Saxophone(){
10         
11     }
12     
13     public void play() {
14         System.out.println("TOOT TOOT TOOT");
15     }
16 
17     public void setSong(String song) {
18         this.song = song;
19     }
20     
21 
22 }
 1 package imple;
 2 
 3 import AopTest.aop;
 4 import inter.Instrument;
 5 import inter.Performer;
 6 
 7 public class Instrumentalist implements Performer {
 8     
 9     private String song;
10     private Instrument instrument;
11 
12     public void perform() {
13     //    System.out.println("playing " + song + " : ");
14         instrument.play();
15     }
16 
17     public String getSong() {
18         return song;
19     }
20 
21     public void setSong(String song) {
22         this.song = song;
23     }
24 
25     public Instrument getInstrument() {
26         return instrument;
27     }
28 
29     public void setInstrument(Instrument instrument) {
30         this.instrument = instrument;
31     }
32     
33 
34 }

运行结果:

begin!
TOOT TOOT TOOT
end! performance took 0 milliseconds

前置后置通知:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xmlns:aop="http://www.springframework.org/schema/aop"
 6         xmlns:tx="http://www.springframework.org/schema/tx"
 7         xmlns:p="http://www.springframework.org/schema/p"
 8         xsi:schemaLocation="
 9             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12     
13     <bean id="instrumentalist" class="imple.Instrumentalist" >
14         <property name="song" value="Jingle Bells" />
15         <property name="instrument" >
16             <bean class="imple.Saxophone"/>
17         </property>
18     </bean>
19     
20     <bean id="saxophone" class="imple.Saxophone" />
21     <!-- aop -->
22     <bean id="audience" class="AopTest.Audience"/>
23     
24     <aop:config>
25         <aop:aspect ref="audience"> 
26             
27                  <aop:pointcut expression="execution(* imple.Saxophone.play(..))" id="performance"/>
28                 
29                 <aop:before method="takeSeats" pointcut-ref="performance"/>
30                 <aop:before method="turnoffCellPhones" pointcut-ref="performance"/>
31                 <aop:after-returning  method="applaud" pointcut-ref="performance"/>
32                 <aop:after-throwing method="demandRefund" pointcut-ref="performance"/> 
33         </aop:aspect>
34         
35     </aop:config>
36     
37 
38 
39 </beans>

其余同上

测试代码:

    @Test
    public void test6() {
        Instrumentalist instrumentalist = (Instrumentalist) ctx.getBean("instrumentalist");
        instrumentalist.perform();
    }

运行结果:

the audience is taking their seats.
the audience is turning  off their cellphones
TOOT TOOT TOOT
CLAP CLAP CLAP CLAP CLAP

-----------------------------------------------------------以下基于注解实现前置后置通知-----------------------------------------------------------------------------

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xmlns:aop="http://www.springframework.org/schema/aop"
 6         xmlns:tx="http://www.springframework.org/schema/tx"
 7         xmlns:p="http://www.springframework.org/schema/p"
 8         xsi:schemaLocation="
 9             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12 
13     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
14     
15     <bean id="audienceAspectJ" class="AopTest.AudienceAspectJ"/>
16     
17 
18     <bean id="instrumentalist" class="imple.Instrumentalist" >
19         <property name="song" value="Jingle Bells" />
20         <property name="instrument" >
21             <bean class="imple.Saxophone"/>
22         </property>
23     </bean>
24     
25    
26 
27 </beans>
 1 package AopTest;
 2 
 3 import org.aspectj.lang.annotation.AfterReturning;
 4 import org.aspectj.lang.annotation.AfterThrowing;
 5 import org.aspectj.lang.annotation.Aspect;
 6 import org.aspectj.lang.annotation.Before;
 7 import org.aspectj.lang.annotation.Pointcut;
 8 
 9 @Aspect
10 public class AudienceAspectJ {
11     
12 
13     @Pointcut("execution(* imple.Saxophone.play(..))")  //表达式
14     public void performance(){  //performance切点名字
15         
16     }
17     
18     @Before("performance()")
19     public void takeSeats(){
20         System.out.println("AudienceAspectJ the audience is taking their seats.");
21     }
22     
23     @Before("performance()")
24     public void turnoffCellPhones(){
25         System.out.println("AudienceAspectJ the audience is turning  off their cellphones");
26     }
27     
28     @AfterReturning("performance()")
29     public void applaud(){
30         System.out.println("AudienceAspectJ CLAP CLAP CLAP CLAP CLAP");    
31     }
32     
33     @AfterThrowing("performance()")
34     public void demandRefund(){
35         System.out.println("AudienceAspectJ Boo! we wangt our money back!");
36     }
37     
38     
39 
40 }
 1 package imple;
 2 
 3 import AopTest.aop;
 4 import inter.Instrument;
 5 import inter.Performer;
 6 
 7 public class Instrumentalist implements Performer {
 8     
 9     private String song;
10     private Instrument instrument;
11 
12     public void perform() {
13     //    System.out.println("playing " + song + " : ");
14         instrument.play();
15     }
16 
17     public String getSong() {
18         return song;
19     }
20 
21     public void setSong(String song) {
22         this.song = song;
23     }
24 
25     public Instrument getInstrument() {
26         return instrument;
27     }
28 
29     public void setInstrument(Instrument instrument) {
30         this.instrument = instrument;
31     }
32     
33 
34 }
 1 package imple;
 2 
 3 import inter.Instrument;
 4 
 5 public class Saxophone implements Instrument{
 6     
 7     private String song = "a";
 8 
 9     public Saxophone(){
10         
11     }
12     
13     public void play() {
14         System.out.println("TOOT TOOT TOOT");
15     }
16 
17     public void setSong(String song) {
18         this.song = song;
19     }
20     
21 
22 }
1     @Test
2     public void test10(){
3         Instrumentalist instrumentalist = (Instrumentalist) ctx.getBean("instrumentalist");
4         instrumentalist.perform();
5         
6     }

输出结果:

AudienceAspectJ the audience is taking their seats.
AudienceAspectJ the audience is turning  off their cellphones
TOOT TOOT TOOT
AudienceAspectJ CLAP CLAP CLAP CLAP CLAP

-------------------------------------------------注解环绕通知-------------------------------------------------------------------------------- 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xmlns:aop="http://www.springframework.org/schema/aop"
 6         xmlns:tx="http://www.springframework.org/schema/tx"
 7         xmlns:p="http://www.springframework.org/schema/p"
 8         xsi:schemaLocation="
 9             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12 
13     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
14     
15     <bean id="audienceAspectJ" class="AopTest.AudienceAspectJ"/>
16     
17 
18     <bean id="instrumentalist" class="imple.Instrumentalist" >
19         <property name="song" value="Jingle Bells" />
20         <property name="instrument" >
21             <bean class="imple.Saxophone"/>
22         </property>
23     </bean>
24    
25 </beans>
 1 package AopTest;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 import org.aspectj.lang.annotation.Around;
 5 import org.aspectj.lang.annotation.Aspect;
 6 import org.aspectj.lang.annotation.Pointcut;
 7 
 8 @Aspect
 9 public class AudienceAspectJ {
10     
11 
12     @Pointcut("execution(* imple.Saxophone.play(..))")
13     public void performance(){
14         
15     }
16     
17 
18     @Around("performance()")
19     public void watchPerformance(ProceedingJoinPoint joinpoint) {
20         try {
21             System.out.println("AudienceAspectJ @Around begin!");
22             long start = System.currentTimeMillis();
23 
24             joinpoint.proceed();
25 
26             long end = System.currentTimeMillis();
27             System.out.println("AudienceAspectJ @Around end!        performance took " + (end - start) + " milliseconds");
28         } catch (Throwable e) {
29             System.out.println("AudienceAspectJ @Around  eee!We want our money back!");
30         }
31     }
32     
33     
34 
35 }
 1 package imple;
 2 
 3 import AopTest.aop;
 4 import inter.Instrument;
 5 import inter.Performer;
 6 
 7 public class Instrumentalist implements Performer {
 8     
 9     private String song;
10     private Instrument instrument;
11 
12     public void perform() {
13     //    System.out.println("playing " + song + " : ");
14         instrument.play();
15     }
16 
17     public String getSong() {
18         return song;
19     }
20 
21     public void setSong(String song) {
22         this.song = song;
23     }
24 
25     public Instrument getInstrument() {
26         return instrument;
27     }
28 
29     public void setInstrument(Instrument instrument) {
30         this.instrument = instrument;
31     }
32     
33 
34 }
 1 package imple;
 2 
 3 import inter.Instrument;
 4 
 5 public class Saxophone implements Instrument{
 6     
 7     private String song = "a";
 8 
 9     public Saxophone(){
10         
11     }
12     
13     public void play() {
14         System.out.println("TOOT TOOT TOOT");
15     }
16 
17     public void setSong(String song) {
18         this.song = song;
19     }
20     
21 
22 }
1     @Test
2     public void test10(){
3         Instrumentalist instrumentalist = (Instrumentalist) ctx.getBean("instrumentalist");
4         instrumentalist.perform();
5         
6     }

输出结果

1 AudienceAspectJ @Around begin!
2 TOOT TOOT TOOT
3 AudienceAspectJ @Around end!        performance took 0 milliseconds
原文地址:https://www.cnblogs.com/a757956132/p/5036069.html