簡體   English   中英

Spring 4運行調度任務兩次,沒有applicationContext.xml

[英]Spring 4 runs schedule task twice, no applicationContext.xml

我讀過這兩本書,但它們似乎沒有用

Java Spring @Scheduled任務執行兩次

Spring @Scheduled使用注釋時執行兩次任務

我沒有@Configurable類,也沒有使用applicationConetxt.xml

我的ScheduleTasks

@EnableScheduling
@Component
public class ScheduledTasks {
    @Autowired
    private ApplicationContext ctx;

    @Scheduled(fixedRate=5000)
    public void monitorRoom(){
        System.out.println("--------------------");
    }
}

我可以看到,當我在tomcat中運行該應用程序時,該任務每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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM