Spring 无法解析的循环引用 Quartz

Spring unresolvable circular reference Quartz

@Autowired
private ApplicationContext applicationContext;

@Autowired
private Scheduler scheduler;

public static void main(String[] args) {
    SpringApplication.run(SchedulingApplication.class, args);
}

@PostConstruct
public void init() {
    try {
        this.scheduler.start();
    } catch (SchedulerException e) {
        LOGGER.error("Failed to start scheduler");
    }
}

@Bean
@Autowired
public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) {
    SchedulerFactoryBean bean = new SchedulerFactoryBean();
    bean.setDataSource(dataSource);
    bean.setApplicationContext(applicationContext);
    bean.setAutoStartup(true);
    bean.setConfigLocation(new ClassPathResource("/config/quartz.properties"));

    AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);

    bean.setJobFactory(jobFactory);

    return bean;
}

创建名称为 'schedulerFactoryBean' 的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用??

适用于 spring boot 1.2.3,不适用于最新的 1.3.2。

SchedulerFactoryBean 用于创建 Scheduler 的实例。 在您的配置中 Spring 尝试在执行 @Bean 方法之前注入一个 Scheduler,因此在创建 SchedulerFactoryBean 之前。 这就是为什么你得到例外。 如果将其拆分,一个配置使用 SchedulerFactoryBean,另一个配置使用 Scheduler,它应该可以工作。