Spring Boot 2.0 Quartz - Use non-primary datasource

后端 未结 3 1354
南笙
南笙 2021-02-08 19:58

I used Quartz as scheduler in my application. Trying to use Spring boot 2.0 features. I have 2 different data sources in the configuration. One for application and another one f

3条回答
  •  野的像风
    2021-02-08 20:20

    You could customise the datasource by building the SchedulerFactoryBean yourself:

    @Bean
    public SchedulerFactoryBean schedulerFactory() {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setDataSource(schedulerDataSource());
        return bean;
    }
    

    Or, in Spring Boot 2, you can use a SchedulerFactoryBeanCustomizer. This will allow you customise the bean instantiated by the autoconfigurer, which may be less work.

    @Configuration
    public class SchedulerConfig {
        DataSource dataSource;
    
        @Autowired
        public SchedulerConfig(@Qualifier("scheduler.datasource") DataSource dataSource) {
            this.dataSource = dataSource;
        }
    
        @Bean
        public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer()
        {
            return bean -> bean.setDataSource(dataSource);
        }
    }
    

提交回复
热议问题