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

来源:互联网 发布:关键词优化 按天 编辑:程序博客网 时间:2024/06/07 01:42

本人项目中使用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");


原创粉丝点击