SpringBoot配置綁定
使用
如果需要從外部加載/配置一些屬性,可以將@ConfigurationProperties添加到類的定義或者方法中.
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
@AliasFor("prefix")
String value() default "";
@AliasFor("value")
String prefix() default "";
boolean ignoreInvalidFields() default false;
boolean ignoreUnknownFields() default true;
}
| 字段 | 含義 |
|---|---|
| value | 綁定到對象上的時候,讀取的配置文件中的前綴 |
| prefix | 和value含義相同 |
| ignoreInvalidFields | 綁定時忽略的無效字段(default:false) |
| ignoreUnknownFields | 綁定時忽略的未知字段(default:true) 如果設置為false,在進行綁定時,實體類沒有對應的字段,會報異常. |
在idea中使用@ConfigurationProperties時,需要添加@Component注解,否則會報錯.
如果引用的是jar包中的對象,它們本身沒有添加@Component,我們可以在自己的配置類中,增加注解:
@EnableConfigurationProperties(xxx.class),
在添加完成注解后,idea可能會提示:
Spring Boot Configuration Annotation Processor not configured
需要在pom中引入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
舉例
創建People實體類
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Component
@ConfigurationProperties(prefix = "people", ignoreUnknownFields = true,ignoreInvalidFields = true)
public class People {
private String name;
private int age;
private String addr;
private String eMail;
private List<People> children;
private Map<String,People> otherMap;
}
配置文件中的屬性
people.age=18
people.name=AAA
people.addr=addr1
people.msn=1234
people.children=children1
啟動后,提示:
Description:
Failed to bind properties under 'people.children' to java.util.List<com.example.springcodelearn.model.People>:
Property: people.children
Value: children1
Origin: class path resource [application.properties] - 5:17
Reason: No converter found capable of converting from type [java.lang.String] to type [java.util.List<com.example.springcodelearn.model.People>]
Action:
Update your application's configuration
因為實體類中,沒有People的類型為對象,檢查類型時有錯誤..
如果將實體類中的
ignoreInvalidFields = false
修改為
ignoreInvalidFields = true
啟動后打印
Description:
Binding to target [Bindable@720bf653 type = com.example.springcodelearn.model.People, value = 'provided', annotations = array<Annotation>[@org.springframework.boot.context.properties.ConfigurationProperties(ignoreInvalidFields=true, ignoreUnknownFields=false, prefix=people, value=people)]] failed:
Property: people.children
Value: children1
Origin: class path resource [application.properties] - 5:17
Reason: The elements [people.children,people.msn] were left unbound.
Property: people.msn
Value: 1234
Origin: class path resource [application.properties] - 4:12
Reason: The elements [people.children,people.msn] were left unbound.
Action:
Update your application's configuration
因為msn並不在People的屬性中.
這時候,再將
ignoreUnknownFields=false
修改為
ignoreUnknownFields=true
啟動正常,打印
People{name='AAA', age=18, addr='addr1', eMail='null', children=null, otherMap=null}
關於List,與Map
-
List
List 對象應當寫為類似於數組的形式:people.children[0].name=children0 people.children[0].age=21 people.children[0].addr=addr1 people.children[1].name=children1 people.children[1].age=22 people.children[1].addr=addr2 -
Map
Map對象應當寫為key=value的形式.
people.otherMap.name=otherMapName people.otherMap.age=-1 people.otherMap.addr=otherMapAddr
properties中增加以上屬性后,啟動打印
People{name='AAA', age=18, addr='addr1', eMail='null', children=[People{name='children0', age=21, addr='addr1', eMail='null', children=null, otherMap=null}, People{name='children1', age=22, addr='addr2', eMail='null', children=null, otherMap=null}], otherMap=null}
