面试突击75:SpringBoot 有几种读取配置文件的方法?
Spring Boot 中读取配置文件有以下 5 种方法:
- 使用 @Value 读取配置文件。
- 使用 @ConfigurationProperties 读取配置文件。
- 使用 Environment 读取配置文件。
- 使用 @PropertySource 读取配置文件。
- 使用原生方式读取配置文件。
它们的具体使用方法如下,为了方便测试,我们在 Spring Boot 配置文件 application.properties 添加以下内容:
profile.name=Spring Boot Profile
profile.desc=Spring Boot Profile Desc.
1.使用 @Value 读取配置文件
使用 @Value 可以读取单个配置项,如下代码所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("My Profile Name:" + name);
}
}
以上程序的执行结果如下图所示:

2.使用 @ConfigurationProperties 读取配置文件
@ConfigurationProperties 和 @Value 的使用略微不同,@Value 是读取单个配置项的,而 @ConfigurationProperties 是读取一组配置项的,我们可以使用 @ConfigurationProperties 加实体类读取一组配置项,如下代码所示:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
private String name;
private String desc;
}
其中 prefix 表示读取一组配置项的根 name,相当于 Java 中的类名,最后再把此配置类,注入到某一个类中就可以使用了,如下代码所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Profile profile;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Profile Object:" + profile);
}
}
以上程序的执行结果如下图所示:

3.使用 Environment 读取配置文件
Environment 是 Spring Core 中的一个用于读取配置文件的类,将此类使用 @Autowired 注入到类中就可以使用它的 getProperty 方法来获取某个配置项的值了,如下代码所示:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Profile Name:" + environment.getProperty("profile.name"));
}
}
以上程序的执行结果如下图所示:

4.使用 @PropertySource 读取配置文件
使用 @PropertySource 注解可以用来指定读取某个配置文件,比如指定读取 application.properties 配置文件的配置内容,具体实现代码如下:
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Name:" + name);
}
}
以上程序的执行结果如下图所示:

中文乱码
如果配置文件中出现中文乱码的情况,可通过指定编码格式的方式来解决中文乱码的问题,具体实现如下:
@PropertySource(value = "dev.properties", encoding = "utf-8")
注意事项
@PropertySource 注解默认是只支持 properties 格式配置文件的读取的。
5.使用原生方式读取配置文件
我们还可以使用最原始的方式 Properties 对象来读取配置文件,如下代码所示:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
@SpringBootApplication
public class DemoApplication implements InitializingBean {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
Properties props = new Properties();
try {
InputStreamReader inputStreamReader = new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream("application.properties"),
StandardCharsets.UTF_8);
props.load(inputStreamReader);
} catch (IOException e1) {
System.out.println(e1);
}
System.out.println("Properties Name:" + props.getProperty("profile.name"));
}
}
以上程序的执行结果如下图所示:

总结
在 Spring Boot 中读取配置文件有以下 5 种方法:
- 使用 @Value 读取配置文件。
- 使用 @ConfigurationProperties 读取配置文件。
- 使用 @PropertySource 读取配置文件。
- 使用 Environment 读取配置文件。
- 使用原生方式读取配置文件。
其中最常用的是前 3 种,如果读取某一个配置项可使用 @Value,如果读取一组配置项可使用 @ConfigurationProperties,如果要指定读取某一个具体的配置文件可使用 @PropertySource 来指定。
是非审之于己,毁誉听之于人,得失安之于数。
公众号:Java面试真题解析
面试突击75:SpringBoot 有几种读取配置文件的方法?的更多相关文章
- SpringBoot学习笔记:读取配置文件
SpringBoot学习笔记:读取配置文件 配置文件 在以往的项目中,我们主要通过XML文件进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息.在SpringBo ...
- SpringBoot:四种读取properties文件的方式
前言 在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作.今天就分享四种在Springboot中获取配置文件的方式. 注:前三种测试配置文件为springboot默认的applic ...
- springboot中使用@Value读取配置文件
一.配置文件配置 直接配置 在src/main/resources下添加配置文件application.properties 例如修改端口号 #端口号 server.port=8089 分环境配置 在 ...
- Shell读取配置文件的方法
参考:http://www.cnblogs.com/binbinjx/p/5680214.html 做批量软件安装自动化时,都喜欢用配置文件的方式改变参数,那怎么通过shell读取配置文件的配置呢?参 ...
- Log4j 2.0读取配置文件的方法
log4j中配置日志文件存放的位置不一定在src下面,即根目录下.这个时候我们需要解决如何加载配置文件的问题.在log4j1.x中解决的方法就比较多了.如:PropertyConfigurator.c ...
- 【面试突击】-SpringBoot面试题(一)
Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...
- SpringBoot两种读取配置文件的方式
方式一 @Value("${custom.group}") private String customGroup; 方式二 @Autowired private Environme ...
- springboot @Value 类中读取配置文件 .properties null 原因和解决方案
问题:在一个工具类中,通过@Value来映射配置文件的值,得到的总是null 原因:不能用new工具类的方式,应该是用容器注册(@Autowried)的方式使用此工具类,就能得到配置文件里的值 上代码 ...
- java中读取配置文件的方法
转自:http://blog.csdn.net/stypace/article/details/38414871 一.使用org.apache.commons.configuration 需要使用的是 ...
随机推荐
- Go微服务框架go-kratos实战01:quickstart 快速开始
先来感受下用 kratos 快速创建项目 一.环境准备 1.1 安装依赖软件 protoc protoc-gen-go 建议开启 GO111MODULE 1.2 安装 kratos cli go in ...
- 小白excel初步使用2022.06.02
1.添加 对表格数据相加求和:在表示数据的那一列黄色表格下输入=SUM(D1:D5)或者alt+=或者SUMIF(D1:D5,">50")或者SUMIF(D1,D3:D7,1 ...
- VS Code - Vim 插件自动切换输入法
前言: 在使用 Linux 的过程中,vim 是一个不错的编辑器,以至于多数人将其用成了习惯,在没有 vim 的环境下还是习惯用 vim 的快捷键来编辑文本.所以便有开发者们为众多的 IDE 和文本编 ...
- Chrome自带功能实现网页截图
更新记录 本文迁移自Panda666原博客,原发布时间:2021年6月28日. 很简单,按下Ctrl+Shift+P,打开命令行窗口,如下图所示. 输入命令. Capture full size sc ...
- 在sqlbolt上学习SQL
在sqlbolt上学习SQL 该网站能够学习sql基础,并且能在网页中直接输入sql语句进行查询. 学习网站原网址https://sqlbolt.com/(!部分指令该网站不支持,且存在一些bug!) ...
- python新建一个目录
源码部分 import os # 创建目录 def mkdir(path): isExists = os.path.exists(path) if not isExists: os.makedirs( ...
- Linux系统安全配置
1.物理安全 硬件服务器,关闭从CD/DVD等这些方面的软启动方式.同时也可以设置BIOS密码,并且要有限制访问的策略与各类流程管控. 还可以禁用USB设备来达到安全的目的: centos7x 安装d ...
- MySQL十种锁,一篇文章带你全解析
MySQL有两个核心的知识点,索引和锁.前几篇文章已经详细讲解了MySQL索引实现机制,今天再一起学习一下MySQL的锁. 1 为什么要加锁? 当多个事务并发操作同一批数据的时候,如果不加锁,就无法保 ...
- Java模拟西宝高速公路
@ 目录 写在前面 一.仿真模拟的具体要求 二.类的设计 2.1 抽象父类PubVehicles 2.2 Expressway类 2.3 Passenger类 2.4 Timer类 2.5 Displ ...
- 业务可视化-让你的流程图"Run"起来
前言 最近在研究业务可视化的问题,在日常的工作中,流程图和代码往往是分开管理的. 一个被维护多次的系统,到最后流程图和代码是否匹配这个都很难说. 于是一直有一个想法,让程序直接读流程图,根据流程图的配 ...