springboot读取自定义properties配置文件方法
1. 添加pom.xml依赖
<!-- springboot configuration依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-configuration-processor</artifactId>
<optional> true </optional>
</dependency>
2. 在resources下建一个config包(当然包名随意), 在包里建一个remote.properties(老规矩, 文件名随意)
3. 在配置文件中写入测试内容
remote.testname=张三
remote.testpass=123456
4. 写一个实体类, 属性和配置文件对应
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Configuration
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false)
@PropertySource("classpath:config/remote.properties")
@Data
@Component
public class RemoteProperties {
private String testname;
private int testpass;
}
5. 在调用配置文件信息的类中搞事情
@EnableConfigurationProperties(RemoteProperties.class)
@RestController
public class PageTestController { @Autowired
RemoteProperties remoteProperties; @RequestMapping("testProperties")
public String testProperties(){
String str = remoteProperties.getTestname();
int i = remoteProperties.getTestpass();
System.out.println(str);
System.out.println(i);
return str+i;
}
}
PS: 有的小伙伴获取的配置文件出现了中文乱码问题, 请点击下方链接
解决 springboot 读取 properties 中文乱码
springboot读取自定义properties配置文件方法的更多相关文章
- SpringBoot读取配置properties配置文件
见:http://www.cnblogs.com/VergiLyn/p/6286507.html
- SpringBoot读取application.properties文件
http://blog.csdn.net/cloume/article/details/52538626 Spring Boot中使用自定义的properties Spring Boot的applic ...
- Springboot读取自定义配置文件的几种方法
一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
- java读取properties配置文件方法(一)
为了修改项目参数方便,需要使用properties配置文件: 首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包) commons configur ...
- java读写properties配置文件方法
1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...
- Properties类操作.properties配置文件方法总结
一.properties文件 Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式,可以用“#”作为注释,jav ...
- ResourceBundle类的方式来读取config.properties配置文件参数值
//获取config.properties配置文件参数值 public static ResourceBundle resource = ResourceBundle.getBundle(" ...
- Spring-Boot注入自定义properties文件配置
创建wzq.properties wzq.properties注入User实体类中 @PropertySource(value = "classpath:wzq.properties&quo ...
- springboot读取自定义配置文件节点
今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...
随机推荐
- CodeForces - 1251E2 (思维+贪心)
题意 https://vjudge.net/problem/CodeForces-1251E2 一共有 n 个选民,你可以付出 pi 的代价让第 i 个选民为你投票,或者,在为你投票的人数达到 mi ...
- show()和隐藏hide() slideDown()和 slideUp() fadeIn()和fadeOut()
1==>显示show()和隐藏hide() 是一组动画 与切换toggle()$("div").show():当不传递参数时,没有动画效果,它将某个元素瞬间显示出来 $(&q ...
- pdfium ppm demo
#include "fpdfview.h" #include <iostream> #include <string> #include <strin ...
- 使用 IDEA 翻译插件
使用 IDEA 翻译插件 1.安装 在IDEA插件中搜索 translation根据下载量排序有个完全匹配名称的插件,下载,重启 2.配置翻译插件 都是中文,就不说了
- Xmind最新的安装与破解教程
参考链接:https://www.jianshu.com/p/e1693dad4dde Tips: hosts文件的默认位置:C:\Windows\System32\drivers\etc
- Special-Judge模板
SPJ模板 放一篇\(SPJ\)(\(Special-Judge\))的模板. 注意,仅适用于\(Lemon\). 并不适用于洛谷. 代码:@zcs0724 #include <bits/std ...
- <LinkedList> 369 (高)143 (第二遍)142 148
369. Plus One Linked List 1.第1次while: 从前往后找到第一个不是9的位,记录. 2.第2次while: 此位+1,后面的所有值设为0(因为后面的位都是9). 返回时注 ...
- 【洛谷3515】[POI2011] Lightning Conductor(决策单调性)
点此看题面 大致题意: 给你一个序列,对于每个\(i\)求最小的自然数\(p\)使得对于任意\(j\)满足\(a_j\le a_i+p-\sqrt{|i-j|}\). 证明单调性 考虑到\(\sqrt ...
- CSP-S 2019 题解
D1T1-格雷码 题中给出了构造格雷码的方法. $solve(n,k)$表示求出$2^n$意义下排名为$k$的格雷码, 只要比较一下考虑最高位的0/1取值就好了. 部分分提示了要开$unsigned\ ...
- ThreadPoolExecutor 线程池 简单解析
jdk1.8 ThreadPoolExecutor ThreadPoolExecutor实际就是java的线程池,开发常用的Executors.newxxxxx()来创建不同类型和作用的线程池,其底部 ...