一、注解@AspectJ形式
1.
package com.springinaction.springidol; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AroundAudience { @Pointcut("execution(* com.springinaction.springidol.Performer.perform(..))") public void performance() { } //<start id="audience_around_bean" /> @Around("performance()") public void watchPerformance(ProceedingJoinPoint joinpoint) { try { System.out.println("The audience is taking their seats."); System.out.println("The audience is turning off their cellphones"); long start = System.currentTimeMillis(); joinpoint.proceed(); long end = System.currentTimeMillis(); System.out.println("CLAP CLAP CLAP CLAP CLAP"); System.out.println("The performance took " + (end - start) + " milliseconds."); } catch (Throwable t) { System.out.println("Boo! We want our money back!"); } } //<end id="audience_around_bean" /> }
The advice method will do everything it needs to do; and when it’s ready to pass control to the advised method, it will call ProceedingJoinPoint ’s proceed() method.
What’s also interesting is that just as you can omit a call to the proceed() method to block access to the advised method, you can also invoke it multiple times from within the advice. One reason for doing this may be to implement retry logic to perform repeated attempts on the advised method should it fail.
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 <bean class="com.springinaction.springidol.AroundAudience" /> 18 19 <aop:aspectj-autoproxy /> 20 21 </beans>
3.或用java配置文件
1 package com.springinaction.springidol; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.ComponentScan; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.context.annotation.EnableAspectJAutoProxy; 7 8 @Configuration 9 @EnableAspectJAutoProxy 10 @ComponentScan 11 public class Config{ 12 @Bean 13 public AroundAudience aroundAudience() { 14 return new AroundAudience(); 15 } 16 17 @Bean 18 public Performer eddie() { 19 Instrumentalist instrumentalist = new Instrumentalist(); 20 instrumentalist.setInstrument(instrument()); 21 return instrumentalist; 22 } 23 24 @Bean 25 public Instrument instrument() { 26 return new Guitar(); 27 } 28 }
4.测试
1 package com.springinaction.springidol; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10 @RunWith(SpringJUnit4ClassRunner.class) 11 //@ContextConfiguration("spring-aroundAdvice.xml") 12 @ContextConfiguration(classes=Config.class)//指定配置文件 13 public class AroundAdviceTest { 14 @Autowired 15 ApplicationContext context; 16 17 @Test 18 public void audienceShouldApplaud() throws Exception { 19 Performer eddie = (Performer) context.getBean("eddie"); 20 eddie.perform(); 21 } 22 23 }
结果:
The audience is taking their seats.
The audience is turning off their cellphones
Strum strum strum
CLAP CLAP CLAP CLAP CLAP
The performance took 0 milliseconds.
二、xml形式<aop:config></aop:config>
1.
1 package com.springinaction.springidol; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 5 public class AroundAudience { 6 //<start id="audience_around_bean"/> 7 public void watchPerformance(ProceedingJoinPoint joinpoint) { 8 try { 9 System.out.println("The audience is taking their seats."); 10 System.out.println("The audience is turning off their cellphones"); 11 long start = System.currentTimeMillis(); //<co id="co_beforeProceed"/> 12 13 joinpoint.proceed(); //<co id="co_proceed"/> 14 15 long end = System.currentTimeMillis(); // <co id="co_afterProceed"/> 16 System.out.println("CLAP CLAP CLAP CLAP CLAP"); 17 System.out.println("The performance took " + (end - start) 18 + " milliseconds."); 19 } catch (Throwable t) { 20 System.out.println("Boo! We want our money back!"); //<co id="co_afterException"/> 21 } 22 } 23 //<end id="audience_around_bean"/> 24 }
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.AroundAudience" /> 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 /> 28 29 <aop:around 30 pointcut-ref="performance" 31 method="watchPerformance" /> 32 </aop:aspect> 33 </aop:config> 34 <!-- <end id="audience_aspect" /> --> 35 36 </beans>