1、确认@scheduled方法所在的类是否在application启动类同级目录或者同级目录之下(保证扫包可以扫到),如果不在同级目录的话需要在启动类上加入注解@ComponentScan来扫包如:
@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = {"com.test"})
public class Test
{
public static void main(String[] args)
{
SpringApplication.run(Test.class, args);
}
}
2、启动类加入@EnableScheduling标签表示启动定时器
3、定时器类加入@Component保证bean被加载