roger
roger

Reputation: 269

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 ?

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 ?

Upvotes: 0

Views: 295

Answers (1)

kriegaex
kriegaex

Reputation: 67457

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.

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.

Upvotes: 0

Related Questions