我是 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.");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在servlet配置中定义:
<context:component-scan base-package="com.test.cron" />
Run Code Online (Sandbox Code Playgroud)
弹簧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>
Run Code Online (Sandbox Code Playgroud)
网页.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> …Run Code Online (Sandbox Code Playgroud)