@AfterReturning流程分析

目标方法

  1. public String doSome(String name,int age){
  2. System.out.println("doSome()方法执行了");
  3. return "abcd";
  4. }

后置通知的方法

public void myAfterReturning(Object obj/*目标方法的返回值*/){
    //??切面可以改变目标方法的返回值
    obj.toUpperCase();//"ABCD"
}

测试方法

String s = doSome("张三",22);
  • 如果目标方法的返回值是8种基本数据类型或String的类型则不可改变
  • 如果目标方法的返回值是引用类型,则可以改变

后置功能基本功能实现

业务

package com.chentianyu.s02;

/**
 *
 */
public interface SomeService {
    String doSome(String name,Integer age);
}

业务实现类

package com.chentianyu.s02;

import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class SomeServiceImpl implements SomeService {
    @Override
    public String doSome(String name, Integer age) {
        System.out.println("doSome方法被执行了...");
        return "abcd";
    }
}

切面类

package com.chentianyu.s02;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 *
 */
@Aspect //交给Aspect框架,扫描类去处理
@Component //交给Spring容器去创建对象
public class MyAspect {
    /**
     * 后置通知的方法的规范
     *  1.访问权限是public
     *  2.当前的方法没有返回值
     *  3.方法名称自定义
     *  4.方法有参数(也可以没有参数,如果目标方法没有返回值,则可以写无参的方法,一般会写有参,这样可以处理无参可以处理有参),这个切面方法的参数就是目标方法的返回值
     *  5.使用@AfterReturning注解表明是后置通知
     *      value: 指定切入点表达式
     *      returning: 指定目标方法返回值的名称,此名称必须与切面方法的参数名称一致
     */
    @AfterReturning(value = "execution(* com.chentianyu.s02.*.*(..))",returning = "obj")
    public void myAfterReturning(Object obj){
        System.out.println("后置通知功能实现....");
    }
}

绑定和扫描包applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <aop:aspectj-autoproxy />
    <context:component-scan base-package="com.chentianyu.s02" />
</beans>

测试

package com.chentianyu;

import com.chentianyu.s02.SomeService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {
    @Test
    public void test01(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("s02/applicationContext.xml");
        //取出代理对象
        SomeService someService = (SomeService) applicationContext.getBean("someServiceImpl");
        String s = someService.doSome("张三",22);
        System.out.println("在测试方法中的目标方法返回值:" + s);
    }
}

得到的结果为

doSome方法被执行了...
后置通知功能实现....
在测试方法中的目标方法返回值:abcd

后置通知中无法改变目标方法返回值示例

后置切面方法要带参数,是因为可以判断目标方法返回值如果是空或者是没有返回值就可以判断是否有返回值

package com.chentianyu.s02;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 *
 */
@Aspect //交给Aspect框架,扫描类去处理
@Component //交给Spring容器去创建对象
public class MyAspect {
    /**
     * 后置通知的方法的规范
     *  1.访问权限是public
     *  2.当前的方法没有返回值
     *  3.方法名称自定义
     *  4.方法有参数(也可以没有参数,如果目标方法没有返回值,则可以写无参的方法,一般会写有参,这样可以处理无参可以处理有参),这个切面方法的参数就是目标方法的返回值
     *  5.使用@AfterReturning注解表明是后置通知
     *      value: 指定切入点表达式
     *      returning: 指定目标方法返回值的名称,此名称必须与切面方法的参数名称一致
     */
    @AfterReturning(value = "execution(* com.chentianyu.s02.*.*(..))",returning = "obj")
    public void myAfterReturning(Object obj){
        System.out.println("后置通知功能实现....");
        if(obj != null){
            if(obj instanceof String){//判断返回值类型
                obj = obj.toString().toUpperCase();
                System.out.println("切面方法的返回值是String类型:"+ obj);
            }
        }
    }
}

转成大写了,如果当我测试类中的s测试的输出小写,就说明我们的转大写没用

doSome方法被执行了...
后置通知功能实现....
切面方法的返回值是String类型:ABCD
在测试方法中的目标方法返回值:abcd

后置通知中可改变目标方法返回值示例

package com.chentianyu.s02;

/**
 *
 */
public class Student {
    private String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
package com.chentianyu.s02;

/**
 *
 */
public interface SomeService {
    String doSome(String name,Integer age);
    //添加的接口
    Student change();
}
package com.chentianyu.s02;

import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class SomeServiceImpl implements SomeService {
    @Override
    public String doSome(String name, Integer age) {
        System.out.println("doSome方法被执行了...");
        return "abcd";
    }

    //添加的方法
    @Override
    public Student change() {
        System.out.println("change()方法被执行...");
        Student student = new Student("张三");
        return student;
    }

}
package com.chentianyu.s02;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 *
 */
@Aspect //交给Aspect框架,扫描类去处理
@Component //交给Spring容器去创建对象
public class MyAspect {
    /**
     * 后置通知的方法的规范
     *  1.访问权限是public
     *  2.当前的方法没有返回值
     *  3.方法名称自定义
     *  4.方法有参数(也可以没有参数,如果目标方法没有返回值,则可以写无参的方法,一般会写有参,这样可以处理无参可以处理有参),这个切面方法的参数就是目标方法的返回值
     *  5.使用@AfterReturning注解表明是后置通知
     *      value: 指定切入点表达式
     *      returning: 指定目标方法返回值的名称,此名称必须与切面方法的参数名称一致
     */
    @AfterReturning(value = "execution(* com.chentianyu.s02.*.*(..))",returning = "obj")
    public void myAfterReturning(Object obj){
        System.out.println("后置通知功能实现....");
        if(obj != null){
            if(obj instanceof String){//判断返回值类型
                obj = obj.toString().toUpperCase();
                System.out.println("切面方法的返回值是String类型:"+ obj);
            }
            if(obj instanceof Student){
                Student student = (Student) obj;
                student.setName("李四");//改变目标方法的返回值
                System.out.println("在切面方法中目标方法的返回值:" + student);
            }
        }
    }
}

测试

@Test
public void test02(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("s02/applicationContext.xml");
    SomeService someService = (SomeService) applicationContext.getBean("someServiceImpl");
    Student student = someService.change();
    System.out.println("在测试方法中的目标方法返回值:" + student);
}

返回的结果是

change()方法被执行...
后置通知功能实现....
在切面方法中目标方法的返回值:Student{name='李四'}
在测试方法中的目标方法返回值:Student{name='李四'}

总结

根据上述代码的结果来看: 如果目标方法的返回值是8种基本数据类型或String的类型则不可改变;如果目标方法的返回值是引用类型,则可以改变