PlatformTransactionManager

PlatformTransactionManager

我有两个类,其中一个类应该在运行时首先在两个类中执行,但是相反。

头等舱:

@Configuration
public class A(){
@Bean
public void methodA(){}

@Bean
public void methodB(){
        for(i=0;i<9;i++){
            B b = new B();
            b.setJdbcUrl();
            b.setUsername();
            b.setPassword();
        }
    }
}


二等舱:

@Configuration
public class B() extends HikariConfig {
@Bean
public HikariDataSource mysqlDataSource(){}

@Bean(name = "someEntityManager")
public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder builder) {}

@Bean(name = "someTransactionManager")
public PlatformTransactionManager mysqlTransactionManager(@Qualifier("someEntityManager") EntityManagerFactory entityManagerFactory) {}



当前,Spring之后的顺序是:
1)b.mysqlDataSource()
2)b.LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder构建器)
3)b.PlatformTransactionManager mysqlTransactionManager(@Qualifier(“ someEntityManager”)EntityManagerFactory entityManagerFactory)
4)a.methodA()
5)a.methodB()

我的订单应在春季之后为:
1)a.methodA()
2)a.methodB()
3)b.mysqlDataSource()
4)b.LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder构建器)
5)b.PlatformTransactionManager mysqlTransactionManager(@Qualifier(“ someEntityManager”)EntityManagerFactory entityManagerFactory)

并且它还必须经过代码,其中类B是唯一一个依赖于所有URL,用户名和密码设置类A的代码,因此必须首先启动A。

最佳答案

您可以使用@DependsOn:

@Configuration
public class A {

    @Bean
    public void methodA() {
    }

    @DependsOn("methodA")
    @Bean
    public void methodB() {
        for (i = 0; i < 9; i++) {
            B b = new B();
            b.setJdbcUrl();
            b.setUsername();
            b.setPassword();
        }
    }
}

@Configuration
@DependsOn({"methodA", "methodB"})
public class B extends HikariConfig {

    @DependsOn("mysqlEntityManagerFactory")
    @Bean
    public HikariDataSource mysqlDataSource() {
    }

    @DependsOn("mysqlTransactionManager")
    @Bean(name = "someEntityManager")
    public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder builder) {
    }

    @Bean(name = "someTransactionManager")
    public PlatformTransactionManager mysqlTransactionManager(@Qualifier("someEntityManager") EntityManagerFactory entityManagerFactory) {
    }

}

10-02 02:39