SpringBoot配置绑定 ConfigurationProperties


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

  1. 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
    
    
  2. 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}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM