测试
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>
package org.coderitl;
public interface SomeService {
public Student doSome(String name, int age);
}
package org.coderitl.impl;
import org.coderitl.SomeService;
import org.coderitl.Student;
import org.springframework.stereotype.Component;
@Component("SomeServiceImplAfterRuturning")
public class SomeServiceImpl implements SomeService {
@Override
public Student doSome(String name, int age) {
Student student = new Student(name, age);
System.out.println("SomeServiceImpl dome: " + student);
return student;
}
}
package org.coderitl;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component("myAspectAfter")
@Aspect
public class MyAfterRuturning {
@AfterReturning(value = "execution(* *..SomeServiceImpl.*(..))", returning = "result")
public void myAfterRuturning(Object result) {
System.out.println("后置通知执行时student值: " + result);
if (result != null) {
result = new Student("address", 80);
System.out.println("修改 result 地址指向: " + result);
Student student = (Student) result;
System.out.println("后置通知中修改前: " + student);
student.setName("fixName");
student.setAge(19);
System.out.println("后置通知中修改后: " + student);
}
}
}
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="org.coderitl"/>
<aop:aspectj-autoproxy/>
</beans>
输出
输出 |
---|
|
执行流程分析
执行流程分析 |
---|
|
结论: 影响