<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[SpringBoot @Scheduled 从配置文件获取cron值]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Fri, 06 Aug 2021 14:53:56 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	今天有个同事说要我帮他看下怎么把这个定时任务的cron表达式放到配置文件里面去，<br/>1、在类上加注解@Component，交给Spring管理<br/>2、在@PropertySource指定配置文件名称，如下配置，指定放在src/main/resource目录下的application.yml文件<br/>3、配置文件application.yml参考内容如下<br/>application.yml<br/><br/>jobs:<br/>&nbsp;&nbsp;customDictUpdateTime:<br/>&nbsp;&nbsp;&nbsp;&nbsp;corn: "0/5 * * * * ?"<br/><br/><br/>@Configuration<br/>@Component<br/>@Slf4j<br/>@PropertySource(value = "classpath:application.yml")<br/>public class QuartzTask &#123;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;@Autowired<br/>&nbsp;&nbsp;&nbsp;&nbsp;private RedisUtil redisUtil;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;private static boolean startTask = true;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;@PostConstruct<br/>&nbsp;&nbsp;&nbsp;&nbsp;public void startInit() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;excuteBusiness();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;//定时任务初始化政务宝自定义词库<br/>&nbsp;&nbsp; //如果获取不到, 取冒号后面的默认值<br/>&nbsp;&nbsp;&nbsp;&nbsp;@Scheduled(cron = "$&#123;jobs.customDictUpdateTime.corn:0/5 * * * * ?&#125;")<br/>&nbsp;&nbsp;&nbsp;&nbsp;public void excuteTask() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;excuteBusiness();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>｝<br/><br/>上面说的是yml配置文件，有的同学就说了，那么我用的application.properties文件配置呢？<br/><br/>application.properties<br/><br/>jobs.customDictUpdateTime.corn=0/5 * * * *<br/><br/>import org.springframework.context.annotation.PropertySource;<br/>import org.springframework.scheduling.annotation.Scheduled;<br/>import org.springframework.stereotype.Component;<br/><br/><br/>@Configuration<br/>@Component<br/>@Slf4j<br/>@PropertySource(value = "classpath:application.properties")<br/>public class QuartzTask &#123;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;@Autowired<br/>&nbsp;&nbsp;&nbsp;&nbsp;private RedisUtil redisUtil;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;private static boolean startTask = true;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;@PostConstruct<br/>&nbsp;&nbsp;&nbsp;&nbsp;public void startInit() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;excuteBusiness();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;//定时任务初始化政务宝自定义词库<br/>&nbsp;&nbsp; //如果获取不到, 取冒号后面的默认值<br/>&nbsp;&nbsp;&nbsp;&nbsp;@Scheduled(cron = "$&#123;jobs.customDictUpdateTime.corn:0/5 * * * * ?&#125;")<br/>&nbsp;&nbsp;&nbsp;&nbsp;public void excuteTask() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;excuteBusiness();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>｝<br/><br/>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] SpringBoot @Scheduled 从配置文件获取cron值]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>