按前缀自动连接自定义@ConfigurationProperties对象



我希望实现不平凡的bean注入实现。

我有一个自定义属性文件:

@Getter
@Setter
@ConfigurationProperties
public class DatabaseProperties {
private String url;
private String username;
private String password;
}

I这是配置文件:

@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?

您不需要创建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);
}

据我所知,如果你想访问多个数据库,Spring将无法为你施展魔法。您将需要创建两个JdbcTemplateSpring托管bean,然后使用@Qualifier注释将它们注入到需要的地方。

这种方法有两个好处:

  1. 它确实有效
  2. 你对自己正在做的事情很明确。Spring应用程序已经有了很多神奇的东西,所以当我们需要更自定义的东西时,我们可能想避免一些额外的东西,而这并不是那么复杂

最新更新