• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Cannot destroy singleton Spring bean and application context

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot terminate the JVM (e.g. by calling System.exit(1) for stand-alone) as such, any code written in registerShutdownHook will not get invoked.

On the contrary, if an exception condition is raised during the start-up of an application server, I want to dispose any Singleton bean and terminate the Spring container.

I tried using the following separately:

configurableListableBeanFactory.destroySingletons( );
abstractApplicationContext.destroy();
abstractApplicationContext.close();

None of them seemed to work.

The abstractApplicationContext (aplicationContext interface for that matter) was still NOT null. The number of beans alive was still the same, as was before calling destroySingletons(), destroy(), close() API.

My goal is to dispose the bean instances themselves.

No clean-up is necessary in any bean (e.g. by writing destroy-method in XML) as these beans do not hold reference to anything like datasource etc.

But I just want to remove all the initialized singleton Spring beans. The Spring beans are not lazy, they are all Singleton, do not have destroy-method in XML, neither do implement DisposeBean interface.

I was thinking that one of the following API would remove the beans from scope and will also make application context null. API referring to are:

configurableListableBeanFactory.destroySingletons( );
abstractApplicationContext.destroy();
abstractApplicationContext.close();

But it did not work.

What am I missing? My code is similar to:




After all three different calls, the number of beans that were there before is the same after individually calling the 3 different API. Both abstractApplicationContext and applicationContext are not null either.

I was hoping that abstractApplicationContext.close() will make abstractApplicationContext and/or applicationContext null and the beans will also get destroyed. As such, if the number of beans before calling any API is 10, the number of beans after calling close() and other API will be ZERO.

However, the number of beans stays the same (e.g. 10) after calling close, destroySingletons(), destroy(); stop() etc. methods. The applicationContext/abstractApplicationContext is not null either.
reply
    Bookmark Topic Watch Topic
  • New Topic