当前位置:网站首页>获取配置文件properties中的数据
获取配置文件properties中的数据
2022-06-21 09:28:00 【凄戚】
通过类加载器来获取配置文件内容
环境
在开发中我们将经常用到的一些常量和一些繁琐的配置可以提取出来,方便我们的使用。这些数据经常保存在配置文件中
配置文件经常和工具类配合使用。当需要使用它们时通过查找配置文件或调用工具类方法来实现。
这些properties文件都是在src/main/resource 下直接存放,而不能再放到文件夹里,否则会找不到。
一般有两种类似的获取方式:
一、直接通过某个线程获取
Properties p = new Properties();
try {
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("identity.properties"));
} catch (IOException e) {
e.printStackTrace();
}
String wordTxt = p.getProperty("wordTxt").trim();
wordTxt是我们在对应的identity.properties中的参数。
二、通过类的类加载器获取
ClassLoader classLoader = JdbcUtils.class.getClassLoader();
//此处返回的URL是统一资源定位符
URL resource = classLoader.getResource("jdbc.properties");
String path = resource.getPath();
Properties prop = new Properties();
prop.load(new FileReader(path));
url = prop.getProperty("url");
user = prop.getProperty("user");
pwd = prop.getProperty("password");
总结
这两个模式的实现其实都是获取到了类加载器来加载资源,而类加载器的使用原理是这样的:当类加载请求到来时,先从缓存中查找该类对象,如果存在直接返回,如果不存在则交给该类加载去的父加载器去加载,倘若没有父加载则交给顶级启动类加载器去加载,最后倘若仍没有找到,则使用findClass()方法去加载(关于findClass()稍后会进一步介绍)。从loadClass实现也可以知道如果不想重新定义加载类的规则,也没有复杂的逻辑,只想在运行时加载自己指定的类,那么我们可以直接使用this.getClass().getClassLoder.getResource("className"),这样就可以直接调用ClassLoader的loadClass方法获取到class对象。
关于类加载器部分借鉴自深入理解类加载器
边栏推荐
- 115. secondary packaging of table components
- 【C】 [time operation] time operation in C language
- 【实战】STM32 FreeRTOS移植系列教程5:FreeRTOS消息队列
- Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
- [practice] stm32mp157 development tutorial FreeRTOS system 6: FreeRTOS list and list items
- 110. JS event loop and setimmediate, process.nexttick
- leetcode:19. 删除链表的倒数第 N 个结点
- The R language plot function visualizes multiple lines in the same plot, and uses the BMP function to save the visualization image to the BMP format file in the specified directory
- [JUC series] completionservice of executor framework
- R language ggplot2 visualization, draw two lines in the same ggplot2 graph in a graph, and use postscript function to save the visualization image to PS format file in the specified directory
猜你喜欢
![[practice] STM32 FreeRTOS porting series tutorial 1: use of FreeRTOS binary semaphores](/img/47/611640b817d3e1573c2cde25a9f2e5.jpg)
[practice] STM32 FreeRTOS porting series tutorial 1: use of FreeRTOS binary semaphores

Alibaba P6 employees came to a small company for an interview and asked for an annual salary increase of 500000 yuan. How dare you speak

【实战】STM32 FreeRTOS移植系列教程2:FreeRTOS 互斥信号量

stm32mp1 Cortex M4开发篇9:扩展板空气温湿度传感器控制

stm32mp1 Cortex M4开发篇10:扩展板数码管控制

stm32mp1 Cortex M4开发篇8:扩展板LED灯控制实验

It is only seven days since the commencement of construction in 2022. I left the outsourcing company

Wechat applet

2. the development of the meta universe

Stm32mp1 cortex M4 development part 10: expansion board nixie tube control
随机推荐
一条命令开启监控之旅!
Prefix sum and difference
1. is god horse a meta universe?
apk 反编译 上的填坑之路
Ali has been working for 8 years. This learning note is left when he reaches P8. He has helped his friends get 10 offers
Abstractqueuedsynchronizer (AQS) source code analysis - cyclicbarrier source code analysis
Source insight shortcut key cross reference
118. summary of basic knowledge of typescript (data type, interface, abstract class, inheritance, attribute encapsulation, modifier)
Stm32mp1 cortex M4 development part 10: expansion board nixie tube control
Stm32mp1 cortex M4 development part 13: external interrupt of expansion board key
An app developed based on retrotfit2.1+material design+ijkplayer
The spring recruitment is also terrible. Ali asked at the beginning of the interview: how to design a high concurrency system? I just split
R language uses the < - operator to create new variables, uses the attach function to bind data, and directly uses the two data column names to calculate mean value to make new feelings in D
Abstractqueuedsynchronizer (AQS) source code detailed analysis - condition queue process analysis
PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
[practice] stm32mp157 development tutorial FreeRTOS system 3: FreeRTOS counting semaphore
Introduction and template of segment tree Foundation (I)
The R language plot function visualizes multiple lines in the same plot, and uses the BMP function to save the visualization image to the BMP format file in the specified directory
stm32mp1 Cortex M4开发篇8:扩展板LED灯控制实验
【实战】STM32 FreeRTOS移植系列教程4:FreeRTOS 软件定时器