I am trying to start and stop a quartz scheduler in a spring singleton bean .But postconstruct is getting called two times and predestroy is not called at all . This link says that because of proxying it natural to be called twice but this is causing exception in the postconstruct method . I only want the postConstruct to be called once after the singleton bean is loaded.
2 Answers
0
votes
why don't you try this using an init-method or try implementing the initializing bean . These provide alternative approaches to postConstruct .
When the spring beans are unloaded i.e , when the container is shut down or the close() method in ConfigurableApplicationContext method gets called through some other means, the preDestroy is invoked.
0
votes
I write a contextloader listener and change web.xml listener .So I can initialize bean only one times.
<listener>
<listener-class>
CustomContextLoaderListener
</listener-class>
</listener>
public class CustomContextLoaderListener extends
org.springframework.web.context.ContextLoaderListener{
Scheduler scheduler;
@Override
public void contextInitialized(javax.servlet.ServletContextEvent event) {
try{
super.contextInitialized(event);
this.scheduler= WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()).getBean(Scheduler.class);
}
@Override
public void contextDestroyed(ServletContextEvent event){
super.contextDestroyed(event);
scheduler.stopSchedulers();
}