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 ...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值