spring_reference/IV. Spring Boot features/23.7.2. Relaxed binding.md

22 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 23.7.2. 松散的绑定Relaxed binding
Spring Boot使用一些宽松的规则用于绑定Environment属性到@ConfigurationProperties beans所以Environment属性名和bean属性名不需要精确匹配。常见的示例中有用的包括虚线分割比如context--path绑定到contextPath和将环境属性转为大写字母比如PORT绑定port
示例:
```java
@Component
@ConfigurationProperties(prefix="person")
public class ConnectionSettings {
private String firstName;
}
```
下面的属性名都能用于上面的@ConfigurationProperties类
| 属性 | 说明 |
| -------- | :----- |
|person.firstName|标准驼峰规则|
|person.first-name|虚线表示,推荐用于.properties和.yml文件中|
|PERSON_FIRST_NAME|大写形式,使用系统环境变量时推荐|
Spring会尝试强制外部的应用属性在绑定到@ConfigurationProperties beans时类型是正确的。如果需要自定义类型转换你可以提供一个ConversionService beanbean id为conversionService或自定义属性编辑器通过一个CustomEditorConfigurer bean