Spring Boot 2 读取配置文件
开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
新建一个名称为demo的Spring Boot项目。
一、默认配置文件
Spring Boot会读取名称application.properties(yml)的配置文件。
如果有多个同名文件,默认情况下,按照下面顺序读取:
(1)项目根目录的config目录
(2)项目根目录
(3)项目classpath下的config目录
(4)项目classpath根目录
如果同一个配置项出现在多份配置文件中,后面读取的值不会覆盖前面的。
测试:
在项目的4个位置各建立application.properties,内容如下:
(1)config/application.properties
test = config/application.properties
test1 = test1
(2)application.properties
test = application.properties
test2 = test2
(3)src/main/resources/config/application.properties
test = src/main/resources/config/application.properties
test3 = test3
(4)src/main/resources/application.properties
test = src/main/resources/application.properties
test4 = test4

修改默认生成的启动类 DemoApplication.java 代码:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @Autowired
private Environment env; @RequestMapping("/")
public String getProp(){
String test = env.getProperty("test");
String test1 = env.getProperty("test1");
String test2 = env.getProperty("test2");
String test3 = env.getProperty("test3");
String test4 = env.getProperty("test4");
return test + "," + test1 + "," + test2 + "," + test3 + "," + test4;
}
}
访问 http://localhost:8080/
输出:config/application.properties,test1,test2,test3,test4
二、指定配置文件
读取指定的配置文件,不使用默认的application.properties。
测试:
(1)src/main/resources/application.properties 内容:
test1 = application.properties
(2)在项目的src/main/resources新建目录config,新建配置文件myConfig.properties,内容:
test2= myConfig.properties

修改默认生成的启动类 DemoApplication.java 代码:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
public class DemoApplication { public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/config/myConfig.properties"
).run(args);
} @Autowired
private Environment env; @RequestMapping("/")
public String getProp(){
String test1 = env.getProperty("test1");
String test2 = env.getProperty("test2");
return test1 + "," + test2;
}
}
访问 http://localhost:8080/
输出:null,myConfig.properties
可见application.properties已读取不到,成功读取到配置文件myConfig.properties。
也可以使用spring.config.name指定配置文件的名称,如下面代码指定了myConfig,Spring Boot会到classpath下寻找myConfig.properties(yml)。
public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);
/*new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/config/myConfig.properties"
).run(args);*/
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.name=myConfig").run(args);
}
三、使用profile指定配置
使用profile可以根据特定的环境来激活不同的配置。
src/main/resources/application.yml 内容如下:
spring:
profiles: mysql
jdbc:
driver:
com.mysql.jdbc.Driver
---
spring:
profiles: oracle
jdbc:
driver:
oracle.jdbc.driver.OracleDriver
修改默认生成的启动类 DemoApplication.java 代码:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Scanner; @SpringBootApplication
@RestController
public class DemoApplication { public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args); Scanner scan = new Scanner(System.in);
String profile = scan.nextLine();
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/application.yml"
).profiles(profile).run(args);
} @Autowired
private Environment env; @RequestMapping("/")
public String getProp(){
String res = env.getProperty("jdbc.driver");
return res;
}
}
在IDEA中点击Run按钮后,在控制台先敲回车再输入oracle,
访问 http://localhost:8080/ 输出:oracle.jdbc.driver.OracleDriver
重新Run,在控制台先敲回车再输入mysql,
访问 http://localhost:8080/ 输出:com.mysql.jdbc.Driver
还可以通过不同配置文件的名称来设置profile,创建下面3个文件。
(1)src/main/resources/application.yml 内容:
spring:
profiles:
active: oracle
(2)src/main/resources/application-mysql.yml 内容:
jdbc:
driver:
com.mysql.jdbc.Driver
(3)src/main/resources/application-oracle.yml 内容:
jdbc:
driver:
oracle.jdbc.driver.OracleDriver

修改默认生成的启动类 DemoApplication.java 代码:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Scanner; @SpringBootApplication
@RestController
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @Autowired
private Environment env; @RequestMapping("/")
public String getProp(){
String res = env.getProperty("jdbc.driver");
return res;
}
}
访问 http://localhost:8080/ 输出:oracle.jdbc.driver.OracleDriver
Spring Boot 2 读取配置文件的更多相关文章
- 【转】spring boot mybatis 读取配置文件
spring boot mybatis 配置整理 一.加载mybatis的配置 1.手写配置,写死在代码里 import java.io.IOException; import java.util.P ...
- spring boot中读取配置文件的两种方式
application.properties test.name=测试 test.url=www.test.com 1.@Value注解 在controller里可以这样直接调用 @Value(&qu ...
- spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。
需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@ 而不是 ...
- spring boot 无法读取application.properties问题
spring boot 无法读取application.properties问题 https://bbs.csdn.net/topics/392374488 Spring Boot 之注解@Compo ...
- # spring boot + mybatis 读取数据库
spring boot + mybatis 读取数据库 创建数据库 use testdb; drop table if exists t_city; create table t_city( id i ...
- Spring Boot 的核心配置文件有哪几个?它们的区别是什么?
Spring Boot 的核心配置文件是 application 和 bootstrap 配置文件.application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置.b ...
- Spring Boot的properties配置文件读取
我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了.具体启动过程以前的博客写 ...
- 解决Spring boot中读取属性配置文件出现中文乱码的问题
问题描述: 在配置文件application.properties中写了 server.port=8081 server.servlet.context-path=/boy name=张三 age=2 ...
- Spring Boot 获取yaml配置文件信息
Spring boot 项目启动过程中: org.springframework.boot.SpringApplication#prepareEnvironment 当程序步入listeners.en ...
随机推荐
- 【代码审计】ESPCMSP8(易思企业建站管理系统)漏洞报告
0x00简介 项目名称:ESPCMS-P8(易思企业建站管理系统) 测试平台:Windwos 版本信息:P8.19082801稳定版 更新时间:2019-08-30 00:56:32 网站官网:htt ...
- Yii2中indexBy()的使用
在项目开发中经常会使用到一些特殊的值作为数组的索引,一般可以先查询出数据后数组循环拼接成所需的格式.不过YII2框架提供了一种更简单的方法indexBy(). 参考Yii文档:https://www. ...
- 【解决】Failed to restart network.service: Unit network.service not found.
问题:使用systemctl restart network 或 service network restart 命令重启网卡失败. 分析:原因其实也很简单,命令用错了,造成了找不到相应的网卡服务. ...
- 【原创】WinForm中实现单独Time控件的方式
WinForm默认只提供了DateTimePicker,今天的项目只用时间,不能出现日期,百撕不得骑姐(^^),也没花多少时间,随便试了一下,就成功了,分享一下. 在DateTimePicker属性中 ...
- idea使用maven中的tomcat插件开启服务出现java.net.BindException: Address already in use: JVM_Bind :8080错误原因
[INFO] create webapp with contextPath: /maven_web 五月 11, 2019 6:05:26 下午 org.apache.coyote.AbstractP ...
- Electron npm install 常见错误(Linux)
Linux版本 Ubuntu 12.04 (32bit) 安装Git sudo apt-get install git 生成ssh key #查看有没有sshkey cd ~/.ssh #生成 ssh ...
- simple go web application & 二维码生成 & 打包部署
go语言简易web应用 & 二维码生成及解码 & 打包部署 转载请注明出处: https://www.cnblogs.com/funnyzpc/p/10801476.html 前言(闲 ...
- 【Kafka】《Kafka权威指南》——提交和偏移量
KafkaConsumer(消费者)每次调用 poll()方法,它总是返回由生产者写入 Kafka但还没有被消费者读取过的记录, 我们因 此可以追踪到哪些记录是被群组里的哪个消费者读取的.之前已经讨论 ...
- 提速企业应用开发,Ntaub 3极速开发平台发布
企业管理系统搞了这么多年,还是以表单.数据增删改这些功能为主.唯一的变化就是“变化”,企业的业务模式和流程越来越多样化,开发需求变更越来越频繁,开发周期却要求越来越短. 无代码.低代码开发平台开始流行 ...
- 将 云数据库MongoDB(阿里云)物理备份文件下载恢复至本地自建数据库 遇到的5个问题
有时候我们可能需要将云上数据库下载到本地,下面是我们在操作MongoDB数据库时遇到的五个小问题. 其实现在RDS的 帮助文档 写的都比较详细了,大家在第一次操作时,可以细读一下,避免一些不必要的问题 ...