1、基于xml的配置方式
<task:scheduled-tasks>
<task:scheduled ref="wxTokenService" method="updateToken" cron="0/5 * * * * ?"/> //每隔5秒执行一次
</task:scheduled-tasks>
2、基于注解
1、在applicationContext.xml中
<!-- 开启定时任务的自动扫描 -->
<
task:annotation-driven
/>
2、
@Service //把这个加入到spring容器中
public
class
WxTokenService {
@Autowired
WxTokenMapper
tokenMapper
;
//初始化时执行 然后每隔7200秒执行一次
@Scheduled
(fixedRate=1000*60*60*2)
public
void
updateToken() {
String
token
=TokenUtils.
getToken
();
WxToken
wxToken
=
new
WxToken();
wxToken
.setId(1);
wxToken
.setToken(
token
);
tokenMapper
.updateByPrimaryKeySelective(
wxToken
);
}
}