首页 > 代码库 > 使用spring的@Scheduled注解执行定时任务,启动项目不输出警告

使用spring的@Scheduled注解执行定时任务,启动项目不输出警告

在applicationContext.xml中添加:

xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.0.xsd"><task:annotation-driven executor="myExecutor" scheduler="myScheduler" /><task:executor id="myExecutor" pool-size="5" /><task:scheduler id="myScheduler" pool-size="10" />

 

java代码:

@Componentpublic class CleanExpireTokenTask {    private Logger logger = LoggerFactory.getLogger(LogTag.BUSINESS);        @Scheduled(cron = "0 * * * * ?")    public void startUpdateSaleThread(){        try{            System.out.println("check token expire");        }catch(Exception e){            logger.error("Make salesReport faild",e);        }    }}

 

注意:

实现类上要加注解@Component

定时器的任务方法不能有返回值

配置及启动报错问题参考自
http://stackoverflow.com/questions/30431776/using-scheduled-and-enablescheduling-but-gives-nosuchbeandefinitionexception
http://www.cnblogs.com/luwinner/p/5109327.html
 

使用spring的@Scheduled注解执行定时任务,启动项目不输出警告