春天的cron工作不起作用

时间:2014-02-27 06:45:06

标签: java spring spring-mvc cron

我是Spring框架的新手。目前我尝试整合cron作业服务。

我将服务类定义为:

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 config中定义了:

<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:

<?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框架。我可能错过了什么?

2 个答案:

答案 0 :(得分:4)

你有@EnableScheduling(或Xml等价物)吗?有关详细信息,请参阅this guide

答案 1 :(得分:2)

您需要在xml命名空间和架构位置添加一些内容。请参阅this指南。