我创建了一个调度程序类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"; } } 现在的问题是,如果条件属性为 false 那么我会得到以下异常 Field testSchedulderNew in com.sbill.app.web.rest.TestCont required a bean of type 'com.sbill.app.schedulerJob.TestSchedulderNew如果是真的,一切正常,我们有什么办法可以解决这个问题吗?
1 回答
动漫人物
TA贡献1815条经验 获得超10个赞
您可以使用@Autowired(required=false)null 签入stopScheduler方法。
@Autowired(required=false)
private TestSchedulderNew testSchedulderNew;
@GetMapping(value = "/stopScheduler")
public String stopSchedule() {
if (testSchedulderNew != null) {
postProcessor.postProcessBeforeDestruction(testSchedulderNew,
SCHEDULED_TASKS);
return "OK";
}
return "NOT_OK";
}
添加回答
举报
0/150
提交
取消