2013-10-31 77 views
2

我需要從我的Class實現Runnable正在打開我的Java EE應用程序7 Spring MVC。我宣佈我的課在servlet.xml如何啓動Runnable?

<beans:bean class="MyClass" scope="singleton"> 
</beans:bean> 
<annotation-driven /> 
<context:component-scan base-package="MyClass" /> 
<context:annotation-config /> 

,這是我的類,它有一個run()方法是從所有的X分鐘,但它不工作。爲什麼?

@Scope("singleton") 
@Component 
public class MyClass implements Runnable { 
    private static final Logger LOG = LoggerFactory.getLogger(myClass.class); 

    @Scheduled(initialDelay = 1000, fixedDelay = 10000) 
    @Override 
    @Transactional 
    public void run() { 
     LOG.debug("IN"); 
    } 
} 

我servlet.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans 
xmlns="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:task="http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd" 
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 

<context:annotation-config /> 
<context:component-scan base-package="com" /> 

<jpa:repositories base-package="com.dao" /> 

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources 
    directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

<beans:bean class="com.MyClass" scope="singleton"> 
</beans:bean> 

回答

2

添加<task:annotation-driven />servlet.xml

<annotation-driven />不夠

詳細的文檔是在這裏 http://docs.spring.io/spring/docs/3.0.x/reference/scheduling.html

不要忘記 的xmlns

<bean .... 
xmlns:task="http://www.springframework.org/schema/task" 
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" 

例如:更換你的XML頭

<beans:beans 
     xmlns="http://www.springframework.org/schema/mvc" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:task="http://www.springframework.org/schema/task" 
     xmlns:beans="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xsi:schemaLocation="http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd 
    "> 
+0

我已經有一半了 MaximeF

+0

@MaximeF粘貼你的xml plz,'task:annotation-driven'不是別的xx:註釋驅動 – farmer1992

+0

在這一行找到多個註釋: \t - cvc-complex-type.2.4.c:匹配的通配符是嚴格的,但是對元素的任務沒有聲明:annotation-driven' 。 \t - 無法找到Spring命名空間處理程序的元素'任務:模式命名空間的註釋驅動''http://www.springframework.org/schema/task http://www.springframework.org/schema/task http:///www.springframework.org/schema/task/ \t spring-task-3.0.xsd' – MaximeF

0

爲豆MyClass的添加全包ID = 「MyClass的」 類=「your.package.name.MyClass 」。並且所有這些註釋都需要被掃描以便被檢測到並嘗試添加@Component(「myClass」)