java springboot 读取自定义配置文件
java springboot 读取自定义配置文件
application.properties、test.properties
maven中引用 :
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.6</version>
</dependency> <dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
在“src\main\resources”中增加一个test.properties文件,内容如下:
database.url=222
database.username=333
database.password=uuu
在Controller中使用:
package com.sixun.scnxsign; import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.io.File; @RestController
public class TestController { @RequestMapping("/test3")
public String test3() throws Exception{ StringBuilder scLog=new StringBuilder();
// Configurations 类和 springboot 的类有冲突,这里写全包名 org.apache.commons.configuration2.builder.fluent.Configurations
org.apache.commons.configuration2.builder.fluent.Configurations configs = new org.apache.commons.configuration2.builder.fluent.Configurations();
try { String appRootDir = System.getProperty("user.dir");
scLog.append("\r\n");
scLog.append(" appRootDir: " + appRootDir); File fi=new File("test.properties");
scLog.append("\r\n");
scLog.append(" fi.getAbsolutePath: " + fi.getAbsolutePath());
//scLog.append("fi.getParent: " + fi.getParent()); // 加载 INI 配置文件
Configuration ini = configs.properties(new File("test.properties")); // 获取数据库连接 URL
String url = ini.getString("database.url");
System.out.println("Database URL: " + url); scLog.append("\r\n");
scLog.append(" Database URL: " + url);
// 获取数据库用户名和密码
String username = ini.getString("database.username");
String password = ini.getString("database.password");
System.out.println("Database Username: " + username);
System.out.println("Database Password: " + password); scLog.append("\r\n");
scLog.append(" Database Username: " + username); scLog.append("\r\n");
scLog.append(" Database Password: " + password); } catch (ConfigurationException e) {
e.printStackTrace();
} return "Hello Spring Boot 1" +scLog.toString();
}
}
注意:Configurations 类和 springboot 的类有冲突,这里写全包名 org.apache.commons.configuration2.builder.fluent.Configurations
页面输出:
Hello Spring Boot 1 appRootDir: D:\java-proj\读取INI测试\scnxsign fi.getAbsolutePath: D:\java-proj\读取INI测试\scnxsign\test.properties Database URL: 222 Database Username: 333 Database Password: uuu
调试时,直接修改 resources\test.properties 文件。
发布时,把 test.properties 和你的 .jar 文本放同一目录即可,你可以实时修改 test.properties 里的内容 。

end
java springboot 读取自定义配置文件的更多相关文章
- Springboot读取自定义配置文件的几种方法
一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
- SpringBoot读取自定义配置文件
自定义配置文件 my-config.properties bfxy.title=bfxy bfxy.name=hello spring boot! bfxy.attr=12345 配置文件类 appc ...
- springboot读取自定义配置文件节点
今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...
- Springboot 之 自定义配置文件及读取配置文件
本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...
- Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)
注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以 错误的.不能读取的例子: mySet .ABAP_AS_POOLED = ABAP_AS_WITH_P ...
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...
- SpringBoot读取外部配置文件的方法
SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...
- springboot读取自定义properties配置文件方法
1. 添加pom.xml依赖 <!-- springboot configuration依赖 --> <dependency> <groupId>org.sprin ...
- springboot下自定义配置文件,并在项目里读取的方法
首先 pom文件引入springboot文件处理器 <dependency> <groupId>org.springframework.boot</groupId> ...
- springboot读取外部配置文件
springboot项目打成jar包后不好进行配置文件修改,可设置为读取外部配置文件,方便进行配置修改. 步骤: 1.将jar包中的application.properties配置文件复制到自定义路径 ...
随机推荐
- [Py] Jupyter 写入和执行 python 文件
以 %%writefile request.py 开头. 下面写 python 代码,然后 shift + enter 键,可以把 python 代码写入开头指定的文件中,没有则自动创建. 以 %ru ...
- dotnet 将任意时区的 DateTimeOffset 转换为中国时区时间文本
本文告诉大家在拿到任意时区的 DateTimeOffset 对象,将 DateTimeOffset 转换为使用中国的 +8 时区表示的时间 在开始之前,需要说明的是,采用 DateTimeOffset ...
- dotnet Roslyn 通过读取 suo 文件获取解决方案的启动项目
本文来告诉大家一个黑科技,通过 .suo 文件读取 VisualStudio 的启动项目.在 sln 项目里面,都会生成对应的 suo 文件,这个文件是 OLE 格式的文件,文件的格式没有公开,本文的 ...
- C++编程英语词汇
abstract抽象的 abstraction抽象性.抽象件 access访问 access level访问级别 access function访问函数 adapter适配器 address地址 ad ...
- AtCoder赛后反思
先贴上本人主页 ABC347 \(\color{blue}1624\color{red}-24\color{black}=\color{blue}1600\) 蓝名保卫战,极限 1600 C 题还是有 ...
- docker-compose 安装 mysql:5.7.31
目录 一.新建一个启动服务的目录 二.新建文件docker-compose.yml 三.新建角本文件 init-mysql.sh 四.实使化目录和配置文件 启动服务 登陆mysql 其它操作 参考文档 ...
- FileInputStream和FileOutputStream
FileInputstream 字节输入流 用于文件内容的读取操作. 创建FileInputstream对象用于读取文件内容,使用后需要进行关闭操作 常用方法: read(); //每次仅读取一个字节 ...
- 动态规划-DP 完整版
动态规划 学完了五大基础dp 做个简单总结 dp特征 动态规划问题 首要是找到做题的目的 是求最大/小值 还是其他: 其二 要确定问题的状态转移方程 这是关键: 第三 为dp数组找到边界. 最后 检查 ...
- k8s快捷命令
查看node使用率: for i in `kubectl get nodes|awk '{print $1}'|sed '1d'`;do echo "=========>" ...
- NumPy 数组排序、过滤与随机数生成详解
NumPy 数组排序 排序数组 排序数组意味着将元素按特定顺序排列.顺序可以是数字大小.字母顺序.升序或降序等. NumPy 的 ndarray 对象提供了一个名为 sort() 的函数,用于对数组进 ...