当前位置:   article > 正文

SpringBoot @ConfigurationProperties参数绑定 详解_@configproperties列表

@configproperties列表
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());
}


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/413345
推荐阅读
相关标签
  

闽ICP备14008679号