编程语言
首页 > 编程语言> > java-专用的Spring概要分析的bean和DependsOn注释

java-专用的Spring概要分析的bean和DependsOn注释

作者:互联网

适用于我的代码:

数据库配置的一部分如下所示:

@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;
}

该配置代表两个互斥豆.存在“ 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.

因此理想的配置是:

数据库部分:

@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;
}

石英部分:

@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,但是Spring具有@Configuration逻辑的部分对我来说是绿色的,现在我不能浪费时间在上面.

解决方法:

我知道这个问题是很久以前问过的,但是在Spring Boot项目中使用Quartz和Flyway遇到了一个问题.

Quartz将在flyway创建表之前尝试启动.以下为我工作:

@DependsOn("flywayInitializer")
public SchedulerFactoryBean quartzScheduler() { ...

标签:spring-profiles,spring,java
来源: https://codeday.me/bug/20191120/2044880.html