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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM