如何在弹簧启动时有条件地自动布线?
2022-09-03 05:45:43
我创建了一个调度程序类
public class TestSchedulderNew {
@Scheduled(fixedDelay = 3000)
public void fixedRateJob1() {
System.out.println("Job 1 running");
}
@Scheduled(fixedDelay = 3000)
public void fixedRateJob2() {
System.out.println("Job 2 running");
}
}
在配置中,我放置了@ConditionalOnProperty注释以在有条件的目的上启用它。
@Bean
@ConditionalOnProperty(value = "jobs.enabled")
public TestSchedulderNew testSchedulderNew() {
return new TestSchedulderNew();
}
现在在控制器中,我已经创建了“stopScheduler”方法来停止那些调度程序,在这个控制器中,我已经自动连接了TestSchedulderNew类
@RestController
@RequestMapping("/api")
public class TestCont {
private static final String SCHEDULED_TASKS = "testSchedulderNew";
@Autowired
private ScheduledAnnotationBeanPostProcessor postProcessor; /]
@Autowired
private TestSchedulderNew testSchedulderNew;
@GetMapping(value = "/stopScheduler")
public String stopSchedule(){
postProcessor.postProcessBeforeDestruction(testSchedulderNew,
SCHEDULED_TASKS);
return "OK";
}
}
现在的问题是,如果条件属性是假的,那么我得到下面的异常
Field testSchedulderNew in com.sbill.app.web.rest.TestCont required a bean of type 'com.sbill.app.schedulerJob.TestSchedulderNew
如果真的一切正常,
我们有什么选择来解决这个问题吗?