当前位置: 代码网 > it编程>编程语言>Java > spring后置通知@AfterReturning的使用

spring后置通知@AfterReturning的使用

2024年05月28日 Java 我要评论
后置通知在目标方法执行之后,增加的业务功能,由于目标方法执行之后执行,所有可以获取到目标方法返回值,该注解是 returning属性就是用于指定接收方法返回值的变量名的。所有被注解为后置通知的方法,除

后置通知

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

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

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

方法定义要求

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

属性

  • value 切入点表达式
  • returning 自定义的变量,表示目标方法的返回值的
  • 自定义变量名必须和通知方法的形参名一样

位置:在方法定义的上面 

特点:

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

接口类

public interface someservice {
    string doother(string name);
    stdent doother2(string anme,int age);
}

接口实现类

@component("someserviceimpl")
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;
    }
}

增加业务功能类

@component("myaspect2")
@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);
    }

}

returning = "res"这个的变量res要和object res的res命名一样

主配置文件:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        <context:component-scan base-package="cn.com.ycy.spring_aspectj.bao02"/>
    <aop:aspectj-autoproxy/>
</beans>

测试类

    @test
    public void test02(){
        string config="applicationcontesxt1.xml";
        applicationcontext ac = new classpathxmlapplicationcontext(config);
        //从容器获取目标对象
        cn.com.ycy.spring_aspectj.bao02.someservice someservice = (cn.com.ycy.spring_aspectj.bao02.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);
        //从容器获取目标对象
        cn.com.ycy.spring_aspectj.bao02.someservice someservice = (cn.com.ycy.spring_aspectj.bao02.someservice) ac.getbean("someserviceimpl");
        //通过代理对象执行方法,实现目标方法执行时,增强了功能
        stdent str = someservice.doother2("ycy",24);
        system.out.println(str);
        //someservice
    }

注意:

在后置通知中也是可以使用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);
        }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com