一、default-lazy-init="true"
1.配置文件
applicationContext.xml
新增:xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
default-lazy-init="true"
<!--开启这个配置,spring才能识别@Scheduled注解-->
<task:annotation-driven/>
<!--配置自动扫描的包-->
<context:component-scan base-package="com.task" use-default-filters="false">
2.task.java
package com.task.test;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
@Lazy(false)
public class test {
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void sys(){
System.out.println("测试定时任务");
}
}
3.效果
二、default-lazy-init="false"
1.配置文件
applicationContext.xml
新增:xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
default-lazy-init="false"
<!-- 定时器开关,配置定时任务的线程池 -->
<task:annotation-driven scheduler="myScheduler" mode="proxy" />
<!-- 配置定时任务的线程池 -->
<task:scheduler id="myScheduler" pool-size="10"/>
<!--配置自动扫描的包-->
<context:component-scan base-package="com.task" use-default-filters="false">
2.task.java
package com.task.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
@EnableScheduling
public class test{
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void sys(){
System.out.println("测试定时任务");
}
}
3.结果