1. 程式人生 > >Spring AOP 自動代理執行增強

Spring AOP 自動代理執行增強

這裡寫圖片描述

基本類

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");
        //代理為指向Interface的代理
        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 ...