Spring的环绕通知
----------------------------
package com.springinaction.springidol;
import org.aspectj.lang.ProceedingJoinPoint;
public class Audience {
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}
public void turnOffCellPhones(){
System.out.println("The audience is turning off their cellphones.");
}
public void applaud(){
System.out.println("CLAP CLAP CLAP");
}
public void demandRefund(){
System.out.println("Boo! we want our money back!");
}
public void watchPerformance(ProceedingJoinPoint joinpoint){
System.out.println("nnnnnn");
try{
takeSeats();
turnOffCellPhones();
long start = System.currentTimeMillis();
joinpoint.proceed();
long end = System.currentTimeMillis();
applaud();
//统计时间
}catch(Throwable t){
demandRefund();
}
}
}
package com.springinaction.springidol;
public class Performer {
public void perform(){
System.out.println("actors perform...");
}
}
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="performer" class="com.springinaction.springidol.Performer" />
<bean id="audience" class="com.springinaction.springidol.Audience" />
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut expression="execution(* com.springinaction.springidol.Performer.perform(..))" id="performance2"/>
<aop:around pointcut-ref="performance2" method="watchPerformance"/>
</aop:aspect>
</aop:config>
</beans>
package com.springinaction.springidol;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args){
ApplicationContext appContext = new ClassPathXmlApplicationContext("t.xml");
Performer p= appContext.getBean("performer", Performer.class);
p.perform();
}
}