SpringBoot的@Value读取properties配置用法示例

  • properties配置文件

    #键值对
    cc.catface.k1=v1
    
    #自定义@Configuration和读取list
    cc.catface.names.list[0]=nameA
    cc.catface.names.list[1]=nameB
    cc.catface.names.list[2]=nameC
    
    #数组
    cc.catface.l1.list=a1,a2,a3,a4
    
    #list
    cc.catface.l2.list=a1,a2,a3,a4,a5
    
    #set
    cc.catface.set=a1,a2,a3,a4,a5,a1
    
    #map
    cc.catface.maps='{"name":"akali","age":"20"}'
    
  • java读取示例

    完整的读取示例

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    @SpringBootTest
    class DemoApplicationTests {
    
    
        //读取键值对
        @Value("${cc.catface.k1}")
        private String v1;
    
    
        //:[配置文件无该key时赋默认值]
        @Value("${noKey:defaultValue}")
        private String noKey;
    
    
        //IllegalArgumentException: Could not resolve placeholder 'names.list' in value "${names.list}"
        /*@Value("${cc.catface.names.list}")
        private List<String> names;*/
        //通过自定义@Configuration和@ConfigurationProperties类可读取list但比较麻烦基本不用该方式
        @Autowired
        private TestListConfig testListConfig;
    
    
        //读取数组[使用:优化]
        //@Value("${cc.catface.l1.list}")
        @Value("${cc.catface.l1.list:}")
        private String[] list1;
    
    
        //使用el表达式将数组读取成list[使用:优化]
        //@Value("#'${cc.catface.l2.list}'.split(',')")
        //@Value("#{'${cc.catface.l2.list:}'.split(',')}")
        @Value("#{'${cc.catface.l2.list}'.empty ? null : '${cc.catface.l2.list:}'.split(',')}")
        private List<String> list2;
    
        //使用el表达式将数组读取成set[自动去重]
        @Value("#{'${cc.catface.set}'.empty ? null : '${cc.catface.set:}'.split(',')}")
        private Set<String> set;
    
        //使用fastjson自定义解析类读取map
        @Value("#{T(com.example.demo.MapDecoder).decodeMap(${cc.catface.maps:})}")
        private Map<String, String> maps;
    
        @Test
        void contextLoads() {
            System.out.println("普通读取-${cc.catface.k1}, v1:" + v1);
    
            System.out.println("普通读取一个没有的键值对:" + noKey);
    
            System.out.println("通过自定义@Configuration&@ConfigurationProperties读取list, size:" + testListConfig.getList().size());
    
            System.out.println("读取数组-${cc.catface.l1.list:}:" + list1.length + ", 第一个元素值:" + list1[0]);
    
            if (null == list2) {
                System.out.println("null == list2");
            } else {
                System.out.println("读取list, size:" + list2.size() + ", 第一个元素值:" + list2.get(0));
            }
    
            System.out.println("读取set, size:" + set.size());
    
            System.out.println("读取map, size:" + maps.size());
        }
    }
    

    这种通过自定义类读取list的方式基本不用的,用el表达式读取

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.List;
    
    @Configuration
    @ConfigurationProperties(value = "cc.catface.names")
    public class TestListConfig {
    
        private List<String> list;
    
        public List<String> getList() {
            return list;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    }
    

    使用fastjson自定义的配置文件中的map解析类

    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.TypeReference;
    
    import java.util.Map;
    
    public class MapDecoder {
        public static Map<String, String> decodeMap(String value) {
            try {
                return JSONObject.parseObject(value, new TypeReference<Map<String, String>>() {});
            } catch (Exception e) {
                return null;
            }
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值