@ConfigurationProperties如何配置复杂类型
2021-11-06 本文已影响0人
liaijuyyer
@ConfigurationProperties 注解可以以前缀的形式统一注入某个前缀的参数 如下面的示例
示例
#数据源
spring.datasource.druid.write.url=jdbc:mysql://localhost:3306/jpa
spring.datasource.druid.write.username=root
spring.datasource.druid.write.password=1
spring.datasource.druid.write.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.read.url=jdbc:mysql://localhost:3306/jpa
spring.datasource.druid.read.username=root
spring.datasource.druid.read.password=1
spring.datasource.druid.read.driver-class-name=com.mysql.jdbc.Driver
@Configuration
public class DruidDataSourceConfig {
/**
* DataSource 配置
* @return
*/
@ConfigurationProperties(prefix = "spring.datasource.druid.read")
@Bean(name = "readDruidDataSource")
public DataSource readDruidDataSource() {
return new DruidDataSource();
}
/**
* DataSource 配置
* @return
*/
@ConfigurationProperties(prefix = "spring.datasource.druid.write")
@Bean(name = "writeDruidDataSource")
@Primary
public DataSource writeDruidDataSource() {
return new DruidDataSource();
}
}
问题
可以看到示例中的配置其实是有两个前缀 spring.datasource.druid.read、spring.datasource.druid.write 如果需要用一个配置类去接收应该怎么去做?可以参考下面的示例
解决方案
下面的配置实现了将read和write两个不同前缀的配置放在了一个配置类中进行注入 核心思想就是利用了内部类
/**
* @ConfigurationProperties 注解必须配合@Component、@Bean、@Configuration注解一起使用
* 也就是说@ConfigurationProperties注解的类 必须被spring托管
* @date 2021/11/06
**/
@Component
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DataSourceProperties {
//write内部类 用于配置前缀为spring.datasource.druid.write的配置
private Write write = new Write();
//read内部类 用于配置前缀为spring.datasource.druid.read的配置
private Read read = new Read();
public Write getWrite() {
return write;
}
public void setWrite(Write write) {
this.write = write;
}
public Read getRead() {
return read;
}
public void setRead(Read read) {
this.read = read;
}
public class Write {
private String url;
private String username;
private String password;
private String driverClassName;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
}
public class Read {
private String url;
private String username;
private String password;
private String driverClassName;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
}
}