0

I need to create one bean that will be execute before Spring Boot create my connection, so I have this 2 @Config to create my dbs

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "dbregistroEntityManagerFactory",basePackages = {"br.com.lumera.repositories.registro"})
@EnableTransactionManagement
public class DbRegistroConfig {

    @Bean(name = "dbRegistroDataSource")
    @ConfigurationProperties(prefix = "datasource.dbregistro")
    public DataSource secondaryDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "dbregistroEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean dbregistroEntityManagerFactory(
            EntityManagerFactoryBuilder builder, @Qualifier("dbRegistroDataSource") DataSource dbRegistroDataSource) {
        return builder
                .dataSource(dbRegistroDataSource)
                .packages("br.com.lumera.entity.registro")
                .persistenceUnit("dbregistro")
                .build();
    }
    @Bean(name = "dbregistroTransactionManager")
    public JpaTransactionManager dbRegistroTransactionManager(@Qualifier("dbregistroEntityManagerFactory") final EntityManagerFactory factory)
    {
        return new JpaTransactionManager(factory);
    }
}

AND

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", basePackages = {"br.com.lumera.repositories.dnaso"})
@EnableTransactionManagement
public class DbDnasoConfig {

    @Bean(name = "dataSource")
    @Primary
    @ConfigurationProperties(prefix = "datasource.dnaso")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "entityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean dbdnasoEntityManagerFactory(
            EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("br.com.lumera.entity.dnaso")
                .persistenceUnit("dbdnaso")
                .build();
    }
    @Bean(name = "transactionManager")
    @Primary
    public JpaTransactionManager dbRegistroTransactionManager(@Qualifier("entityManagerFactory") final EntityManagerFactory factory)
    {
        return new JpaTransactionManager(factory);
    }
}

Then I create my class with autoconfigurate:

@Component
@AutoConfigureOrder
@ConditionalOnClass(DataSourceAutoConfiguration.class)
@ConfigurationProperties(prefix="dbdanso.flyway")
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
public class FlywayConf {
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Bean
    public Boolean teste(){
        Flyway flyway = new Flyway();
        flyway.setDataSource("jdbc:postgresql://localhost:5432/portalservicos","postgres","dna44100");
        flyway.setBaselineOnMigrate(true);
        flyway.setLocations("classpath:db/migration/dbdnaso");
        flyway.migrate();
        return true;
    }

}

but he execute after my DbRegistroConfig and DbDnasoConfig anyone can help me ?

Fabio Ebner
  • 2,613
  • 16
  • 50
  • 77
  • Look at this link: [Spring Boot auto configuration order](http://stackoverflow.com/questions/31692905/spring-boot-auto-configuration-order). In particular, note that "... @AutoConfigureOrder is just another means to order auto-configurations, not the instantiation of beans they create". See also [Spring Boot Service Bean Creation Order](http://stackoverflow.com/questions/33874095/java-spring-boot-service-bean-creation-order) – paulsm4 Apr 07 '16 at 00:07
  • Doesn't configuration always occur before components? You are trying to create the flywayConf bean before the Configuration beans. I think in order to make that happen, you will need to create the flywayConf bean within a class also marked with Configuration and then handle order from there. – pczeus Apr 07 '16 at 01:43
  • basically you want to execute the FlaywayConf before DbRegistroConfig and DbDnasoConfig. Is that right? – Hareesh Apr 07 '16 at 04:50
  • @Hareesh correct. I think to execute in primaryDataSource() or secondaryDataSource(), but will be better if I separate :D tks – Fabio Ebner Apr 07 '16 at 11:15

1 Answers1

1

Spring does't bother about whether its a AutoConfiguration or normal POJO class. It just looks for the Configurations and it creates the beans based on the configuration given at the bean level.

To run FlywayConf before the DbRegistroConfig and DbDnasoConfig,add @DependsOn Annoatation @Bean level of the DbRegistroConfig and DbDnasoConfig.

Hareesh
  • 694
  • 6
  • 18