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 ...
随机推荐
- @Data注解使用后get set报错解决方法
Maven项目中已经导入相关的lombok.jar包但是使用后仍提示无set/get方法 .在idea中安装如下插件,安装后重启idea可用不报错. 转载于:https://www.cnblogs.c ...
- Python内置类属性
__dict__ : 类的属性(包含一个字典,由类的数据属性组成) __doc__ :类的文档字符串 __name__: 类名 __module__: 类定义所在的模块(类的全名是'__main__. ...
- c# 使用Split分割 换行符
c# 使用Split分割 换行符,方法如下(其余方法有空再添加): string str = "aa" + "\r\n" + "bb"; s ...
- LOJ #2718. 「NOI2018」归程 Dijkstra+可持久化并查集
把 $Noi2018$ day1t1 想出来还是挺开心的,虽然是一道水题~ 预处理出来 1 号点到其它点的最短路,然后预处理边权从大到小排序后加入前 $i$ 个边的并查集. 这个并查集用可持久化线段树 ...
- 哈密尔顿环x
欧拉回路是指不重复地走过所有路径的回路,而哈密尔顿环是指不重复地走过所有的点,并且最后还能回到起点的回路. 代码如下: #include<iostream> #include<cs ...
- jQuery_插入操作
jQuery的插入方法有很多,有内部插入,也有外部插入,每个插入方式里面还有很多种,本文一一介绍,注释在代码里,直接上代码: 代码: <!DOCTYPE html> <html> ...
- Apicloud_(项目)网上书城01_前端搭建
[本文皆在记录自己开发Apicloud项目过程,不具备教学水平性文章] 参考书籍<30天App开发从0到1> Apicloud_(项目)网上书城01_前端页面开发 传送门 Apicloud ...
- 3分割线左右间距(LinearLayoutManager.VERTICAL)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- C++入门经典-例6.4-输出字符数组中的内容
1:代码如下: // 6.4.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using ...
- 第3周Java编程总结
1.打印输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个“水仙花数”. 2. 编写Java程序,求13-23+33-43+…+973-983+ ...