Java在使用Quartz空指针时无法使用jar命令启动项目
我使用spring boot与Quartz集成,在工作中使用Autowired时遇到问题Java在使用Quartz空指针时无法使用jar命令启动项目,java,spring,quartz-scheduler,Java,Spring,Quartz Scheduler,我使用spring boot与Quartz集成,在工作中使用Autowired时遇到问题 @Bean public Scheduler customScheduler() throws SchedulerException { SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactoryBean.getS
@Bean
public Scheduler customScheduler() throws SchedulerException {
SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactoryBean.getScheduler();
scheduler.setJobFactory(customJobFactory);// Custom JobFactory makes it possible to use the @Autowired
scheduler.start();
return scheduler;
}
当一切准备就绪,在IDEA中启动项目并进行测试时,神奇的事情发生了,当我将使用maven打包为jar的项目时,使用jar命令启动项目
@Resource
private CustomJobFactory customJobFactory;
customJobFactory的输出值为空。
这并不是每次都会发生,当它发生时,如果您使用maven重新打包它,则有时不会发生异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzJobListener': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customScheduler' defined in class path resource [com/th/files/quartz/listener/QuartzJobListener.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.quartz.Scheduler]: Circular reference involving containing bean 'quartzJobListener' - consider declaring the factory method as static for independence from its containing instance. Factory method 'customScheduler' threw exception; nested exception is java.lang.IllegalArgumentException: JobFactory cannot be set to null!
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321) ~[spring-context-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
我做错了什么?
PS:我在CustomJobFactory实例测试中同时使用了@Resource和@Autowired,有时会得到null
spring-boot.version 2.2.7.RELEASE
spring-context.version 5.2.0.RELEASE
quartz.version 2.3.1
quartz-jobs.version 2.3.1
我可以问一下为什么要创建定制的AdaptiableJobFactory吗?同样仅供参考,您可以使用
spring boot starter quartz
自动配置quartz。并使用SchedulerFactoryBeanCustomizer类自定义几乎所有内容。另请参见org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration#quartzScheduler
类和代码comments@gtiwari333使用AdapteableJobFactory的原因是无法使用资源实例化用于业务逻辑处理的作业的实现。请尝试使用@Component
而不是自定义JobFactory类的@配置
。如果没有帮助,请分享你的完整代码。
spring-boot.version 2.2.7.RELEASE
spring-context.version 5.2.0.RELEASE
quartz.version 2.3.1
quartz-jobs.version 2.3.1