spring-后置通知 @AfterReturning

36 篇文章 1 订阅

后置通知

目标方法执行之后增加的业务功能,由于目标方法执行之后执行,所以可以获取到目标方法返回值,该注解是 returning属性就是用于指定接收方法返回值的变量名的。

所有被注解后置通知的方法,除了可以加入JoinPoint参数外,还可以包含一个用于接收返回值的变量,该变量最好使用Object类型的,目标方法的返回值可以是任何类型的。

后置定义方法,方法是实现切面功能

方法定义要求

public公共方法
方法没有返回值 void
方法名称自定义
方法有参数,推荐使用Object,参数名自定义,用于接收目标方法的返回值

使用@AfterReturning:就是在业务逻辑方法的后面增加的功能

public class LogAspect{
	@Pointcut("@annotation(apiLog)")
	public void logPointCut(ApiLog apiLog) {}
	
	@AfterReturning(value = "logPointCut(apiLog)", argNames = "joinPoint,apiLog,returningValue", returning = "returningValue")
	public void log(JoinPoint joinPoint, ApiLog apiLog, Object returningValue) {
		...
	}
}

属性

value = "logPointCut(apiLog)" 切入点表达式
argNames = "joinPoint,apiLog,returningValue" 方法参数
returning = "returningValue"  自定义的变量,表示目标方法的返回值的
自定义变量名必须和通知方法的形参名一样

位置在方法定义的上面

特点

1. 在目标方法之后执行的
2. 能够获取到目标方法的返回值,可以根据这个返回值做不同的处理操作
3. 可以修改这个返回值

后置通知中也是可以使用JoinPoint的,并且这个必须放在第一个位置

@Component("myAspect2")
@Aspect
public class MyaspectJ {
	@AfterReturning(value ="execution(* *..SomeserviceImpl.doOther2(..))",returning = "res")
    public void myaspectJ2(JoinPoint joinPoint ,Object res){
        System.out.println(joinPoint.getSignature());
        System.out.println(joinPoint.getSignature().getName());
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
}

测试

public interface Someservice {
    String doOther(String name);
    stdent doOther2(String anme,int age);
}
@Component
public class SomeserviceImpl implements Someservice {
    @Override
    public String doOther(String name) {
        System.out.println("------目标方法执行doOther()-------");
        return name;
    }

    @Override
    public stdent doOther2(String name, int age) {
        System.out.println("------目标方法执行doOther()-------");
        stdent st = new stdent();
        st.setAge(age);
        st.setName(name);
        return st;
    }
}
returning = "res"这个的变量res要和Object res的res命名一样
@Component
@Aspect
public class MyaspectJ {
    @AfterReturning(value ="execution(* *..SomeserviceImpl.doOther(..))",returning = "res")
    public void myaspectJ(Object res){
        System.out.println("后置通知的返回值为:"+res);
        res = "18204229-"+res;
        System.out.println("修改之后的:"+res);
    }

    @AfterReturning(value ="execution(* *..SomeserviceImpl.doOther2(..))",returning = "res")
    public void myaspectJ2(JoinPoint joinPoint ,Object res){
        System.out.println(joinPoint.getSignature());
        System.out.println(joinPoint.getSignature().getName());
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
        stdent stdent = new stdent();
        stdent.setAge(22);
        stdent.setName("44455");
        res = stdent;
        System.out.println("后置通知中:"+res);
    }
}
    @Test
    public void test02(){
        String config="ApplicationContesxt1.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //从容器获取目标对象
        Someservice someservice = ac.getBean("someserviceImpl");
        //通过代理对象执行方法,实现目标方法执行时,增强了功能
        String str = someservice.doOther("ycy");
        System.out.println(str);
    }
    //返回一个对象,是否发生改变
    @Test
    public void test03(){
        String config="ApplicationContesxt1.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //从容器获取目标对象
        Someservice someservice = ac.getBean("someserviceImpl");
        //通过代理对象执行方法,实现目标方法执行时,增强了功能
        stdent str = someservice.doOther2("ycy",24);
        System.out.println(str);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值