2014-02-27 143 views
2

我是Spring框架的新手。目前我嘗試整合cron job服務。春季cron工作不工作

予定義的服務類如:

<context:component-scan base-package="com.test.cron" /> 

彈簧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.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 

<context:annotation-config /> 

<context:component-scan base-package="com.test.cron" /> 
<task:annotation-driven /> 
</beans> 

web.xml中:

package com.test.cron; 
@Service 
public class CronJob { 
    protected static final Logger logger = Logger.getLogger(PasswordRemindFlusher.class); 

    @Scheduled(cron="0 0/2 * * * ?") 
    public void demoServiceMethod() 
    { 

     logger.debug("Cron job started."); 
    } 
} 

然後我在servlet配置定義

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    version="2.5"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

例外:

SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener 
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext 
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172) 

在同一個包工程的所有其他控制器。 我使用Spring 3.1.2框架。我可以錯過什麼嗎?

+0

能否請您發佈完整的xml配置文件,因爲Amudhan和Dave Syer是可靠的,您需要添加''。另一方面,你寫了(在Amudhan的評論中)這導致了一個例外。所以,你的xml文件必須有錯誤,因此請發佈完整的xml文件,包括「標題」。 – Ralph

+1

您似乎要加載兩次相同的XML文件。一旦應該做(如果你刪除了ContextLoaderListener)。 –

回答

2

您將需要在xml名稱空間和模式位置中添加少量內容。請參閱this指南。

+0

如果我嘗試添加:,則會收到一個異常,因爲在XML中寫入。如果我刪除,我不會收到錯誤。 – xsiraul

2

您是否@EnableScheduling(或Xml等價物)?有關更多詳細信息,請參見this guide

+0

如果我嘗試添加:,則會收到一個異常,因爲在XML中寫入。如果我刪除,我不會收到錯誤。 – xsiraul

+0

這沒有多大意義。你能顯示異常嗎?順便說一句,沒有必要以同樣的方式回覆多個答案(即使它們是相同的) - 我們都可以閱讀所有評論。 –