Spring Boot详解配置文件的用途与用法
作者:独一无二的哈密瓜 发布时间:2022-08-01 20:37:27
1. SpringBoot 配置文件
1.1 配置文件的作用
配置文件中配置了项目中重要的数据, 例如:
数据库的连接信息 (用户名密码)
项目的启动端口
第三方系统的调用密钥等信息
用于发现和定位问题的普通日志和异常日志等
Spring Boot项目没有配置信息, 就不能连接和操作数据库, 甚至不能保存可以用于排查问题的关键日志. 所以配置文件非常重要.
1.2 配置文件的格式
在Spring Boot 中配置文件主要分为两种:
.properties
(主要是key=value格式).yml
(主要是key: value格式)
注意:
当项目中既有
.properties
和.yml
, 且两个配置文件中有相同的配置项, Spring Boot 会优先考虑.properties
, 因为.properties
的优先级更高一些.一个项目中允许存在两种不同的配置文件,
.properties
.yml
, 但是在项目中建议只使用一种配置文件的格式.
1.3 properties 配置文件说明
1.3.1 properties 基本语法
properties 是以 key=value
这种格式配置的.
server.port=9090
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/2022-6-1
spring.datasource.username=root
spring.datasource.password=1234
配置文件的注释信息使用 “#”
1.3.2 读取配置文件
读取配置文件的内容, 可以使用 @Value
注解来实现
@Value
注解使用 "${}"
的格式读取.
@Component
public class Read implements InitializingBean {
@Value("${server.port}")
private String port;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println();
System.out.println(port);
System.out.println();
}
}
1.4 yml 配置文件说明
yml 是 YMAL 是缩写, 它的全称是 Yet Another Markup Language, 译为 另一种标记语言.
1.4.1 yml 基本语法
yml 是树形结构的配置文件, 它的基础语法是 key: value
, 这里的:
后面跟着一个空格.
server:
port: 9090
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/2022-6-1
username: root
password: 1234
1.4.2 yml 使用进阶
# ~代表null
null.value: ~
查看一段代码
string:
str1: Hello \n World
str2: 'Hello \n World'
str3: "Hello \n World"
读取yml中的这段代码
@Component
public class Read1 implements InitializingBean {
@Value("${string.str1}")
private String str1;
@Value("${string.str2}")
private String str2;
@Value("${string.str3}")
private String str3;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println();
System.out.println("str1: "+str1);
System.out.println("str2: "+str2);
System.out.println("str3: "+str3);
System.out.println();
}
}
运行结果:
字符串加上双引号, 会执行\n 换行.
1.4.3 配置对象
yml 中配置对象
student:
id: 1
name: zhangsan
age: 18
读取配置的对象, 就需要用到另一个注解: @ConfigurationProperties
@Component
@ConfigurationProperties("student")
public class User {
private int id;
private String name;
private int age;
// 一堆getter setter
}
读取
@Component
public class Read2 implements InitializingBean {
@Autowired
private Student student;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println();
System.out.println(student);
System.out.println();
}
}
1.4.4 配置集合
yml 中 配置集合
mylist:
colors:
- RED
- GREEN
- BLACK
读取配置集合
@Component
@ConfigurationProperties("mylist")
public class MyList {
private List<String> colors;
// 一堆getter 和 setter
}
打印代码
@Component
public class Read3 implements InitializingBean {
@Autowired
private MyList myList;
@Override
public void afterPropertiesSet() throws Exception {
for (String val : myList.getColors()){
System.out.println(val);
}
}
}
1.4.5 yml的另一种写法(行内写法)
配置对象
student: {id: 1,name: zhangsan,age: 18}
配置集合
mylist: {colors: [RED,GREEN,BLACK]}
1.5 properties 和 yml 比较
properties
的语法更复杂, yml
语法更简洁
yml通用性更好, 支持更多的语言, 如 Java, Go, Python等
yml支持更多的数据类型
yml格式的配置文件写的时候容易出错(在:之后有一个空格), 而properties写法传统比较复制,但不太容易出错
2. 读取 SpringBoot 配置文件的方法
2.1 使用 @Value 读取配置文件
只能读取一个
@Component
public class Read implements InitializingBean {
@Value("${server.port}")
private String port;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println();
System.out.println(port);
System.out.println();
}
}
2.2 使用@ConfigurationProperties
直接在类上写
@Component
@ConfigurationProperties("mylist")
public class MyList {
private List<String> colors;
public List<String> getColors() {
return colors;
}
public void setColors(List<String> colors) {
this.colors = colors;
}
}
2.3 @PropertySource读取指定配置文件
jdbc.username=root2
jdbc.password=root1
@Component
@PropertySource(value = {"classpath:application.properties"})
public class JDBC implements InitializingBean {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(username + " " + password);
}
}
来源:https://wangzhi430.blog.csdn.net/article/details/125085869


猜你喜欢
- 本文开始做一个网上商城的项目,首先从搭建环境开始,一步步
- MessageFormat本身与语言环境无关,而与用户提供给MessageFormat的模式和用于已插入参数的子格式模式有关,以生成适用于不
- 前言自从用了SpringBoot,个人最喜欢的就是SpringBoot的配置文件了,和Spring比起SpringBoot更加灵活,修改的某
- 本文实例为大家分享了Android实现跟随手指画圆的具体代码,供大家参考,具体内容如下首先自己定义一个View子类:package com.
- 在Android平台上面,应用程序OOM异常永远都是值得关注的问题。通常这一块也是程序这中的重点之一。这下我就如何解决OOM作一点简单的介绍
- 引言最近的项目需求中有使用到后端发送http请求,在网上寻找资料后发现可以使用spring自带的RestTemplate类实现,故作此记录项
- 在之前,已经学习到了线程的创建和状态控制,但是每个线程之间几乎都没有什么太大的联系。可是有的时候,可能存在多个线程多同一个数据进行操作,这样
- //直接插入排序void DirectInsertionSort(int* arr, in
- 一、JTA组件简介什么是JTAJTA,全称:Java Transaction API。JTA事务比JDBC事务更强大。一个JTA事务可以有多
- SpringCloudStream配置以下配置摘自《SpringCloud微服务实战》,配置主要包括两大部分:Stream配置(基础配置、通
- The java.io.Writer.flush() method flushes the stream. If the stream ha
- 近日沉醉于熟悉公司新项目的过程,不断地接触新的应用场景与实现技术,对于我是一种学不来的进步,实践是检验真理的唯一标准。我们今天就浅浅的谈一谈
- 1、Jetbrains官网下载IntelliJ IDEA1.1 官方网站http://www.jetbrains.com/idea/&nbs
- 本文实例为大家分享了Android实现录音静音降噪的具体代码,供大家参考,具体内容如下需求:客户反馈产品的录音里面很多杂音(因为我们把Cod
- 本文实例讲述了C#实现软件监控外部程序运行状态的方法。分享给大家供大家参考。具体方法如下:需要 * 一个程序,用于监控另一个程序运行状态,一旦
- 题主要区分清楚内码(internal encoding)和外码(external encoding)就好了。内码是程序内部使用的字符编码,特
- 引言♀ 小AD:明哥,我终于出了这口恶气了。♂ 明世隐:打爽了是吧。♀ 小AD:那必须的,打十盘我赢九盘,我随意。♂ 明世隐:那小朋友不是搞
- 本文实例为大家分享了Android实现3D云标签效果的具体代码,供大家参考,具体内容如下一、自定义Viewpublic class TagC
- 前言:这里所说的全局Dialog是指无论当前应用是处于哪一个页面上,都能够及时弹出Dialog来提示用户一些信息,用户体验不会很好,一般应用
- 本文实例为大家分享了Android实现五子棋游戏的具体代码,供大家参考,具体内容如下实现环境: android studio 3