Spring定时任务未能运行

3

我是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配置中进行了定义:

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

spring-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框架。我可能遗漏了什么吗?


请您能否发布完整的XML配置文件,因为Amudhan和Dave Syer是正确的,您需要添加<task:annotation-driven />。另一方面,您在Amudhan的评论中写道,这会导致异常。因此,您的XML文件肯定有很大问题,请发布包括“头部”的完整XML文件。 - Ralph
1
您似乎正在加载相同的XML文件两次。如果您删除ContextLoaderListener,一次就足够了。 - Dave Syer
2个回答

5

您是否已经 @EnableScheduling (或者使用XML等效方式)?请参考此指南获取更多详细信息。


如果我尝试添加:<task:annotation-driven />,我会收到一个异常,因为在XML中写的是<context:annotation-config />。如果我删除<task:annotation-drive />,我就不会收到错误。 - xsiraul
那不太有意义。你能展示一下异常吗?顺便说一句,没有必要对多个答案以相同的方式进行回复(即使它们是相同的)- 我们都可以阅读所有的评论。 - Dave Syer

3

您需要在您的xml命名空间和模式位置中添加一些内容。请参见此指南


如果我尝试添加:<task:annotation-driven />,我会收到一个异常,因为在XML中写的是<context:annotation-config />。如果我删除<task:annotation-drive />,我就不会收到错误。 - xsiraul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接