首页 > 编程语言 > 详细

SpringBoot配置绑定 ConfigurationProperties

时间:2021-05-04 14:21:17      阅读:39      评论:0      收藏:0      [点我收藏+]

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 org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "people", ignoreUnknownFields = false,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;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public String geteMail() {
        return eMail;
    }

    public void seteMail(String eMail) {
        this.eMail = eMail;
    }

    public List<People> getChildren() {
        return children;
    }

    public void setChildren(List<People> children) {
        this.children = children;
    }

    public Map<String, People> getOtherMap() {
        return otherMap;
    }

    public void setOtherMap(Map<String, People> otherMap) {
        this.otherMap = otherMap;
    }

    @Override
    public String toString() {
        return "People{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", addr=‘" + addr + ‘\‘‘ +
                ", eMail=‘" + eMail + ‘\‘‘ +
                ", children=" + children +
                ", otherMap=" + 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}

SpringBoot配置绑定 ConfigurationProperties

原文:https://www.cnblogs.com/reclusiveone/p/14729188.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!