在Spring AOP中使用@AfterReturning修改类中的值 [英] Modify value from class with @AfterReturning in Spring AOP
本文介绍了在Spring AOP中使用@AfterReturning修改类中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用@AfterReturning建议修改值,它适用于除String之外的任何对象.我知道String是不可变的.以及如何在不更改AccountDAO类中的saveEverything()函数的返回类型的情况下修改字符串?这是代码段:
How to modify value with @AfterReturning advice, it works for any object except String. I know that String is Immutability. and how to modify the string without changing returning type of saveEverything() function in AccountDAO class? here are code snippet:
@Component
public class AccountDAO {
public String saveEverything(){
String save = "save";
return save;
}
}
和方面:
@Aspect
@Component
public class AfterAdviceAspect {
@AfterReturning(pointcut = "execution(* *.save*())", returning = "save")
public void afterReturn(JoinPoint joinPoint, Object save){
save = "0";
System.out.println("Done");
}
}
和主应用程序:
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(JavaConfiguration.class);
AccountDAO accountDAO = context.getBean("accountDAO", AccountDAO.class);
System.out.println(">"+accountDAO.saveEverything());;
context.close();
}
}
推荐答案
查看全文