Quartz在spring中配置,被调用两次的处理办法

  • Post author:
  • Post category:其他


本人项目中使用Quartz时,发现Quartz注册方法每次都会被连续调用两次,经一番google,网友普遍反映应该是spring被加载了两次,而各种修改方法实验一遍都不见其效。

都来发现可能是项目在web.xml中加载的启动监听器中调用了spring的IOC缘故。

由于listener并不能由spring接管,所以原先采用的是下面的方法(此方法导致了spring的双重加载):

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SystemInitService systemInitService=(SystemInitService) context.getBean("systemInitService");

参考上篇转载的文章《在Servlet(或者Filter,或者Listener)中使用spring的IOC容器》的方法,对程序进行修改:

由ServletContextListener的实现类获取ServletContext,并在调用方法中将ServletContext做参数传递过去,用此ServletContext获取spring中的配置bean采用以下方式:

WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);  
SystemInitService systemInitService= (SystemInitService) webApplicationContext.getBean("systemInitService");



版权声明:本文为cz_arel原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。