Java 读取application.properties配置文件中配置
实际开发中若需要读取配置文件application.properties中的配置,代码如下。例:读取配置文件中name属性配置值:

代码如下:
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils; import java.util.Properties; public class Test { /**
* 通过配置文件名读取内容
* @param fileName
* @return
*/
public static Properties readPropertiesFile(String fileName) {
try {
Resource resource = new ClassPathResource(fileName);
Properties props = PropertiesLoaderUtils.loadProperties(resource);
return props;
} catch (Exception e) {
System.out.println("————读取配置文件:" + fileName + "出现异常,读取失败————");
e.printStackTrace();
}
return null;
} public static void main(String[] args) {
Properties properties = readPropertiesFile("application.properties");
System.out.println(properties.getProperty("name"));
}
}
执行结果:

若使用上述方法读取出现中文乱码时,说明编码格式不一致,可使用下面可设置编码格式方法读取:
/**
* 通过配置文件名读取内容
* @param fileName
* @return
*/
public Properties readPropertiesFile(String fileName) {
Properties properties = new Properties();
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream(fileName);
try {
properties.load(new InputStreamReader(inputStream, "UTF-8"));
return properties;
} catch (Exception e) {
logger.info("————读取配置文件:" + fileName + "出现异常,读取失败————");
e.printStackTrace();
}
return null;
}
Java 读取application.properties配置文件中配置的更多相关文章
- Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。
Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可.
- springboot项目logback.xml或者logback-spring.xml中读取不到application.yml或application.properties配置文件中的配置解决办法
在springboot项目中我们可能想要实现不同环境的日志项目配置不同,比如我想让不同环境的日志路径不同. 这时候我们很容易想: 1.到将日志路径配置在springboot的:application- ...
- SpringBoot在logback.xml中读取application.properties中配置的日志路径
1.在springboot项目中使用logback记录日志,在logback.xml中配置日志存储位置时读取application.properties中配置的路径,在 logback.xml中配置引 ...
- 使用Properties配置文件进行配置读取
#使用Properties配置文件进行配置读取: 例如:有一个配置文件的内容如下: # setting.properties last_open_file=/data/hello.txt auto_s ...
- SpringBoot配置文件 application.properties,yaml配置
SpringBoot配置文件 application.properties,yaml配置 1.Spring Boot 的配置文件 application.properties 1.1 位置问题 1.2 ...
- Java 获取*.properties配置文件中的内容 ,常见的两种方法
import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...
- SpringBoot读取application.properties文件
http://blog.csdn.net/cloume/article/details/52538626 Spring Boot中使用自定义的properties Spring Boot的applic ...
- Spring Boot加载application.properties配置文件顺序规则
SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...
- 日志配置文件读取spring boot配置文件中的属性
如果是读取 application.properties 这种spring boot的默认配置文件时 其中 scope固定为context 指明从上下文中获取, name 根据自己的意思给, sou ...
随机推荐
- html页面中引入html
我们写页面通常会遇到这种情况,一个模块很多页面都用到,那么我们为了方便就会单独写到一个页面,然后引入进去,我知道的有三种: 1.用标签<iframe></iframe> 例: ...
- windows如何正确下载补丁包
今天公司让给windows安装补丁,打开链接,我蒙蔽了,这么多包要下载哪个腻?下面来跟杨老师一起学习一下如何确定windows版本,下载正确的补丁包. 首先先看一下下载补丁的页面,懵~~ 登录你需要安 ...
- SNMP命令
snmp命令 配置管理网络协议Weblogic项目管理Cisco Snmputil 命令 Snmputil是一个命令行下的软件,使用语法如下: usage: snmputil get|getnext| ...
- C++入门经典-例6.19-字符串类型之修改string字符串的单个字符串
1:头文件 #include <string> 声明一个string变量,形式如下: std::string s; 初始化string类型的变量: std::string s1(" ...
- 移动端 iphone手机在中文情况下不执行keyup事件
问题:移动端 在
- LeetCode 60. 第k个排列(Permutation Sequence)
题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "1 ...
- HTML功能框架
起始预定义函数 function $(obj) { return document.getElementById(obj); } 1.用户登陆框架 <!DOCTYPE html> < ...
- Linux高级调试与优化——Address Sanitizer
Address Sanitizer ASAN最早可以追溯到 LLVM 的 sanitizers项目(https://github.com/google/sanitizers),这个项目包含了Addre ...
- python源码
初学者 GitHub - kennethreitz/pip-pop: Tools for managing requirements files. GitHub - kennethreitz/envo ...
- javascript - 事件详解(阻止事件冒泡+阻止事件行为)
一.事件流 1.事件流 描述的是在页面中接受事件的顺序 2.事件冒泡 由最具体的元素接收,然后逐级向上传播至最不具体的元素的节点 (最具体 –> 最不具体) 3.事件捕获 最不具体的节点先接收事 ...