型別安全配置:@ConfigurationProperties
阿新 • • 發佈:2018-12-22
@ConfigurationProperties可以看作@Value的升級版,通過該註解可將配置檔案中的屬性和一個Bean關聯,從而實現型別安全的配置
一、新增依賴包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
二、配置檔案設定屬性(application.yml)
author:
name: 猴子哥哥
age: 28
注:application.properties中容易中文亂碼問題
三、配置Bean
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "author") public class AuthorConfig { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { final StringBuilder sb = new StringBuilder("AuthorConfig{"); sb.append("\"name\":\"") .append(name).append('\"'); sb.append(",\"age\":") .append(age); sb.append('}'); return sb.toString(); } }
四、註解原始碼及說明
package org.springframework.boot.context.properties; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor; /** * Annotation for externalized configuration. Add this to a class definition or a * {@code @Bean} method in a {@code @Configuration} class if you want to bind and validate * some external Properties (e.g. from a .properties file). * <p> * Note that contrary to {@code @Value}, SpEL expressions are not evaluated since property * values are externalized. * * @author Dave Syer * @see ConfigurationPropertiesBindingPostProcessor * @see EnableConfigurationProperties */ @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ConfigurationProperties { /** * The name prefix of the properties that are valid to bind to this object. Synonym * for {@link #prefix()}. A valid prefix is defined by one or more words separated * with dots (e.g. {@code "acme.system.feature"}). * @return the name prefix of the properties to bind */ @AliasFor("prefix") String value() default ""; /** * The name prefix of the properties that are valid to bind to this object. Synonym * for {@link #value()}. A valid prefix is defined by one or more words separated with * dots (e.g. {@code "acme.system.feature"}). * @return the name prefix of the properties to bind */ @AliasFor("value") String prefix() default ""; /** * Flag to indicate that when binding to this object invalid fields should be ignored. * Invalid means invalid according to the binder that is used, and usually this means * fields of the wrong type (or that cannot be coerced into the correct type). * @return the flag value (default false) */ boolean ignoreInvalidFields() default false; /** * Flag to indicate that when binding to this object unknown fields should be ignored. * An unknown field could be a sign of a mistake in the Properties. * @return the flag value (default true) */ boolean ignoreUnknownFields() default true; }
1)prefix=value:配置檔案中的字首
2)ignoreInvalidFields:是否忽略型別不一致的屬性
3)ignoreUnknownFields:是否忽略屬性名稱對應不上的屬性