Spring定时任务

    用过其他几个定时器,最近发现spring定时器超级好用

    1、首先在xml文件头中添加

         xmlns:task="http://www.springframework.org/schema/task"

         xsi:schemaLocation="http://www.springframework.org/schema/task/spring-task-3.2.xsd"

    2、在xml文件中添加

        <task:executor id="executor" pool-size="5" />    
        <task:scheduler id="scheduler" pool-size="10" />    
        <task:annotation-driven executor="executor" scheduler="scheduler" />     

    3、在执行类中通过注释@Scheduled(cron="0 0/5 * * * ?")就会被调度

  

    在定时多任务的时可以通过配置文件定义

    @PropertySource("classpath:activity.properties")

    @Scheduled(cron ="${taks.job}")

    并在activity.properties中添加 taks.job ="0 0/5 * * * ?"



需要注意 <context:component-scan base-package="*" /> 必须在spring扫描的包内,否则无法进行调度。