文章目录
配置文件的作用
在Spring Boot项目中,配置文件用于配置应用程序的行为和属性。通过配置文件,您可以更改应用程序的各种设置,而无需修改代码。这使得应用程序配置变得灵活和可定制。
通过配置文件,您可以配置一些常见的属性,如
- 端口号
- 数据库连接
- 日志级别
- 环境配置
…
Spring Boot会读取这些配置文件,并将其应用于您的应用程序。
配置文件语法格式(.yml 和 .properties)
在Spring Boot中,配置文件有两种常用格式:YAML(.yml)和属性(.properties)。这两种格式都可以用来配置Spring Boot应用程序,但它们在语法和使用方式上有一些区别。
语法格式:
- .yml:使用缩进和层级表示数据结构,使用冒号(:)和短横线(-)表示键值对和数组。
- .properties:使用键值对的形式,用等号(=)分隔键和值。
可读性:
- .yml:由于使用缩进和层级,.yml的格式较为简洁和易读,可以更好地表达复杂的数据结构。
- .properties:.properties文件的格式相对简单,适合于简单的键值对配置。
支持复杂数据结构:
- .yml:.yml支持更复杂的数据结构,例如嵌套的映射、列表和对象。
- .properties:.properties文件只支持简单的键值对,无法直接表达复杂的数据结构。
属性值引用:
- .yml:.yml可以使用&和*引用先前定义的.properties值,以便在不同位置使用相同的值。
- .properties:.properties文件不支持属性值的引用。
注释支持:
- .yml:.yml支持行内注释和块注释,可以更容易地注释配置项。
- .properties:.properties文件只支持行注释,使用#符号。
综上所述,.yml适用于复杂的配置和数据结构,提供了更丰富的语法和可读性。.properties文件则适用于简单的键值对配置。在实际使用中,可以根据需求和个人喜好选择适合的配置文件格式。
.yml文件的常见编写结构(YAML)
注意:在.yml文件中要在 ' : ' 后面加空格,一般加一两格就行。
常规结构(键值对key: value):
userinfo:
name: Kejin
age: 20
多行字符串格式:
description: |
This is a multi-line
string in YAML.
It preserves line breaks.
数组:
fruits:
- apple
- banana
- orange
嵌套对象结构:
userinfo:
name: Kejin
age: 20
girlfriend:
name: yk
age: 20
list嵌套list格式:
list:
-
- Ruby
- Perl
- Python
-
- c
- c++
- java
list嵌套map格式:
list:
-
id: 1
name: dawan
-
id: 2
name: erwa
注释:
# This is a comment
key: value # Inline comment
引用和锚点:
defaults: &defaults
timeout: 30
retries: 3
server1:
<<: *defaults
host: example.com
server2:
<<: *defaults
host: example.org
引用其他.yml文件:
myConfig: !!include config.yml
.properties文件的常见编写格式
常规结构(键值对key: value):
key=value
userinfo.name=John Doe
userinfo.age=25
注释:
# This is a comment
key=value
多行值:
multiline=This is a \
multi-line \
value
转义字符:
specialChars=This is a \# special \! \t chars
变量引用:
server.host=example.com
server.port=8080
url=http://${server.host}:${server.port}/api
空值:
key=value
emptyKey=
.properties文件本身不直接支持列表(List)的语法。但可以使用特定的命名约定来表示列表,然后在代码中读取.properties文件时,可以使用 @ConfigurationProperties 注解或 @Value 注解来绑定键值对,并将其转换为Java中的List数据类型。
所以使用.yml文件要方便得多。
配置文件注入
一般注解默认是识别application.yml 或 application.properties文件。在使用@Value注解和@ConfigurationProperties注解可以通过添加 @PropertySource(“classpath:user.yml”)注解来进行其他配置文件的注入。
1、@Value 注解
使用@Value注解可以将配置文件的值直接注入到变量中。使用的配置文件是上面的常规的键值对配置文件的内容。
// 这是.properties文件的注入
@Value("${userInfo.name}")
private String name;
@Value("${userInfo.age}")
private Integer age;
// 这是.yml文件的注入
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
@PropertySource(“classpath:user.yml”)注解使用的位置,支持多个配置文件用逗号隔开。
@PropertySource("classpath:user.yml")
public class User {
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
}
2、@ConfigurationProperties 注解
通常用于将属性绑定到类的字段或@Bean方法的参数上。它是从Spring Boot中继承并广泛应用于绑定外部配置文件中的属性。
@ConfigurationProperties(prefix = "userinfo")
public class User {
private String name;
private Integer age;
}
获取custom.properties文件的将开头为custom的属性绑定到CustomProperties 示例上。
@Configuration
@PropertySource("classpath:custom.properties")
public class AppConfig {
@Bean
@ConfigurationProperties(prefix = "custom")
public CustomProperties customProperties() {
return new CustomProperties();
}
// 其他的@Bean方法...
}
在使用该注解的时候需要先在 pom.xml文件中添加下面的依赖,不然会出现提示:未配置spring boot注解配置处理器。在出现提示之后才添加的需要重启idea,如果提示还在就无视提示运行。正常情况下是可以运行的。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3、Environment 对象
使用该对象的时候,默认是使用主配置文件 application.properties 或 application.yml 里的内容。
@Autowired
private Environment environment;
public void yourMethod() {
String propertyValue = environment.getProperty("your.property.key");
}
@Autowired
private Environment environment;
public void yourMethod() {
String propertyValue = environment.getProperty("key");
}
XML文件
在Spring Boot中,使用XML配置文件相对较少,因为Spring Boot更倾向于使用基于注解的Java配置。然而,仍然可以在Spring Boot应用程序中使用XML配置文件,特别是在与旧有的XML配置文件集成或使用某些扩展框架时。
要在Spring Boot中使用XML配置文件,需要完成以下步骤:
1、添加Maven依赖: 在 pom.xml 文件中添加以下依赖,以便使用Spring的XML配置支持。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
2、创建XML配置文件: 在 src/main/resources 目录下创建一个新的 XML 文件,例如 application.xml。
3、配置XML文件: 在 XML 文件中配置 Spring Bean,以及其他需要的配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="exampleBean" class="com.example.ExampleBean">
<!-- Bean 的属性配置 -->
<property name="property1" value="value1"/>
<property name="property2" value="value2"/>
</bean>
</beans>
4、启用XML配置: 在Spring Boot应用程序的入口类上使用 @ImportResource
注解来启用 XML 配置文件。
@SpringBootApplication
@ImportResource("classpath:application.xml")
public class YourApplication {
// 程序入口类的其他配置
}
**5、使用配置:**在代码注入和使用XML配置文件中声明Bean
@Autowired
private ExampleBean exampleBean;
// 使用 exampleBean
小结
这些做为个人学习记录,如有不足欢迎留言补充。