springboot的Scheduled定时器不工作

问题情况

使用springboot,使用注解方式启动定时器进行业务调度。

在入口类中加了注解如下:

package org.test.xyz;

@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = {"org.test.abc"})
public class Test
{
    public static void main(String[] args)
    {
        SpringApplication.run(Test.class, args);
    }
}

 

定时器类如下:

package org.test.xyz;

@Component
public class Timer
{
    @Scheduled(fixedRate = 5000)
    public void test()
    {
        System.out.println("Time " + new Date().toString());
    }
}

 

springboot启动后,并没有按照预期结果打印:Time xxx 的日志。

 

分析结果

因为在入口类中使用了@ComponentScan注解,并且指定了basePackages,所以程序只会扫描指定的包路径下的Component类,其他位置的Component类不会进行扫描,从而不会启动定时器。

参考链接:https://blog.csdn.net/u014695188/article/details/52263903

 

posted @ 2018-08-31 17:46  Mister_W  阅读(8459)  评论(1编辑  收藏  举报