Spring Task提供两种方式进行配置还是一种是annotation(标注),另外一种就是XML配置。
第一种:注解方式
1.配置文件头部:
xmlns:task="http://www.springframework.org/schema/task"
2.xsi:schemaLocation添加:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
3.再加上前几节说道的 注解扫描的配置
<context:component-scan base-package="com.gl">
</context:component-scan>
4.最后加上
<task:annotation-driven />
搞定
spring task还有很多参数,参考xsd文档 http://www.springframework.org/schema/task/spring-task-4.0.xsd
第一种:注解方式
1.配置文件头部:
xmlns:task="http://www.springframework.org/schema/task"
2.xsi:schemaLocation添加:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
3.再加上前几节说道的 注解扫描的配置
<context:component-scan base-package="com.gl">
</context:component-scan>
4.最后加上
<task:annotation-driven />
搞定
spring task还有很多参数,参考xsd文档 http://www.springframework.org/schema/task/spring-task-4.0.xsd
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:task="http://www.springframework.org/schema/task"
- xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
- 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-4.1.xsd
- http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"> - <!-- 配置自动扫描的包 -->
- <context:component-scan base-package="com.gl">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
- <!-- task-->
- <task:annotation-driven />
- </beans>
5.任务类
@Component
@Lazy(value=false)
public class Test throws Exception {
@Scheduled(cron = "*/5 * * * * ?")//每隔5秒执行一次具体执行间隔请百度
public void test(){
System.out.println("this is task test");
}
第二种:配置文件方式
1:配置
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:task="http://www.springframework.org/schema/task"
- xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
- 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-4.1.xsd
- http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"> - <!-- 配置自动扫描的包 -->
- <context:component-scan base-package="com.gl">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- <task:scheduler id="scheduler" pool-size="5" />
- <task:scheduled-tasks scheduler="scheduler">
- <task:scheduled ref="Test" method="test" cron="0 * * * * ?"/>
- </task:scheduled-tasks>
- </beans>
ref : 定时任务类名 method:执行的方法名
2.测试类
@Service
public
class
Test {
public
void
test
() {
System.out.println(“this is task test”);
}
}