简体   繁体   English

调度程序作为 Spring 引导中的依赖项

[英]Scheduler as dependency in Spring Boot

I have two maven module 'client' and 'scheduler'.我有两个 maven 模块“客户端”和“调度程序”。 'scheduler' module consists of code with Scheduler and runs each minute. “调度程序”模块由带有调度程序的代码组成,每分钟运行一次。 When I run 'scheduler' module, it is working fine and scheduler executes each minute.当我运行“调度程序”模块时,它工作正常,调度程序每分钟执行一次。 Now, when I add it as a dependency in 'client' module, scheduler never runs.现在,当我将它添加为“客户端”模块中的依赖项时,调度程序永远不会运行。

Spring boot needs to know two things to run the scheduler. Spring 启动需要知道两件事才能运行调度程序。 ie The bean of the scheduler and config for enabling scheduling.即调度程序的bean和用于启用调度的配置。

So, you need to add @EnableScheduling annotation to enable the schedulers and you need to register the scheduler bean in the spring context.因此,您需要添加@EnableScheduling注解以启用调度程序,并且您需要在 spring 上下文中注册调度程序 bean。 For that, you can use为此,您可以使用

@ComponentScan (basePackages= {'current project package', 'scheduler package'}

or或者

@SpringBootApplication(scanBasePackages = {'current project package', 'scheduler package'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM