我正在使用Spring MVC 4.0和Hibernate 4.0.1。我设置@Scheduled(cron =" 0 21,22 17 * * *",每次调用任务两次。如果我运行独立应用程序,任务被调用一次,但如果我打电话它来自Web容器,taks被称为两次。 如果我从web.xml中删除了org.springframework.web.context.ContextLoaderListener,则Web应用程序未加载.HTTP状态500 - 未找到WebApplicationContext:没有注册ContextLoaderListener?
<bean id="demoServiceBasicUsageFixedDelay" class="com.mysource.batch.DemoServiceBasicUsageFixedDelay"></bean>
<task:annotation-driven/>
解决方案:
可能发生的事情是,使用@Scheduled注释的bean包含在根应用程序上下文和Web应用程序上下文中。
I separated @Scheduled related bean and prepared separate xml.THis xml is loaded from web.xml.
Servlet-context.xml
----------------------
<bean id="demoServiceBasicUsageFixedDelay" class="com.bics.batch.DemoServiceBasicUsageFixedDelay"></bean>
<task:scheduled-tasks>
<task:scheduled ref="demoServiceBasicUsageFixedDelay" method="demoServiceMethod" cron="0 35,36 12 * * *"></task:scheduled>
</task:scheduled-tasks>
Web.xml:
-------
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,
/WEB-INF/spring-security-context.xml,
/WEB-INF/servlet-context.xml</param-value>
</context-param>
Now Task is called one time and more over i am able to run web application also.