-
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; } } }
SpringBoot的@Value读取properties配置用法示例
于 2021-06-29 15:41:53 首次发布