我读了这两篇文章,但它们似乎不起作用
Java Spring @Scheduled tasks executing twice
Spring @Scheduled is executing task twice when using annotations
我没有@Configurable类,并且我不使用applicationConetxt.xml
。
我的计划任务
@EnableScheduling
@Component
public class ScheduledTasks {
@Autowired
private ApplicationContext ctx;
@Scheduled(fixedRate=5000)
public void monitorRoom(){
System.out.println("--------------------");
}
}
我可以看到,当我在 tomcat 中运行应用程序时,任务会打印两次 evrey 5 秒。
有人说要删除
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
它有效,但我觉得这不是一个解决方案。因为这应该是需要的。(至少从谷歌的例子来看)
这是我的 web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>wodinow</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
调度程序-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="wodinow.weixin.jaskey" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/pages/**" location="/resources/pages/" />
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:annotation-driven/>
</beans>
最佳答案
删除上下文加载器监听器。仅当您必须初始化父上下文时才需要这样做。调度程序 servlet 中的上下文是独立的,但如果需要,可以访问此类父上下文,即检索服务或 dao。
关于java - Spring 4运行调度任务两次,没有applicationContext.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26779567/