Jun 3rd, 2011, 12:08 PM
An aspect to introduce common @PostConstruct code
I have a bit of @PostConstruct code that I want in many, many spring objects.
Is it possible to use an aspect to automatically introduce this code on any spring bean with a particular annotation (say @MyComponent) ?
- If a spring bean has @MyComponent annotation
- It should get the following method and code automatically injected:
public void myComponentStart()
// common code here
- Spring should then instantiate the bean itself as normal, but it should see this @PostConstruct and execute it as you would expect it to had the original code been in this class all along
Is this possible?
Jun 4th, 2011, 12:33 PM
You have to use either loadtime or compiletimeweaving. If you want to same code in a lot of other beans why not simply make an abstract super class and have all the other classes extend that... I would first try to solve it without aspects (basically trying to keep it simple) instead of with aspects, adds a lot of complexity to your application.
Tags for this Thread