简介
说明
对于application.yml里边的配置,我们可以通过@Value来获得,比如:@Value(“${user.name}”)。但是,如果属性很多,写很多个@Value就会很繁琐。
解决方案就是:使用@ConfigurationProperties,将一组属性通过一个类来接收。
注意
- 保存属性的类必须加getter和setter。
- prefix不能有这些:大写字母、某些特殊字符(比如:_、~、!、@、#、$、*、+、:等)
- 可以包含的字符有:-
@ConfigurationProperties与@Value的区别
| 项 | @ConfigurationProperties | @Value |
| 类型 | Map、内部类、对象等。 | 不支持内部类、对象。 |
| spEl表达式 | 不支持 | 支持 |
| JSR303数据校验 | 支持 | 不支持 |
| 功能 | 一个列属性批量注入 | 单属性注入 |
使用方法
法1:@ConfigurationProperties+@Component
@Component
@ConfigurationProperties(prefix = "hello")
@Data
public class HelloProperties {
private String name;
private List<String> emails;
private Map<String, Integer> price;
}
法2:@ConfigurationProperties+@Bean
此时HelloProperties不需要加任何注解。
配置类
@Configuration
public class Config {
@Bean
@ConfigurationProperties(prefix = "hello")
public HelloProperties helloProperties(){
return new HelloProperties();
}
}
实体类
@Data
public class HelloProperties {
private String name;
private List<String> emails;
private Map<String, Integer> price;
}
法3:@EnableConfigurationProperties+@ConfigurationProperties
在有@Configuration注解的类上加@EnableConfigurationProperties来声明。
配置类
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloConfiguration {
}
实体类
@ConfigurationProperties(prefix = "hello")
@Data
public class HelloProperties {
private String name;
private List<String> emails;
private Map<String, Integer> price;
}
嵌套实体类
如果存在嵌套实体类,要写为静态内部类。只有静态内部类在yml配置时才会有自动补全提示!
比如:
package com.example.config;
import lombok.Data;
@Component
@ConfigurationProperties(prefix = "hello")
@Data
public class HelloProperties {
private Boolean enabled;
private String helloName1;
private ChildProperties child;
@Data
public static class ChildProperties {
private String childName;
private Long id;
}
}
这样在输入hello.child时,会自动提示出child-name和id。
如果不写为静态内部类,就只能提示到hello.child这一层,没有child-name和id这种内部字段的提示!
备注
以上代码提示是starter使用如下依赖时的功能:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<scope>provided</scope>
</dependency>
实例
application.yml
hello:
relaxed:
enabled: true
helloname1: helloname1
helloName2: helloName2
HELLONAME3: HELLONAME3
hello-name4: hello-name4
hello_name5: hello_name5
HELLO-NAME6: HELLO-NAME6
hiname1: hiname1
hiName2: hiName2
HINAME3: HINAME3
hi-name4: hi-name4
hi_name5: hi_name5
HI-NAME6: HI-NAME6
child:
childName1: childName
id: 1000
配置类及属性类
配置类
package com.example.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigPropConf {
@Bean
@ConfigurationProperties(prefix = "hello.relaxed")
public HelloProperties httpsProperties() {
return new HelloProperties();
}
}
顶层属性类
package com.example.config;
import lombok.Data;
@Data
public class HelloProperties {
private Boolean enabled;
private String helloName1;
private String helloName2;
private String helloName3;
private String helloName4;
private String helloName5;
private String helloName6;
private String hiname1;
private String hiname2;
private String hiname3;
private String hiname4;
private String hiname5;
private String hiname6;
private ChildProperties child;
@Data
public static class ChildProperties {
private String child_Name1;
private Long id;
}
}
Controller
package com.example.controller;
import com.example.config.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private HelloProperties helloProperties;
@GetMapping("/prop")
public HelloProperties prop() {
return helloProperties;
}
}
测试
访问:http://localhost:8080/hello/prop


请先 !