SpringBoot @ConfigurationProperties参数绑定

1> 引入 spring-boot-configuration-processor 库

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-configuration-processor</artifactId>

    <optional>true</optional>

</dependency>

2> 在 application.yml 中要获取的数据

myprops: # 自定义的属性和值

  simple-prop: simplePropValue

  array-props: 1,2,3,4,5

  list-prop1:

    - name: abc

      value: abcValue

    - name: efg

      value: efgValue

  list-prop2:

    - config2Value1

    - config2Vavlue2

  map-props:

    key1: value1

    key2: value2

注 : 1> 注意命名规则,根节点不能采用驼峰命名法,否则将会导致识别失败这是在 2.0.1.RELEASE 出现的问题,最好全部小写,剩余的其他字段可以采用驼峰命名法,可以使用 - 方式来修改

       2> 注意查看日志中的提示信息

3> 使用 @ConfigurationProperties 将数据绑定到对象上有两种方式

方式1> 在方法上引入 @ConfigurationProperties 注解

public class MyProps {

    private String simpleProp;

    private String[] arrayProps;

    /** 接收prop1里面的属性值 */

    private List<Map<String, String>> listProp1 = new ArrayList<>();

    /** 接收prop2里面的属性值 */

    private List<String> listProp2 = new ArrayList<>();

    /** 接收prop1里面的属性值 */

    private Map<String, String> mapProps = new HashMap<>();

    public String getSimpleProp() {

        return simpleProp;

    }

    /**

     * String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要

     * @param simpleProp

     */

    public void setSimpleProp(String simpleProp) {

        this.simpleProp = simpleProp;

    }

    public String[] getArrayProps() {

        return arrayProps;

    }

    public void setArrayProps(String[] arrayProps) {

        this.arrayProps = arrayProps;

    }

    public List<Map<String, String>> getListProp1() {

        return listProp1;

    }

    public void setListProp1(List<Map<String, String>> listProp1) {

        this.listProp1 = listProp1;

    }

    public List<String> getListProp2() {

        return listProp2;

    }

    public void setListProp2(List<String> listProp2) {

        this.listProp2 = listProp2;

    }

    public Map<String, String> getMapProps() {

        return mapProps;

    }

    public void setMapProps(Map<String, String> mapProps) {

        this.mapProps = mapProps;

    }

}

# 使用在方法上

@Bean

@ConfigurationProperties(prefix = "myprops")

public MyProps myProps() {

    return new MyProps();

}

方式2> 在类上使用 @ConfigurationProperties 注解

@Component

@ConfigurationProperties(prefix = "myprops")

public class MyProps {

    private String simpleProp;

    private String[] arrayProps;

    /** 接收prop1里面的属性值 */

    private List<Map<String, String>> listProp1 = new ArrayList<>();

    /** 接收prop2里面的属性值 */

    private List<String> listProp2 = new ArrayList<>();

    /** 接收prop1里面的属性值 */

    private Map<String, String> mapProps = new HashMap<>();

    public String getSimpleProp() {

        return simpleProp;

    }

   /**

    * String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要

    *

    * @param simpleProp

    */

    public void setSimpleProp(String simpleProp) {

        this.simpleProp = simpleProp;

    }

    public String[] getArrayProps() {

        return arrayProps;

    }

    public void setArrayProps(String[] arrayProps) {

        this.arrayProps = arrayProps;

    }

    public List<Map<String, String>> getListProp1() {

        return listProp1;

    }

    public void setListProp1(List<Map<String, String>> listProp1) {

        this.listProp1 = listProp1;

    }

    public List<String> getListProp2() {

        return listProp2;

    }

    public void setListProp2(List<String> listProp2) {

        this.listProp2 = listProp2;

    }

    public Map<String, String> getMapProps() {

        return mapProps;

    }

    public void setMapProps(Map<String, String> mapProps) {

        this.mapProps = mapProps;

    }

}

以上两种方法,任意一种均可,之后就可以直接使用

@Autowired

private MyProps myProps;

@Test

public void propsTest() {

    System.out.println("simpleProp: " + myProps.getSimpleProp());

}

猜你喜欢

转载自blog.csdn.net/hhb910/article/details/82683687