![这里写图片描述](https://img-blog.csdn.net/20170426002605367?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjYxMTU3MzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
基本类
package com.advice;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/25 23:41
*/
public class Performer {
public void doPerform() {
System.out.println("Performer do perform ....................... ");
}
}
增强类(Advice类)
package com.advice;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/25 23:42
*/
public class Audience {
public void takeSeas(){
System.out.println("The audience is taking their seats.");
}
public void turnOffPhone(){
System.out.println("The audience is turn off their cellphone.");
}
public void applaund(){
System.out.println("CLAP CLAP CLAP CLAP ...");
}
public void demandRefund(){
System.out.println("Boo! we want our money back!");
}
public void watchPerfomance(ProceedingJoinPoint joinPoint) {
try {
Long start = System.currentTimeMillis();
joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("The performance took "+(end-start)+" milliseconds");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
Spring 配置
此配置为:在执行函数 doPerform 的时刻执行不同的方法
<bean id="performer" class="com.advice.Performer"/>
<bean id="audience" class="com.advice.Audience"/>
<aop:config>
<aop:aspect ref="audience">
<aop:before method="takeSeas" pointcut="execution(* com.advice.Performer.doPerform(..))"/>
<aop:before method="turnOffPhone" pointcut="execution(* com.advice.Performer.doPerform(..))"/>
<aop:after-returning method="applaund" pointcut="execution(* com.advice.Performer.doPerform(..))"/>
<aop:after-throwing method="demandRefund" pointcut="execution(* com.advice.Performer.doPerform(..))"/>
<aop:around method="watchPerfomance" pointcut="execution(* com.advice.Performer.doPerform(..))"/>
</aop:aspect>
</aop:config>
也可以这样配置
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="doPerform" expression="execution(* com.advice.Performer.doPerform(..))"/>
<aop:before method="takeSeas" pointcut-ref="doPerform"/>
<aop:before method="turnOffPhone" pointcut-ref="doPerform"/>
<aop:after-returning method="applaund" pointcut-ref="doPerform"/>
<aop:after-throwing method="demandRefund" pointcut-ref="doPerform"/>
<aop:around method="watchPerfomance" pointcut-ref="doPerform"/>
</aop:aspect>
</aop:config>
第二种配置:常用语事务处理配置
当执行 ServiceImpl 内任何函数的时候,都需要通知 requestAD 执行里面的相应的函数
<bean id="requestAD" class="com.core.impl.ServiceExecutionAdvice"/>
<aop:config expose-proxy="true">
<aop:pointcut id="servicePointcut" expression="execution(* *..*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="requestAD" pointcut-ref="servicePointcut"/>
</aop:config>
测试类
/**
* Method: doPerform()
*/
@Test
public void testDoPerform() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-another-context.xml");
Performer performer = (Performer) context.getBean("performer");
System.out.println("+++++++++++++++++++++++++++++++++");
performer.doPerform();
}
测试结果
+++++++++++++++++++++++++++++++++
2017-04-26 00:06:31,133 DEBUG [main] (AbstractBeanFactory.java:251) - Returning cached instance of singleton bean 'audience'
The audience is taking their seats.
2017-04-26 00:06:31,133 DEBUG [main] (AbstractBeanFactory.java:251) - Returning cached instance of singleton bean 'audience'
The audience is turn off their cellphone.
Performer do perform .......................
2017-04-26 00:06:31,169 DEBUG [main] (AbstractBeanFactory.java:251) - Returning cached instance of singleton bean 'audience'
CLAP CLAP CLAP CLAP ...