spring boot 读取配置文件(application.yml)中的属性值
在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值:
1、引入依赖:
- <!-- 支持 @ConfigurationProperties 注解 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
2、配置文件(application.yml)中配置各个属性的值:
- myProps: #自定义的属性和值
- simpleProp: simplePropValue
- arrayProps: 1,2,3,4,5
- listProp1:
- - name: abc
- value: abcValue
- - name: efg
- value: efgValue
- listProp2:
- - config2Value1
- - config2Vavlue2
- mapProps:
- key1: value1
- key2: value2
3、创建一个bean来接收配置信息:
- @Component
- @ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的属性
- public class MyProps {
- private String simpleProp;
- private String[] arrayProps;
- private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值
- private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值
- private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值
- public String getSimpleProp() {
- return simpleProp;
- }
- //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
- public void setSimpleProp(String simpleProp) {
- this.simpleProp = simpleProp;
- }
- public List<Map<String, String>> getListProp1() {
- return listProp1;
- }
- public List<String> getListProp2() {
- return listProp2;
- }
- public String[] getArrayProps() {
- return arrayProps;
- }
- public void setArrayProps(String[] arrayProps) {
- this.arrayProps = arrayProps;
- }
- public Map<String, String> getMapProps() {
- return mapProps;
- }
- public void setMapProps(Map<String, String> mapProps) {
- this.mapProps = mapProps;
- }
- }
启动后,这个bean里面的属性就会自动接收配置的值了。
4、单元测试用例:
- @Autowired
- private MyProps myProps;
- @Test
- public void propsTest() throws JsonProcessingException {
- System.out.println("simpleProp: " + myProps.getSimpleProp());
- System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps()));
- System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1()));
- System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2()));
- System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps()));
- }
测试结果:
- simpleProp: simplePropValue
- arrayProps: ["1","2","3","4","5"]
- listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]
- listProp2: ["config2Value1","config2Vavlue2"]
- mapProps: {"key1":"value1","key2":"value2"}
spring boot 读取配置文件(application.yml)中的属性值的更多相关文章
- Spring Boot 的配置文件application.properties
Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...
- 一:Spring Boot 的配置文件 application.properties
Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...
- 是时候搞清楚 Spring Boot 的配置文件 application.properties 了!
在 Spring Boot 中,配置文件有两种不同的格式,一个是 properties ,另一个是 yaml . 虽然 properties 文件比较常见,但是相对于 properties 而言,ya ...
- spring boot读取配置文件
一.springboot配置文件 核心配置文件和自定义配置文件.核心配置文件是指在resources根目录下的application.properties或application.yml配置文 ...
- 第二篇:彻底搞清楚 Spring Boot 的配置文件 application.properties
前言 在Spring Boot中,配置文件有两种不同的格式,一个是properties,另一个是yaml. 虽然properties文件比较常见,但是相对于properties而言,yaml更加简洁明 ...
- IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示
在使用spring boot开发过程中,经常会定义一些应用自己的属性,直接写到application配置文件中使用@Value注解进行使用,这样使用也没有什么问题.不过我认为更优雅的方式是定义自己的属 ...
- spring boot 读取配置文件信息
1.读取application.properties @Component @ConfigurationProperties(prefix="images.product.column&qu ...
- Spring Boot读取配置文件的几种方式
Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...
- springboot工程读取配置文件application.yml的写法
现在流行springboot框架的项目,里面的默认配置文件为application.yml,我们怎样读取这个配置文件呢? 先贴上我得配置文件吧 目录结构 里面内容 1 写读取配置文件的工具类 @Con ...
随机推荐
- gcc学习记录
-Wall: 使输出中包含警告信息,提示一些可以避免的错误.如果没有错误,则不会输出信息. -o:后面加上可执行文件的名字.如果不加-o选项,会默认生成a.out可执行文件.举例:gcc -Wall ...
- 项目记事【StreamAPI】:使用 StreamAPI 简化对 Collection 的操作
最近项目里有这么一段代码,我在做 code-review 的时候,觉得可以使用 Java8 StreamAPI 简化一下. 这里先看一下代码(不是源码,一些敏感信息被我用其他类替代了): privat ...
- [HDU3516] Tree Construction [四边形不等式dp]
题面: 传送门 思路: 这道题有个结论: 把两棵树$\left[i,k\right]$以及$\left[k+1,j\right]$连接起来的最小花费是$x\left[k+1\right]-x\left ...
- 字符串函数 (strfun)
字符串函数 (strfun) 题目描述 两个等长的由大写英文字母构成的字符串a和b,从a中选择连续子串x,从b中选出连续子串y.子串x与子串y的长度相等. 定义函数f(x,y)为满足条件xi=yi(1 ...
- easyUI 接收Spring Mvc中@ResponseBody中文乱码解决
接触springMVC不够深入,乱码困扰我到深夜,特此留下记忆: @responsebody默认滴是ISO-8859-1 Controller注解参数 @ResponseBody 标注后返回Strin ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat 团体天梯赛 L2-010. 排座位
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- 【虚拟机】主机与VMware虚拟机通信(XP版)(转)
一.与主机共享ADSL链接/无线网络(虚拟机内可上网) 安装虚拟机后,在网络链接下除了本地链接外,会出现两个新的链接,分别是VMware Network Adapter VMnet1和VMware N ...
- java中 文件压缩处理
public static void main(String[] args) throws IOException { File file=new File("./mhxx_configs. ...
- QUICK START GIT
install git at you laptop https://git-scm.com/downloads config git at you laptop git config --global ...