环绕通知:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="instrumentalist" class="imple.Instrumentalist" >
<property name="song" value="Jingle Bells" />
<property name="instrument" >
<bean class="imple.Saxophone"/>
</property>
</bean> <bean id="saxophone" class="imple.Saxophone" /> <!-- aop -->
<bean id="audience" class="AopTest.Audience"/> <aop:config>
<aop:aspect ref="audience"> <aop:pointcut expression="execution(* imple.Saxophone.play(..))" id="performance"/>
<aop:around pointcut-ref="performance" method="watchPerformance"/>
</aop:aspect> </aop:config> </beans>
Saxophone:
package imple; import inter.Instrument; public class Saxophone implements Instrument{ private String song = "a"; public Saxophone(){ } public void play() {
System.out.println("TOOT TOOT TOOT");
} public void setSong(String song) {
this.song = song;
} }
package imple; import AopTest.aop;
import inter.Instrument;
import inter.Performer; public class Instrumentalist implements Performer { private String song;
private Instrument instrument; public void perform() {
// System.out.println("playing " + song + " : ");
instrument.play();
} public String getSong() {
return song;
} public void setSong(String song) {
this.song = song;
} public Instrument getInstrument() {
return instrument;
} public void setInstrument(Instrument instrument) {
this.instrument = instrument;
} }
运行结果:
begin!
TOOT TOOT TOOT
end! performance took 0 milliseconds
前置后置通知:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="instrumentalist" class="imple.Instrumentalist" >
<property name="song" value="Jingle Bells" />
<property name="instrument" >
<bean class="imple.Saxophone"/>
</property>
</bean> <bean id="saxophone" class="imple.Saxophone" />
<!-- aop -->
<bean id="audience" class="AopTest.Audience"/> <aop:config>
<aop:aspect ref="audience"> <aop:pointcut expression="execution(* imple.Saxophone.play(..))" id="performance"/> <aop:before method="takeSeats" pointcut-ref="performance"/>
<aop:before method="turnoffCellPhones" pointcut-ref="performance"/>
<aop:after-returning method="applaud" pointcut-ref="performance"/>
<aop:after-throwing method="demandRefund" pointcut-ref="performance"/>
</aop:aspect> </aop:config> </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
-----------------------------------------------------------以下基于注解实现前置后置通知-----------------------------------------------------------------------------
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="audienceAspectJ" class="AopTest.AudienceAspectJ"/> <bean id="instrumentalist" class="imple.Instrumentalist" >
<property name="song" value="Jingle Bells" />
<property name="instrument" >
<bean class="imple.Saxophone"/>
</property>
</bean> </beans>
package AopTest; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class AudienceAspectJ { @Pointcut("execution(* imple.Saxophone.play(..))") //表达式
public void performance(){ //performance切点名字 } @Before("performance()")
public void takeSeats(){
System.out.println("AudienceAspectJ the audience is taking their seats.");
} @Before("performance()")
public void turnoffCellPhones(){
System.out.println("AudienceAspectJ the audience is turning off their cellphones");
} @AfterReturning("performance()")
public void applaud(){
System.out.println("AudienceAspectJ CLAP CLAP CLAP CLAP CLAP");
} @AfterThrowing("performance()")
public void demandRefund(){
System.out.println("AudienceAspectJ Boo! we wangt our money back!");
} }
package imple; import AopTest.aop;
import inter.Instrument;
import inter.Performer; public class Instrumentalist implements Performer { private String song;
private Instrument instrument; public void perform() {
// System.out.println("playing " + song + " : ");
instrument.play();
} public String getSong() {
return song;
} public void setSong(String song) {
this.song = song;
} public Instrument getInstrument() {
return instrument;
} public void setInstrument(Instrument instrument) {
this.instrument = instrument;
} }
package imple; import inter.Instrument; public class Saxophone implements Instrument{ private String song = "a"; public Saxophone(){ } public void play() {
System.out.println("TOOT TOOT TOOT");
} public void setSong(String song) {
this.song = song;
} }
@Test
public void test10(){
Instrumentalist instrumentalist = (Instrumentalist) ctx.getBean("instrumentalist");
instrumentalist.perform(); }
输出结果:
AudienceAspectJ the audience is taking their seats.
AudienceAspectJ the audience is turning off their cellphones
TOOT TOOT TOOT
AudienceAspectJ CLAP CLAP CLAP CLAP CLAP
-------------------------------------------------注解环绕通知--------------------------------------------------------------------------------
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="audienceAspectJ" class="AopTest.AudienceAspectJ"/> <bean id="instrumentalist" class="imple.Instrumentalist" >
<property name="song" value="Jingle Bells" />
<property name="instrument" >
<bean class="imple.Saxophone"/>
</property>
</bean> </beans>
package AopTest; 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 AudienceAspectJ { @Pointcut("execution(* imple.Saxophone.play(..))")
public void performance(){ } @Around("performance()")
public void watchPerformance(ProceedingJoinPoint joinpoint) {
try {
System.out.println("AudienceAspectJ @Around begin!");
long start = System.currentTimeMillis(); joinpoint.proceed(); long end = System.currentTimeMillis();
System.out.println("AudienceAspectJ @Around end! performance took " + (end - start) + " milliseconds");
} catch (Throwable e) {
System.out.println("AudienceAspectJ @Around eee!We want our money back!");
}
} }
package imple; import AopTest.aop;
import inter.Instrument;
import inter.Performer; public class Instrumentalist implements Performer { private String song;
private Instrument instrument; public void perform() {
// System.out.println("playing " + song + " : ");
instrument.play();
} public String getSong() {
return song;
} public void setSong(String song) {
this.song = song;
} public Instrument getInstrument() {
return instrument;
} public void setInstrument(Instrument instrument) {
this.instrument = instrument;
} }
package imple; import inter.Instrument; public class Saxophone implements Instrument{ private String song = "a"; public Saxophone(){ } public void play() {
System.out.println("TOOT TOOT TOOT");
} public void setSong(String song) {
this.song = song;
} }
@Test
public void test10(){
Instrumentalist instrumentalist = (Instrumentalist) ctx.getBean("instrumentalist");
instrumentalist.perform(); }
输出结果
AudienceAspectJ @Around begin!
TOOT TOOT TOOT
AudienceAspectJ @Around end! performance took 0 milliseconds