springboot 加载一个properties文件转换为对象、List和Map数据结构
springboot 加载一个properties文件转换为对象、List和Map数据结构
- 一、springboot 加载一个properties文件转换为对象
- 1.1、配置文件-blog.properties
- 1.2、Blog2Properties类
- 1.3、运行
- 二、springboot 加载一个properties文件转换为List
- 2.1、配置文件-cityCode.properties
- 2.2、CityCodeProperties类
- 2.3、运行
- 三、springboot 加载一个properties文件转换为Map
- 前言
- 1. 创建一个properties文件, properties文件内容:
- 2、在java中将该properties文件转换为map
- 3、注意事项
- 四、遇到的问题:@PropertySource 注解实现读取 yml 文件
- 4.1、配置文件-person.yml
- 4.2、PersonProperties类
- 4.3、MyPropertySourceFactory 类
- 4.4、运行
- 五、参考链接
一、springboot 加载一个properties文件转换为对象
1.1、配置文件-blog.properties
com.chenheng.blog.name=陈大大
com.chenheng.blog.title=Spring Boot学习教程
com.chenheng.blog.desc=${com.chenheng.blog.name}正在努力写《${com.chenheng.blog.title}》
# 随机字符串
com.chenheng.blog.value=${random.value}
# 随机int
com.chenheng.blog.number=${random.int}
# 随机long
com.chenheng.blog.bignumber=${random.long}
# 10以内的随机数
com.chenheng.blog.test1=${random.int(10)}
# 10-20的随机数
com.chenheng.blog.test2=${random.int[10,20]}
1.2、Blog2Properties类
package com.chenheng.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/*** @author chenheng* @date 2022/4/30 21:17*/
@PropertySource(value={"classpath:config/blog.properties"})
@ConfigurationProperties(prefix = "com.chenheng.blog")
@Component
@Data
public class Blog2Properties {private String name;private String title;private String desc;private String value;private String number;private String bignumber;private String test1;private String test2;
}
1.3、运行
package com.chenheng;import com.chenheng.properties.Blog2Properties;
import com.chenheng.properties.BlogProperties;
import com.chenheng.properties.PersonProperties;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/*** @author chenheng* @date 2022/4/30 20:49*/
@SpringBootTest
public class SpringBootTest01 {@Autowiredprivate Blog2Properties blog2Properties;@Testvoid testProperties02(){System.out.println(blog2Properties);}
}
二、springboot 加载一个properties文件转换为List
2.1、配置文件-cityCode.properties
#List properties
com.city.code.list[0]=www
com.city.code.list[1]=localhost
com.city.code.list[2]=wuhan
com.city.code.list[3]=tianjin
#Map Properties
com.city.code.map.www=4201
com.city.code.map.wuhan=4201
com.city.code.map.tianjin=1200
2.2、CityCodeProperties类
package com.chenheng.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** @author chenheng* @date 2022/5/1 14:39*/
@PropertySource(value={"classpath:config/cityCode.properties"})
@ConfigurationProperties(prefix = "com.city.code")
@Component
@Data
public class CityCodeProperties {private List<String> list = new ArrayList<>();private Map<String, String> map = new HashMap<>();
}
2.3、运行
package com.chenheng;import com.chenheng.properties.Blog2Properties;
import com.chenheng.properties.BlogProperties;
import com.chenheng.properties.CityCodeProperties;
import com.chenheng.properties.PersonProperties;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;
import java.util.Map;/*** @author chenheng* @date 2022/4/30 20:49*/
@SpringBootTest
public class SpringBootTest01 {@Autowiredprivate CityCodeProperties cityCodeProperties;@Testvoid testCityCodeProperties(){List<String> list = cityCodeProperties.getList();Map<String, String> map = cityCodeProperties.getMap();System.out.println(list + "->" + map);}
}
三、springboot 加载一个properties文件转换为Map
前言
springboot中比较常见的获取properties中的值,就是直接在字段上面添加@Value的属性.
但有时候我们不确定key有多少, 但会有一定的规律(这个规律是根据业务来定的,如下), 这时候我们就可以考虑将properties中的信息转换为一个map, 然后根据key的规律操作响应的数据
1. 创建一个properties文件, properties文件内容:
名称为
customExportFields.properties
文件路径为
内容为
#15-排名
custom.customData.15=rank
#17-店铺ID
custom.customData.17=shopId
2、在java中将该properties文件转换为map
package com.xxx.config;import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;// lombok专用注解
@Data
// 指定配置文件
@PropertySource("classpath:config/customExportFields.properties")
// custom指的是customExportFields.properties中前缀
@ConfigurationProperties(prefix = "custom")
@Component
public class CustomExportPropertiesConfig {// prefix的值+customData变量名为properties key的前一部分, 将key剩余的部分作为map的key, value作为map的valuepublic Map<String, String> customData = new HashMap<>();
}
3、注意事项
customExportFields.properties文件中
custom.customData.15=rank
对象JavaConfig类中
属性文件中的custom是@ConfigurationProperties(prefix = "custom")对应的前缀名称
属性文件中的customData即是Map<String, String> customData = new HashMap<>()中customData集合的变量名,这一点一定要注意,不然找不到值
属性文件中的15即是customData集合中的key,而rank是customData集合中15这个key所对应的value值
四、遇到的问题:@PropertySource 注解实现读取 yml 文件
4.1、配置文件-person.yml
person:name: lucyage: 16addr: 北京books: [java,kafka,netty]course-map-teacher: {key1: v1,key2: v2}dog:name: ${person.name}age: ${random.int(5)} #表达式
4.2、PersonProperties类
package com.chenheng.properties;import factory.MyPropertySourceFactory;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;/*** @author chenheng* @date 2022/4/30 22:19* @PropertySource 默认的无法读取yml文件,需要写一个类即MyPropertySourceFactory* https://juejin.cn/post/6844903768308334606*/
@PropertySource(value={"classpath:config/person.yml"},encoding = "utf-8",factory = MyPropertySourceFactory.class)
@ConfigurationProperties(prefix = "person")
@Component
@Data
public class PersonProperties {private String name;private int age;private String addr;private List<String> books;private Map<String, String> courseMapTeacher;private Dog dog;
}
4.3、MyPropertySourceFactory 类
在实际的开发中使用 @PropertySource 注解无法加载 yml 配置文件问题,需要继承DefaultPropertySourceFactory类,重写createPropertySource方法
package factory;import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;import java.io.IOException;
import java.util.List;/*** @author chenheng* @date 2022/4/30 22:34*/
public class MyPropertySourceFactory extends DefaultPropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {if (resource == null){return super.createPropertySource(name, resource);}List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());return sources.get(0);}
}
4.4、运行
package com.chenheng;import com.chenheng.properties.Blog2Properties;
import com.chenheng.properties.BlogProperties;
import com.chenheng.properties.CityCodeProperties;
import com.chenheng.properties.PersonProperties;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;
import java.util.Map;/*** @author chenheng* @date 2022/4/30 20:49*/
@SpringBootTest
public class SpringBootTest01 {@Autowiredprivate PersonProperties personProperties;@Testvoid testPersonProperties(){System.out.println(personProperties);}
}
五、参考链接
1、springboot 加载一个properties文件转换为map
2、Springboot读取properties配置文件的List、Map格式的配置项(属性)
3、SpringBoot 从配置文件读取值到对象中
4、@PropertySource 注解实现读取 yml 文件
5、@PropertySource 解析 yml 配置文件,自定义解析 yaml 工厂类
6、@PropertySource 注解的使用
springboot 加载一个properties文件转换为对象、List和Map数据结构相关推荐
- 手写简版spring --5--资源加载器解析文件注册对象
一.目标 在完成 Spring 的框架雏形后,现在我们可以通过单元测试进行手动操作Bean对象的定义.注册和属性填充,以及最终获取对象调用方法.但这里会有一个问题,就是如果实际使用这个 Spring ...
- Spring框架——加载属性(properties)文件
使用属性文件的好处 有效的减少硬编码(将配置信息直接写入Java代码中) 当应用程序的运行环境发生改变时,只需要修改属性文件,而不需要改变源码.提高了运维人员操作的便利性 加载属性文件的方式 使用注解 ...
- 解决Idea中maven项目druid连接池加载druid.properties文件时报 inStream parameter is null异常(原因有待考证)
我认为应该是没有把druid.properties文件加载到输入流中. 原代码是 InputStream is = JDBCUtils.class.getClassLoader().getResour ...
- 加密Spring加载的Properties文件
目标:要加密spring的jdbc配置文件的密码口令. 实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密. 1.扩展 package com.lavasoft.freamwork. ...
- 如何在HTML中加载一个CSS文件?
可以通过link标签加载: <link rel="stylesheet" href="wcss.css" type="text/css" ...
- 在中WebBrowser加载Excel后获取excel对象
在最新的Visual Studio 2005中,为我们提供了一个WebBrowser的封装控件,这个控件隐藏了底层的axWebBrowser控件,我们就利用WebBrowser控件来完成我们的Exce ...
- html引用单文件组件,webpack入坑之旅(五)加载vue单文件组件_html/css_WEB-ITnose
需要什么? 在经过前面的四个练习,相信已经对于 webapck有了一定的了解,现在我们就来一个综合案例,进一步甲申年对于 webpack的理解. 首先我们应该思考要解析 .vue类型的文件,需要什么样 ...
- springboot mybatis 热加载mapper.xml文件(最简单)
大家好,我是烤鸭: 今天介绍一下springboot mybatis 热加载mapper.xml文件. 本来不打算写的,看到网上比较流行的方式都比较麻烦,想着简化一下. 网上流行的版本. https: ...
- SpringBoot 加载不出来application.yml文件
摘要 记录一次SpringBoot加载不出来application.yml文件的问题解决过程 问题 配置了application.yml文件,但是映射到properties bean的时候失败 @Co ...
最新文章
- Junit中error和failure区别
- nameof() 到底是编译时还是运行时行为?
- 转:json与map互转
- id门禁卡复制到手机_手机NFC有哪些功能?怎么设置手机门禁卡?别浪费了手机的NFC功能...
- IE自动弹出窗口(JS/TrojanDownloader.Iframe.NDR 木马查杀)故障解决
- css中hover的妙用!!
- Android学习笔记(九)——Activity的跳转和数据传递
- 40岁了,突然公司黄了,怎么办?
- django导入mysql_django如何直接对数据库进行插入操作?
- 去年互联网普及率首次过半 42.7%网民遭遇过网络安全问题
- linux 安装mysql 5.7.16
- vue axios 接口封装
- java mysql查询试题_2016年Java认证考试题
- 1004. 成绩排名 (20)
- VS2008 SP1 安装卡在 VS90sp1-KB945140-X86-CHS的解决方法
- zabbix监控WEB页面及告警 实战
- 用c语言编写6位计算器,用c语言编写易简计算器.doc
- ad16自动布线设置规则_AD16快捷方式和常规设置
- 第一章 行列式(知识点部分)
- 关于软件研发生产力的误区与思考
热门文章
- 安装思源笔记/siyuan插件pandoc
- CentOS8下载及设置安装源(最新设置)
- 6.2 病毒机制与组成结构
- 阿里视频直播自定义推拉流地址生成
- Android之自定义动画框架实现ScrollView滚动动画总结(雷惊风)
- Android仿微信朋友圈查看全文/收起功能(雷惊风)
- php 发 语音验证码,php语音验证码接口_php语音接口_php语音验证码_语音验证码代码示例_达信通...
- 量化交易6-backtrader编写策略的时数据获取
- Python练习:炉石传说荣誉室返尘最优策略
- 对拍--from Altf4