I have two classes and one of the classes should execute first at run-time among-st the two but it's happening the other way around.
First Class:
@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();
}
}
}
Second Class:
@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) {}
Currently the order followed by the Spring is :
1) b.mysqlDataSource()
2) b.LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder builder)
3) b.PlatformTransactionManager mysqlTransactionManager(@Qualifier("someEntityManager") EntityManagerFactory entityManagerFactory)
4) a.methodA()
5) a.methodB()
My order should be followed by the Spring as:
1) a.methodA()
2) a.methodB()
3) b.mysqlDataSource()
4) b.LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder builder)
5) b.PlatformTransactionManager mysqlTransactionManager(@Qualifier("someEntityManager") EntityManagerFactory entityManagerFactory)
And it must also go through the code where class B is the only one which depends all the url,username and password settings class A, so A must be initiated first.
You can use @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) {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.