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配置文件方法的更多相关文章

  1. SpringBoot读取配置properties配置文件

    见:http://www.cnblogs.com/VergiLyn/p/6286507.html

  2. SpringBoot读取application.properties文件

    http://blog.csdn.net/cloume/article/details/52538626 Spring Boot中使用自定义的properties Spring Boot的applic ...

  3. Springboot读取自定义配置文件的几种方法

    一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...

  4. java读取properties配置文件方法(一)

    为了修改项目参数方便,需要使用properties配置文件: 首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包) commons configur ...

  5. java读写properties配置文件方法

    1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...

  6. Properties类操作.properties配置文件方法总结

    一.properties文件 Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式,可以用“#”作为注释,jav ...

  7. ResourceBundle类的方式来读取config.properties配置文件参数值

    //获取config.properties配置文件参数值 public static ResourceBundle resource = ResourceBundle.getBundle(" ...

  8. Spring-Boot注入自定义properties文件配置

    创建wzq.properties wzq.properties注入User实体类中 @PropertySource(value = "classpath:wzq.properties&quo ...

  9. springboot读取自定义配置文件节点

    今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...

随机推荐

  1. client-go向controller进发---code-generator实现

    这个时间长了,可能前后想了一周时间. 哎,其它不怪,只怪go的工程包管理这几年太混乱, 而国内下载资源也太漫长. 现在,只能坚持 使用go mod了. 但在使用code-generator时,go m ...

  2. 浅谈[].slice.call(arguments, 1)

    今天在跟踪调试项目的代码时,发现在一个function中用到了这么一段[].slice.call(arguments, 1) 的代码.心中便起了疑惑,对于slice 和 call 的方法,倒是不怎么陌 ...

  3. drf框架知识总结

  4. python的wraps函数

    当使用@修饰符修饰函数时,会存在这样一个问题:被修饰的函数会消失(这是因为修饰函数没有设置返回值,如果设置了返回值,则就把返回值赋给被修饰函数,比如,test1函数的返回值设置为 return 6, ...

  5. C++ 基于rapidjson对json字符串的进行序列化与反序列化

    json字符串的解析以封装在我们开发过程中经常见到, 尤其在socket通信上面, 在一次项目中碰到json字符串的进行解析, 而公司有没有封装好的库, 于是就自己基于开源的库进行了一次封装, 接下是 ...

  6. Java连载7-变量&数据类型

    一.变量 1.注意点: 在同一个“作用域”中,变量名不能重名,但是变量可以重新赋值. 2.什么是作用域? 答:描述的是变量的有效范围,在范围之内是可以被访问的,只要出了作用域就无法访问(也就是在大括号 ...

  7. linux中dd命令详解

    本文转自:https://www.cnblogs.com/yuanqiangfei/p/9138625.html 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. ...

  8. postgresql in 优化

    原sql: SELECT res_id_ori FROM wk_sheet A, wk_page b WHERE A .wk_sheet_id = b.wk_sheet_id ') ; 原sql执行计 ...

  9. netCore3.0+webapi到前端vue(前端)

    前篇已经完成后端配置并获取到api连接 https://www.cnblogs.com/ouyangkai/p/11504279.html 前端项目用的是VS code编译器完成 vue 第一步 引入 ...

  10. 01_python基础(一)

    python学习笔记,打算用五章介绍完python基础语法及基本用法. 开发环境: python3.7    推荐:  https://github.com/jackfrued/Python-100- ...