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

时间:2022-12-09 15:45:42

一、注解@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.

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="eddie"
class="com.springinaction.springidol.Instrumentalist">
<property name="instrument">
<bean class="com.springinaction.springidol.Guitar" />
</property>
</bean> <bean class="com.springinaction.springidol.AroundAudience" /> <aop:aspectj-autoproxy /> </beans>

3.或用java配置文件

 package com.springinaction.springidol;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class Config{
@Bean
public AroundAudience aroundAudience() {
return new AroundAudience();
} @Bean
public Performer eddie() {
Instrumentalist instrumentalist = new Instrumentalist();
instrumentalist.setInstrument(instrument());
return instrumentalist;
} @Bean
public Instrument instrument() {
return new Guitar();
}
}

4.测试

 package com.springinaction.springidol;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("spring-aroundAdvice.xml")
@ContextConfiguration(classes=Config.class)//指定配置文件
public class AroundAdviceTest {
@Autowired
ApplicationContext context; @Test
public void audienceShouldApplaud() throws Exception {
Performer eddie = (Performer) context.getBean("eddie");
eddie.perform();
} }

结果:

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.

 package com.springinaction.springidol;

 import org.aspectj.lang.ProceedingJoinPoint;

 public class AroundAudience {
//<start id="audience_around_bean"/>
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(); //<co id="co_beforeProceed"/> joinpoint.proceed(); //<co id="co_proceed"/> long end = System.currentTimeMillis(); // <co id="co_afterProceed"/>
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!"); //<co id="co_afterException"/>
}
}
//<end id="audience_around_bean"/>
}

2.

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="eddie"
class="com.springinaction.springidol.Instrumentalist">
<property name="instrument">
<bean class="com.springinaction.springidol.Guitar" />
</property>
</bean> <!-- <start id="audience_bean" /> -->
<bean id="audience"
class="com.springinaction.springidol.AroundAudience" />
<!-- <end id="audience_bean" /> --> <!-- <start id="audience_aspect" /> -->
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression=
"execution(* com.springinaction.springidol.Performer.perform(..))"
/> <aop:around
pointcut-ref="performance"
method="watchPerformance" />
</aop:aspect>
</aop:config>
<!-- <end id="audience_aspect" /> --> </beans>