适用于我的代码:
数据库配置的一部分如下所示:
@Profile("!dbClean")
@Bean(initMethod = "migrate")
public Flyway flywayNotADestroyer() {
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource());
flyway.setInitOnMigrate(true);
return flyway;
}
@Profile("dbClean")
@Bean(initMethod = "migrate")
public Flyway flywayTheDestroyer() {
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource());
flyway.setInitOnMigrate(true);
flyway.clean();
return flyway;
}
配置代表两个独占bean。当存在“dbClean”配置文件时创建一个,而当不存在时创建另一个配置文件。忍受我,忘记代码重复。
另一种配置管理Quartz配置:
@Autowired
private Flyway flyway;
@Bean
public SchedulerFactoryBean quartzScheduler(Flyway flyway) throws SchedulerException {
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
quartzScheduler.setDataSource(dataSource);
quartzScheduler.setTransactionManager(transactionManager);
quartzScheduler.setOverwriteExistingJobs(true);
quartzScheduler.setSchedulerName("mysuperduperthegratest-quartz-scheduler");
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
quartzScheduler.setJobFactory(jobFactory);
quartzScheduler.setQuartzProperties(schedulingProperties);
return quartzScheduler;
}
上面的作品就像一个魅力。问题是Quartz配置没有使用Flyway自动装配的bean。它只需要比Quartz调度程序更早创建这个bean。
所以理想的配置是:
db部分:
@Profile("!dbClean")
@Bean(name = "flyway", initMethod = "migrate")
public Flyway flywayNotADestroyer() {
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource());
flyway.setInitOnMigrate(true);
return flyway;
}
@Profile("dbClean")
@Bean(name = "flyway", initMethod = "migrate")
public Flyway flywayTheDestroyer() {
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource());
flyway.setInitOnMigrate(true);
flyway.clean();
return flyway;
}
Quartz部分:
@Bean
@DependsOn({"flyway"})
public SchedulerFactoryBean quartzScheduler() throws SchedulerException {
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
quartzScheduler.setDataSource(dataSource);
quartzScheduler.setTransactionManager(transactionManager);
quartzScheduler.setOverwriteExistingJobs(true);
quartzScheduler.setSchedulerName("mysuperduperthegratest-quartz-scheduler");
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
quartzScheduler.setJobFactory(jobFactory);
quartzScheduler.setQuartzProperties(schedulingProperties);
return quartzScheduler;
}
问题是最后的配置不起作用。这两个飞路豆都没有被创建过。我不明白为什么。回到我使用xml配置时,我记得在不同的配置文件中有两个具有相同名称的bean。它奏效了。我在这里做错了什么,或者这可能是Spring本身的一些错误?通常我自己调试Spring,但是使用@Configuration逻辑的Spring部分是一个绿色的文件,我现在不能浪费时间。
答案 0 :(得分:3)
我知道这个问题很久以前就被问过了,但我在Spring Boot项目中遇到了一个使用Quartz和Flyway的问题。
Quartz会在flyway创建表之前尝试启动。以下对我有用:
@DependsOn("flywayInitializer")
public SchedulerFactoryBean quartzScheduler() { ...