Spring 中获取配置的三种方式
- 通过
@Value
方式动态获取单个配置 - 通过
@ConfigurationProperties
+ 前缀方式批量获取配置 -
通过
@Value
动态获取单个配置作用
-
优点
-
缺点
配置名不能被有效枚举到
- 每一个配置的使用都需重新定义,使用较为麻烦
-
使用场景
项目强依赖该配置的加载,想要从源头避免因配置缺失导致的未知问题
-
代码示例
@Configuration
public class ConfigByValueAnnotation {
@Value("${server.port}")
private String serverPort;
public String getServerPort() {
return serverPort;
}
}
测试代码
@DisplayName("multipart get config")
@SpringBootTest
public class MultipartGetConfigTest {
private static final Logger log = LoggerFactory.getLogger(MultipartGetConfigTest.class);
@Autowired
private ConfigByValueAnnotation configByValueAnnotation;
@Test
public void getByValueAnnotation(){
log.info("get by @Value, value: {}", configByValueAnnotation.getServerPort());
}
}
测试结果
org.spring.demo.MultipartGetConfigTest : get by @Value, value: 7100
通过
@ConfigurationProperties
+ 前缀方式批量获取作用
-
优点
使用配置只需确定 key 的前缀即能使用,有利于批量获取场景的使用
因采用前缀匹配,所以在使用新的相同前缀 key 的配置时无需改动代码
缺点
使用复杂,需定义配置类或者手动创建 bean 后引入使用
-
使用场景
需要同时使用多前缀相同 key 的配置
-
代码示例
```java @Component @ConfigurationProperties(prefix = “server”, ignoreInvalidFields = true) public class ConfigByConfigurationProperties {
private Integer port;
public Integer getPort() {
return port;
}
public ConfigByConfigurationProperties setPort(Integer port) {
this.port = port;
return this;
} }
@Configuration public class ConfigByConfigurationPropertiesV2 {
@Bean("configByValueAnnotationV2")
@ConfigurationProperties(prefix = "server2")
public Properties properties(){
return new Properties();
}
}
<a name="K8TeD"></a>
#### 测试代码
```java
@DisplayName("multipart get config")
@SpringBootTest
public class MultipartGetConfigTest {
private static final Logger log = LoggerFactory.getLogger(MultipartGetConfigTest.class);
@Autowired
private ConfigByConfigurationProperties configByConfigurationProperties;
@Autowired
@Qualifier("configByValueAnnotationV2")
private Properties properties;
@Test
public void getByConfigurationProperties(){
log.info("get by @ConfigurationProperties, value: {}", configByConfigurationProperties.getPort());
log.info("get by @ConfigurationProperties and manual create bean, value: {}", properties.getProperty("port"));
}
}
测试结果
org.spring.demo.MultipartGetConfigTest : get by @ConfigurationProperties, value: 7100
org.spring.demo.MultipartGetConfigTest : get by @ConfigurationProperties and manual create bean, value: 7100
通过 Environment
动态获取单个配置
作用
-
优点
获取的配置的 key 可不提前定义,程序灵活性高
-
缺点
使用较复杂,需继承 Environment 接口形成工具类进行获取
获取 key 对应的枚举与 key 定义分离,value 获取链路较长
使用场景
只需使用少量的配置
获取配置的 key 无提前定义,需要根据对配置的有无进行灵活使用
代码示例
```java @Component public class ConfigByEnvironment implements EnvironmentAware {
private static final Logger log = LoggerFactory.getLogger(ConfigByEnvironment.class);
private Environment environment;
public Optional
get(String configKey){ String config = environment.getProperty(configKey);
return Objects.isNull(config) ? Optional.empty() : Optional.of(config);
}
public void get(String configKey, Consumer
consumer){ Optional<String> config = get(configKey);
if(!config.isPresent()){
log.warn("application config, get config by key fail, key: {}", configKey);
}
config.ifPresent(consumer);
}
@Override public void setEnvironment(@NonNull Environment environment) {
this.environment = environment;
} }
public enum ConfigByEnvironmentKey {
SERVER_PORT("server.port", "server port");
private String key;
private String description;
ConfigByEnvironmentKey(String key, String description) {
this.key = key;
this.description = description;
}
public String getKey() {
return key;
}
public String getDescription() {
return description;
}
}
<a name="sVcvt"></a>
#### 测试代码
```java
@DisplayName("multipart get config")
@SpringBootTest
public class MultipartGetConfigTest {
private static final Logger log = LoggerFactory.getLogger(MultipartGetConfigTest.class);
@Autowired
private ConfigByEnvironment configByEnvironment;
@Test
public void getByEnvironment(){
configByEnvironment.get(ConfigByEnvironmentKey.SERVER_PORT.getKey()).ifPresent(value -> log.info("get by environment, value: {}", value));
}
}
测试结果
org.spring.demo.MultipartGetConfigTest : get by environment, value: 7100