我有两个DataSource bean和两个DataSourceInitializer bean,每个DataSource一个。这些配置位于不同的类文件中,但两个DataSourceInitializers都尝试设置DataSourceOne。
如何告诉DataSourceInitializerTwo使用(注入?)DataSourceTwo bean?
//Data Source 1
@Bean
public DataSource dataSourceOne() {
JdbcDataSource ds = new JdbcDataSource();
ds.setUrl("jdbc:h2:mem:app1db;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS app1\\;SET SCHEMA app1");
ds.setUser("app1");
ds.setPassword("app1");
return ds;
}
@Bean
public DataSourceInitializer dataSourceInitializerOne(final DataSource dataSource) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
initializer.setDatabasePopulator(DatabasePopulator());
return initializer;
}
//Data Source 2
@Bean
public DataSource dataSourceTwo() {
JdbcDataSource ds = new JdbcDataSource();
ds.setUrl("jdbc:h2:mem:app1db;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS app2\\;SET SCHEMA app2");
ds.setUser("app2");
ds.setPassword("app2");
return ds;
}
@Bean
public DataSourceInitializer dataSourceInitializerTwo(final DataSource dataSource) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSourceTwo);
initializer.setDatabasePopulator(databasePopulatorTwo());
return initializer;
}
答案 0 :(得分:0)
DataSourceInitializer方法似乎基于参数名称注入DataSource bean。只需将返回DataSource的方法的名称与适当的初始化方法所接受的DataSource参数的名称相匹配即可。
//Data Source 1
@Bean
public DataSource dataSourceOne() {
JdbcDataSource ds = new JdbcDataSource();
ds.setUrl("jdbc:h2:mem:app1db;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS app1\\;SET SCHEMA app1");
ds.setUser("app1");
ds.setPassword("app1");
return ds;
}
@Bean
public DataSourceInitializer dataSourceInitializerOne(final DataSource dataSourceOne) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSourceOne);
initializer.setDatabasePopulator(DatabasePopulator());
return initializer;
}
//Data Source 2
@Bean
public DataSource dataSourceTwo() {
JdbcDataSource ds = new JdbcDataSource();
ds.setUrl("jdbc:h2:mem:app1db;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS app2\\;SET SCHEMA app2");
ds.setUser("app2");
ds.setPassword("app2");
return ds;
}
@Bean
public DataSourceInitializer dataSourceInitializerTwo(final DataSource dataSourceTwo) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSourceTwo);
initializer.setDatabasePopulator(databasePopulatorTwo());
return initializer;
}