Spring 的简单scheduler例子
创建scheduler代码
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.proliu.scheduler;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
@Component public class SimpleScheduler {
@Scheduled(cron="0/5 * * * * ?") public void excute(){ System.out.println("Spring Scheduler"); } }
|
在xml里面配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<task:annotation-driven/>
<context:annotation-config/> <context:component-scan base-package="com.proliu.scheduler"/>
</beans>
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.proliu.test.scheduler;
import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
@RunWith(BlockJUnit4ClassRunner.class) public class SimpleSchedulerTest{
@Test public void test1(){ String springConfig = "classpath*:spring/Spring-Scheduler.xml" ; ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
}
}
|
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE