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