使用过spring整合的的quzrtz,虽然配置起来已经比较清晰容易,但还是避免不了有很多的xml文件中的配置,现在spring支持基于注解的quzrtz配置,可以很方便的使用,也免去了xml中冗余的配置。
增加quartz配置文件并建立命名空间:
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.2.xsd
其他的按照需要增加。
XML文件配置:
<task:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.lfex.quzrtz"/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
创建测试类测试:
@Component
public class QuartzTest {
@Scheduled(fixedDelay = 1000)
public void test(){
System.out.println("定时任务开始执行");
}
}
将quartz配置文件加入到web.xml文件中:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-context.xml,classpath:/quartz-config.xml</param-value>
</context-param>