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. python pip 升级 或者换源

    1. 临时换源python -m pip install --upgrade pip -i https://pypi.douban.com/simple pip国内的一些镜像   阿里云  https ...

  2. Vue生命周期钩子---3

    vue生命周期流程图:4张图 : 生命周期的解析和应用: Vue 实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom→渲染.更新→渲染.卸载等一系列过程,我们称这是 Vue ...

  3. goroutine使用

    Goroutine是建立在线程之上的轻量级的抽象.它允许我们以非常低的代价在同一个地址空间中并行地执行多个函数或者方法.相比于线程,它的创建和销毁的代价要小很多,并且它的调度是独立于线程的.在gola ...

  4. ESA2GJK1DH1K升级篇: 阿里云物联网平台 OTA: 关于阿里云物联网平台 OTA 的升级流程

    前言 鉴于有些用户直接想使用现成的物联网平台实现 OTA 远程升级 我就写一写这系列的文章 注意:首先大家必须把我自建服务器是如何实现的看明白! 我看了下阿里云提供的,实际上流程和咱自建实现的差别不大 ...

  5. ASP.NET开发实战——(六)ASP.NET MVC & 分层 代码篇

    上一篇文章对如何规范使用ASP.NET进行了介绍,本章内容将根据上一篇得出的结论来修改博客应用的代码. 代码分层 综合考虑将博客应用代码分为以下几个层次: ○ 模型:代表应用程序中的数据模型,与数据库 ...

  6. LuaFramework 学习

    LuaFramework_UGUI_V2 https://github.com/jarjin/LuaFramework_UGUI_V2 using UnityEngine; using LuaInte ...

  7. Linux 系统下安装JDK1.8的教程详解

    一,安装前的清理工作 ? 1 2 3 rpm -qa | grep jdk rpm -qa | grep gcj yum -y remove java-xxx-xxx 二 , 在线下载JDK 命令: ...

  8. 匿名函数和for_each用法

    匿名函数,C++11的 for_each 用法 #include <iostream> #include <algorithm> #include "testClas ...

  9. matplotlib画图相关

    一. plt显示一副图像 1. import matplotlib.pyplot as plt 2. plt.figure()                    # 图像名称 3. plt.ims ...

  10. 在 EF Core 中 Book 实体在新增、修改、删除时,给 LastUpdated 字段赋值。

    直接贴代码: public class MenusContext : DbContext { public static class ColumnNames { public const string ...