SpringBoot @ConfigurationProperties参数绑定 详解

3 篇文章 0 订阅
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());
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值