我有一些奇怪的情况(spring-boot
):
我在bean中有一个循环,但在Application.java
(main with SpringApplication.run(Application.class, args)
)中没有循环。在其他类(循环发生的地方)中,我使用@Configuration
怎么可能?
这些bean是:
@ConfigurationProperties(prefix = "my2")
@PostConstruct
@Bean
public javax.sql.DataSource dataSource2() {
return ...
}
@ConfigurationProperties(prefix = "my1")
@PostConstruct
@Bean
public javax.sql.DataSource dataSource1() {
return ...
}
@Primary
@Bean
public RoutingDataSource dataSource() {
RoutingDataSource rDS= new RoutingDataSource();
routingDataSource.setDefaultTargetDataSource(dataSource1);
Map<Object, Object> tds = new HashMap<Object, Object>();
tds.put("my2", dataSource2);
tds.put("my1", dataSource1);
rDS.setTargetDataSources(tds );
return rDS;
}
修改
@Configuration
class DataConfig {
@ConfigurationProperties(prefix = "my2")
@PostConstruct
public javax.sql.DataSource dataSource2() {
return ...
}
@ConfigurationProperties(prefix = "my1")
@Bean
public javax.sql.DataSource dataSource1() {
return ...
}
@DependOn(value = {"dataSource2", "dataSource1"})
@Primary
@Bean
public RoutingDataSource dataSource() {
RoutingDataSource rDS= new RoutingDataSource();
routingDataSource.setDefaultTargetDataSource(dataSource1);
Map<Object, Object> tds = new HashMap<Object, Object>();
tds.put("my2", dataSource2);
tds.put("my1", dataSource1);
rDS.setTargetDataSources(tds );
return rDS;
}
}