- 
        Couldn't load subscription status. 
- Fork 38.8k
Description
Michael Rippon opened SPR-10013 and commented
SpringBeanAutowiringInterceptor by default uses SingletonBeanFactoryLocator, which keeps an internal counter of references to the SpringContext. This is incremented on @PostConstruct, and decremented on @PreDestroy, however, according to the EJB spec:
The following scenarios result in the PreDestroy lifecycle callback interceptor method(s) not being called for an instance:
...
• A system exception thrown from the instance’s method to the container.
This means that if an exception is thrown during EJB initialisation, the @PreDestroy method is never called, the reference count in SingletonBeanFactoryLocator is never decremented, and the application context is therefore never closed.
This has implications (for example), with the MBean exporter, where MBeans are never unregistered, and the next application deployment fails as the previous MBean instances already exist.
I suggest that in the case where an exception is thrown during the @PostConstruct phase, the beanFactoryReference should be released. Ie:
doAutowireBean(invocationContext.getTarget());
try {
invocationContext.proceed();
}
catch {
...
doReleaseBean(invocationContext.getTarget());
}
Affects: 3.1 GA
Referenced from: commits eb492ed, 721fa9d
Backported to: 3.1.4