【sping揭秘】15、afterreturning

Spring AOP @AfterReturning 示例
本文通过具体的代码示例展示了如何使用 Spring AOP 的 @AfterReturning 注解来实现方法执行后的通知逻辑。示例中定义了一个简单的测试 Bean 和一个切面类,用于在指定的方法调用结束后记录日志。
@afterreturning

我们同理写几个测试类

 

package cn.cutter.start.bean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

@Component
public class AterReturningTestBean {

    private static final Log logger = LogFactory.getLog(AterReturningTestBean.class);

    public void aterReturningTest() {
        logger.info("aterReturningTest sds");
    }
    
    public int aterReturningTestInt() {
        logger.info("aterReturningTestInt");
        
        return 666;
    }
    
}

 

写几个测试的拦截器

package cn.cutter.start.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 
 * @author xiaof
 *
 */
@Component
@Aspect
public class AterReturningAspect {

    private static final Log logger = LogFactory.getLog(AterReturningAspect.class);
    
    //execution(* cn.cutter.start.bean.BeofreTestBean.*(..))
    @Pointcut("execution(* cn.cutter.start.bean.AterReturningTestBean.*(..))")
    private void pointCut1() {}
    
    @AfterReturning(pointcut="pointCut1()", returning="value1")
    public void testReturn1(int value1) {
        
        logger.info("返回拦截:" + value1);
        
    }
    
    @AfterReturning(pointcut="pointCut1()")
    public void testReturn2() {
        
        logger.info("返回拦截:没有参数");
        
    }
    
}

结果:

@Test
    public void testAop4() {
        ApplicationContext ctx = this.before();
        
        AterReturningTestBean att = (AterReturningTestBean) ctx.getBean("aterReturningTestBean");
        
//        att.aterReturningTest();
        
        att.aterReturningTestInt();
        
    }

 

转载于:https://www.cnblogs.com/cutter-point/p/8977821.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值