spring post notification @ AfterReturning

Post notification

After the target method is executed, the added business function can get the return value of the target method. The annotation is the returning attribute, which is used to specify the variable name of the return value of the receiving method. In addition to the JoinPoint parameter, all methods annotated as post notification can also contain a variable for receiving the return value. The variable is preferably of type Object, and the return value of the target method can be of any type.
The post definition method is to realize the section function
Method definition requirements

  1. Public public method
  2. Method has no return value void
  3. Method name customization
  4. The method has parameters. It is recommended to use Object. The parameter name is customized. It is used to receive the return value of the target method
Use @ AfterReturning: it is the function added behind the business logic method

attribute

  1. value pointcut expression
  2. returning is a custom variable that represents the return value of the target method
  3. The custom variable name must be the same as the formal parameter name of the notification method
    Location: above the method definition
    characteristic:
        1 . Executed after the target method
        2. The return value of the target method can be obtained. Different processing operations can be performed according to the return value, and the return value can be modified
        3. This return value can be modified

Interface class:

public interface Someservice {
    String doOther(String name);
    stdent doOther2(String anme,int age);
}

Interface implementation class

@Component("SomeserviceImpl")
public class SomeserviceImpl implements Someservice {
    @Override
    public String doOther(String name) {
        System.out.println("------Target method execution doOther()-------");
        return name;
    }

    @Override
    public stdent doOther2(String name, int age) {
        System.out.println("------Target method execution doOther()-------");
        stdent st = new stdent();
        st.setAge(age);
        st.setName(name);
        return st;
    }
}

Add business function class

@Component("myAspect2")
@Aspect
public class MyaspectJ {
    @AfterReturning(value ="execution(* *..SomeserviceImpl.doOther(..))",returning = "res")
    public void myaspectJ(Object res){
        System.out.println("The return value of post notification is:"+res);
        res = "18204229-"+res;
        System.out.println("Modified:"+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("In post notification:"+res);
    }

}

returning = "res" the variable res should be named the same as the res of Object res

Master profile:

<?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 class

    @Test
    public void test02(){
        String config="ApplicationContesxt1.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //Get target object from container
        cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl");
        //Through the proxy object execution method, the function is enhanced when the target method is executed
        String str = someservice.doOther("ycy");
        System.out.println(str);
    }
    //Returns an object, whether it has changed
    @Test
    public void test03(){
        String config="ApplicationContesxt1.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //Get target object from container
        cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl");
        //Through the proxy object execution method, the function is enhanced when the target method is executed
        stdent str = someservice.doOther2("ycy",24);
        System.out.println(str);
        //someService
    }
be careful:

JoinPoint can also be used in post notification, and this must be put in the first place

@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);
        }
}

Tags: Spring

Posted by landysaccount on Mon, 23 May 2022 17:19:22 +0300