简体   繁体   English

如何将自定义值传递给spring aop建议?

[英]How can I pass a customized value to a spring aop advice?

If I take a service method named public void delete(int id); 如果我采用名为public void delete(int id);的服务方法public void delete(int id); as the pointcut, I want to add an after-returning advice on it, but I don't know what kind of object was deleted(however, the servlet which called the service knows the type value), so I was wondering if I can pass a customized value to this after-returning advice when it is activated, like 'user' . 作为切入点,我想在其上添加一个返回后的建议,但是我不知道删除了哪种对象(但是,调用该服务的servlet知道类型值),所以我想知道是否可以激活后,将自定义值传递给此返回后建议,例如'user' I've already checked the related document on Spring's website and I still know nothing. 我已经检查了Spring网站上的相关文档,但仍然一无所知。 I'd appreciate your answer, THX. 感谢您的回答,THX。

One solution but its required refactoring in Service method 一种解决方案,但需要在Service方法中进行重构

1) Refactoring 1)重构

public class DeleteRequest {
   String type;
   Long id;
}

public boolean delete(DeleteRequest request){ // impl}

2) Use Around Advice 2.1) Before proceeding method execution, read passed parameter & get to be deleted object for "XYZ" requirement. 2)使用建议2.1)在执行方法执行之前,读取传递的参数并获取满足“ XYZ”要求的对象。 2.2) Capture result of delete method execution IF its TRUE then DO your stuff 2.2)如果删除方法执行为TRUE,则捕获其执行结果,然后执行操作

Note: I used this approach for deleted entity notification. 注意:我使用这种方法来删除实体通知。 So how we can get deleted entity information in after-advice, hence keep it entity information in before phase & use it in after-successful execution. 因此,我们如何在事后建议中获取已删除的实体信息,从而将其保留在阶段之前的实体信息中,并在事后执行中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM