写定时任务的参考代码
2017-11-23 13:40
155 查看
值得注意的是使用el表达式的时候一定要先注册类PropertySourcesPlaceholderConfigurer:可以用@Bean注册或者用XML的<bean>标签
这里贴上源码:
package task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Create by szw on 2017/11/23 10:05
*/
@Component
@Lazy(false)
@PropertySource(value = "classpath:task.properties")
public class SpringTask {
@Autowired
private Environment environment;
@Value(value = "${demo.url}")
private String name;
@Scheduled(cron = "${corn}")
public void task() {
String corn = environment.getProperty("demo.url");
System.out.println("我的名字" + name);
System.out.println("我执行了:" + corn);
System.out.println("执行");
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
这里贴上源码:
package task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Create by szw on 2017/11/23 10:05
*/
@Component
@Lazy(false)
@PropertySource(value = "classpath:task.properties")
public class SpringTask {
@Autowired
private Environment environment;
@Value(value = "${demo.url}")
private String name;
@Scheduled(cron = "${corn}")
public void task() {
String corn = environment.getProperty("demo.url");
System.out.println("我的名字" + name);
System.out.println("我执行了:" + corn);
System.out.println("执行");
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
相关文章推荐
- quarta使用代码控制定时任务的启停
- ScheduledExecutorService任务定时代码示例
- 嵌入式启动定时任务代码
- 多线程_定时任务的多次执行代码体现
- springBoot定时任务处理类的实现代码
- spring框架整合使用定时任务框架java quartz的示例代码配置
- 纯PHP代码实现类似计划任务的无浏览器定时
- Windows任务计划定时执行PHP代码
- spring框架整合使用定时任务框架java quartz的示例代码配置
- PHP计划任务、定时执行任务的实现代码
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码
- 调用定时任务的配置和代码
- Spring 定时任务quartz配置及代码示例详解
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码
- 多线程-定时任务的多次执行代码体现
- Laravel实现定时任务的示例代码
- Windows任务计划定时执行PHP代码及扩展
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码
- spring框架整合使用定时任务框架java quartz的示例代码配置