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的文章就需 ...
随机推荐
- 21.Java基础_String类
String类构造方法 package pack1; //推荐使用直接赋值的方式得到字符串 public class test { public static void main(String[] a ...
- CSS自定义字体的实现,前端实现字体压缩
CSS中使用自定义字体,首先需要下载你需要的字体ttf或者otf文件 这里推荐一个网站:http://www.zitixiazai.org/ /********css中********/ @font- ...
- 初学JavaScript正则表达式(八)
JavaScript正则贪婪模式与非贪婪模式 贪婪模式: 例: \d{3,6} 匹配数字3到6次(正则表达式默认为贪婪模式,即匹配最多次,失败即停) '12345678'.replace(/\d{3, ...
- day75_10_22频率认证和jwt
一.频率认证原理. 1.从dispatch中获取配置,找到setting中的配置. 2.从thtoyyling中寻找到各个认证类. 3.所有认证类都继承自basethrottle,basethrott ...
- day4_7.2
流程语句 1.if判断语句 在python中if语句可以依据判断的条件,决定执行哪个语句.其格式如下: if 条件: 代码1 else: 代码2 当满足条件1时,执行代码1,否则执行代码2.所以条件语 ...
- 《Vue 进阶系列之响应式原理及实现》
https://www.bilibili.com/video/av51444410/?p=5 https://github.com/amandakelake/blog/issues/63 https: ...
- zz传统方法和深度学习结合的感知策略探索
今天分享下 Pony.ai 在感知探索的过程中,使用的传统方法和深度学习方法.传统方法不代表多传统,深度学习也不代表多深度.它们都有各自的优点,也都能解决各自的问题.我们希望发挥它们的优点,并且结合起 ...
- layui中form表单渲染的问题
layui 官网的这部分文档介绍:http://www.layui.com/doc/modules/form.html#render 注意:针对的是表单元素,input select textare ...
- 【转】ServletContext介绍及用法
1.1. 介绍 ServletContext官方叫servlet上下文.服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象.这个对象全局唯一,而且工程内部的所有servl ...
- 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...