@ConfigurationProperties的使用

具体可参考:
URL:https://hello.blog.csdn.net/article/details/104575745

如下是一种使用场景

当Bean被实例化时,@ConfigurationProperties会将对应前缀的后面的属性与Bean对象的属性匹配。符合条件则进行赋值。

配置文件内内容

test:
  test1: 'test1'
  test2:
    test3:
      test4: 'test4'
    test5: 'test5'
  test6:
    test7: 'test7'

ConfigurationProperties 使用时赋值用的对象实体类

package net.shop.test;


import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//将普通pojo实例化到spring容器 
@Component
//表示将配置文件中 前缀为test 的内容复制到改对象上
@ConfigurationProperties(prefix = "test")
public class CompanyProperties implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {

    }
    private String test1;
    private test2 test2 = new test2();
    private test6 test6 = new test6();

    public class test2 {
        private String test5;
        private test3 test3 = new test3();

        public class test3 {
            private String test4;

            public String getTest4() {
                return test4;
            }

            public void setTest4(String test4) {
                this.test4 = test4;
            }
        }

        public CompanyProperties.test2.test3 getTest3() {
            return test3;
        }

        public void setTest3(CompanyProperties.test2.test3 test3) {
            this.test3 = test3;
        }

        public String getTest5() {
            return test5;
        }

        public void setTest5(String test5) {
            this.test5 = test5;
        }
    }

    public class test6 {
        private String test7;

        public String getTest7() {
            return test7;
        }

        public void setTest7(String test7) {
            this.test7 = test7;
        }
    }

    public String getTest1() {
        return test1;
    }

    public void setTest1(String test1) {
        this.test1 = test1;
    }

    public CompanyProperties.test2 getTest2() {
        return test2;
    }

    public void setTest2(CompanyProperties.test2 test2) {
        this.test2 = test2;
    }

    public CompanyProperties.test6 getTest6() {
        return test6;
    }

    public void setTest6(CompanyProperties.test6 test6) {
        this.test6 = test6;
    }
}

使用方式

   @Autowired
    private CompanyProperties props;
    
    public void centerOrderImporting(){
        System.out.println(props.getTest1());
        System.out.println(props.getTest2().getTest3().getTest4());
        System.out.println(props.getTest2().getTest5());
        System.out.println(props.getTest6().getTest7());
}

输出内容:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值