简体   繁体   English

AspectJ关于销毁春豆的切入点

[英]Aspectj pointcut on spring bean destruction

Is it possible to execute an AspectJ advice in Java that executes when the target object goes out of scope or gets destroyed ? 是否可以在Java中执行在目标对象超出范围或被销毁时执行的AspectJ建议?

Suppose we have a class which have various methods that are being matched by a pointcut expression and a caching aspect has been implemented such that all method calls' responses have been cached. 假设我们有一个类,该类具有与切入点表达式匹配的各种方法,并且已实现了缓存方面,以便所有方法调用的响应都已缓存。 The cache key is the concatenation of the proxy object and the method signature and arguments. 缓存键是代理对象以及方法签名和参数的串联。 We would like to invalidate the cache when the target object is destroyed. 当目标对象被破坏时,我们想使缓存无效。

Can anyone share details on how we can execute an advice when this object is destroyed ? 任何人都可以分享有关在销毁该对象时如何执行建议的详细信息吗?

Probably you are referring to your caching aspect here . 可能您在这里指的是缓存方面。

You could keep references not just to strings representing method calls but to the actual objects upon which the methods to be cached are called. 您不仅可以保留对表示方法调用的字符串的引用,还可以保留对要在其上缓存方法的实际对象的引用。 The important part here is that you have to use weak references so as not to keep the corresponding objects to go out of scope. 这里的重要部分是您必须使用弱引用,以免使相应的对象超出范围。 You could collect all objects in a WeakHashMap and regularly run a clean-up job taking care of removing non-existent objects from you actual cache. 您可以收集WeakHashMap所有对象并定期运行清理作业,以从实际缓存中删除不存在的对象。

Update: I am not a Spring user, but maybe there also are lifecycle methods/hooks in Spring which can be fired when a component goes out of scope or otherwise ends its lifecycle. 更新:我不是Spring用户,但是Spring中可能还有生命周期方法/挂钩,当组件超出范围或以其他方式终止其生命周期时,可以触发这些生命周期方法/挂钩。

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

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