在春季 AOP 中使用@AfterReturning修改类的值



如何使用@AfterReturning建议修改值,它适用于除字符串以外的任何对象。我知道字符串是不可变的。以及如何在不更改 AccountDAO 类中 saveEverything(( 函数的返回类型的情况下修改字符串? 以下是代码片段:

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

来自文档:返回建议后

请注意,无法退回完全不同的 返回建议后使用时的参考。

正如anavaras lamurep在评论中正确指出的那样,@Around建议可以用来满足您的要求。一个示例方面如下

@Aspect
@Component
public class ExampleAspect {
@Around("execution(* com.package..*.save*()) && within(com.package..*)")
public String around(ProceedingJoinPoint pjp) throws Throwable {
String rtnValue = null;
try {
// get the return value;
rtnValue = (String) pjp.proceed();
} catch(Exception e) {
// log or re-throw the exception 
}
// modify the return value
rtnValue = "0";
return rtnValue;
}
}

请注意,问题中给出的切入点表达式是全局的。此表达式将匹配对任何以save开头并返回Object的 Spring Bean 方法的调用。这可能会产生意想不到的结果。建议将课程的范围限制为建议。

---更新---

正如@kriegaex所指出的,为了更好的可读性和可维护性,切入点表达式可以重写为

execution(* com.package..*.save*())

execution(* save*()) && within(com.package..*)

最新更新