I have an external component that is autowired using the AspectJ @Configurable weaving because I need one instance per session and its from an external framework.
I was interested in counting objects instances to ensure that spring was able to take on and then give up the object, so I put together the below code. As I create instances the instanceCount goes up, however when I destroy the bean (by setting to null) the instance count doesn't drop.
What's going on here? Is the garbage collector needed to clean up the object (and does this interact with the container!)? Or is there a specific way to unregister from Spring in order to run the @PreDestroy method?
Thanks,
David
Code:@Configurable public class MyBean extends ExternalFrameworkObject { @Autowired private Dependency dependency; private static int instanceCount = 0; private static final Object countLock = new Object(); @PostConstruct public synchronized void init() { synchronized (countLock) { System.out.println(++instanceCount + " instances on heap"); } } @PreDestroy public synchronized void destroy() { synchronized (countLock) { System.out.println(--instanceCount + " instances on heap"); } } }


Reply With Quote