我想實作非平凡的 bean 注入實作。
我有一個自定義屬性檔案:
@Getter
@Setter
@ConfigurationProperties
public class DatabaseProperties {
private String url;
private String username;
private String password;
}
我這里是組態檔:
@Configuration
@EnableConfigurationProperties(DatabaseProperties.class)
public class DBConfig {
@Bean
@ConfigurationProperties(prefix = "datasource.database1")
public JdbcTemplate jdbcTemplateDatabase1(DatabaseProperties databaseProperties) {
DataSource dataSource = new DriverManagerDataSource(
databaseProperties.getUrl(),
databaseProperties.getUsername(),
databaseProperties.getPassword());
return new JdbcTemplate(dataSource);
}
@Bean
@ConfigurationProperties(prefix = "datasource.database2")
public JdbcTemplate jdbcTemplateDatabase2(DatabaseProperties databaseProperties) {
DataSource dataSource = new DriverManagerDataSource(
databaseProperties.getUrl(),
databaseProperties.getUsername(),
databaseProperties.getPassword());
return new JdbcTemplate(dataSource);
}
}
我想要實作的目標是基于前綴實體化一個新的 DatabaseProperties 實體。
有兩種可能的解決方案:
- 使用相應的前綴創建兩個 DatabaseProperties 型別的 bean 和兩個 JdbcTemplate bean,其中引數相應地限定為 DatabaseProperties bean。
- 在每個 JdbcTemplate bean 中提供 3 個引數(字串 url、字串用戶名、字串密碼)并通過 @Value 注入它們
但是是否可以擺脫為每個 JdbcTemplate 創建 DatabaseProperties bean 或使用 @Value ?
uj5u.com熱心網友回復:
據我所知,如果您想訪問多個資料庫,Spring 將無法為您做魔術,這一事實是無法回避的。您將需要創建兩個JdbcTemplateSpring 管理的 bean,然后使用@Qualifier注釋將它們注入到需要的地方。
這種方法有兩個好處:
- 它確實有效;
- 你很清楚你在做什么。Spring 應用程式已經在其中發生了大量的魔法,因此當我們需要一些更自定義的東西并且實作起來并不那么復雜時,我們可能希望避免一些額外的魔法。
uj5u.com熱心網友回復:
您不需要創建DatabaseProperties. Spring 已經在資料源和屬性變數上為我們做了這個
@Configuration
public class ConfigDataSource {
@Bean("datasource-1") // this name will qualify on @autowired
@ConfigurationProperties(prefix="spring.datasource.yourname-datasource-1") // this is the name for the prefix for datasource on .properties settings
public DataSource dataSourcePostgres() {
return DataSourceBuilder.create().build();
}
@Bean("datasource-2") // this name will qualify on @autowired
@ConfigurationProperties(prefix="spring.datasource.yourname-datasource-2") // this is the name for the prefix for datasource on .properties settings
public DataSource dataSourceMySql() {
return DataSourceBuilder.create().build();
}
}
。特性
# Its required use the same name declared in bean
spring.datasource.yourname-datasource-1.url=...
spring.datasource.yourname-datasource-1.jdbcUrl=${spring.datasource.yourname-datasource-1}
spring.datasource.yourname-datasource-1.username=user
spring.datasource.yourname-datasource-1.password=pass
spring.datasource.yourname-datasource-1.driver-class-name=your.driver
spring.datasource.yourname-datasource-2.url=...
spring.datasource.yourname-datasource-2.jdbcUrl=${spring.datasource.yourname-datasource-2}
spring.datasource.yourname-datasource-2.username=user
spring.datasource.yourname-datasource-2.password=pass
spring.datasource.yourname-datasource-2.driver-class-name=your.driver
在服務上使用
@Awtowired
@Qualifier("datasource-1")
private DataSource dataSource1;
@Awtowired
@Qualifier("datasource-2")
private DataSource dataSource2;
public testJdbcTemplate(){
// You can qualifier JdbcTemplate below on bean and not necessary need instance on service
JdbcTemplate jdbcTemplateDatasource1 = new JdbcTemplate(dataSource1);
JdbcTemplate jdbcTemplateDatasource2 = new JdbcTemplate(dataSource2);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/375995.html
標籤:爪哇 春天 配置 spring-jdbc 春豆
下一篇:springcloudstreamkafka::創建名為“supplierInitializer”的bean在類路徑資源中定義時出錯
