SpringBoot中的配置文件与配置读取(一)

总结SpringBoot中的properties与yml配置文件与手动读取配置。

1.配置文件

两种配置文件

Spring Boot中,可以使用两种配置文件格式:properties配置文件和yml配置文件。

  1. properties配置文件:

properties配置文件是一种键值对配置文件,主要用于存储静态属性值。Spring Boot会自动加载classpath下的application.properties文件。例如:

1
2
app.name=My Application
app.version=1.0.0
  1. yml配置文件:

yml配置文件是一种更简洁的配置文件格式,主要用于存储动态属性值。Spring Boot会自动加载classpath下的application.yml文件。例如:

1
2
3
app:
name: My Application
version: 1.0.0

配置文件多个起名情况

多配置文件的情况下,配置文件一般要求以application开头,可以是.yml结尾的文件,也可以是.properties结尾的文件。

配置文件的优先级

propertie和yml的配置文件的优先级

在Spring Boot中,properties文件的配置优先级高于yml文件。如果两个文件中都有同一个配置项,那么将优先使用properties文件中的配置值。

yml配置文件的指定与优先级

可以使用application.yml指定加载另一个配置文件,这样就能同时使用两个配置文件。

通常创建一个application.yml,通过配置信息 spring.profile.active 来指定需要加载的配置文件:

这个时候,如果application-local里面和application里面有相同的配置,项目会以application-local为准。

默认的配置文件是放在 src/main/resources 目录下,当然也是可以放其他位置的:

  • 外置,在相对于应用程序运行目录的 /config 子目录中
  • 外置,在应用程序运行的目录中
  • 内置,放在config包下(即 src/main/resources/config)目录下
  • 内置,放在classpath根目录下(即默认的 src/main/resources/目录下

上面的优先级是从高到低来的,即外置的改与内置的;config下面的高于根目录下的

2.@Value注解(推荐使用)

例如,在application.properties文件中定义一个属性:

1
myapp.name=My Application

使用@Value注解的方式有两种:

直接注入属性值

1
2
@Value("${property.key}")
private String propertyValue;

在上述代码中,"${property.key}"是配置文件中的属性键,propertyValue是要注入的属性值。Spring Boot会自动将配置文件中的属性值注入到propertyValue变量中。

注入整个配置对象

1
2
3
4
5
6
@ConfigurationProperties(prefix = "prefix")
@Component
public class MyConfig {
private String propertyValue;
// getter and setter methods
}

在上述代码中,@ConfigurationProperties注解用于指定配置文件中的属性前缀,MyConfig类中的属性将与配置文件中的属性进行匹配,并自动注入。

需要注意的是,为了使用@Value注解,我们需要在Spring Boot应用程序的配置类或组件类上添加@PropertySource注解,以指定要加载的配置文件。例如:

1
2
3
4
5
@Configuration
@PropertySource("classpath:config.properties")
public class AppConfig {
// configuration code
}

这样,当Spring Boot应用程序启动时,@Value注解将会自动将配置文件中的属性值注入到相应的变量中。

3.传统接收方式Properties.java

在 Spring Boot 中,可以使用 @ConfigurationProperties 注解将 YAML 文件中的配置映射到 properties.java 文件中。以下是一些示例步骤:

  1. 创建一个 properties.java 类,其中包含我们要映射的属性。例如,如果我们在 YAML 文件中有一个名为 myapp.name 的属性,我们可以在 properties.java 中创建一个对应的属性,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("myapp")
public class MyAppProperties {
private String name;

// Getter 和 Setter 方法

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
  1. 确保在我们的 @SpringBootApplication 标记的主类或配置类上添加 @EnableConfigurationProperties 注解,并指定我们的 properties.java 类。例如:
1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(MyAppProperties.class)
public class YourApplication {

public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
  1. 在我们的 YAML 配置文件中使用符合命名规则的属性。例如,我们可以在 application.yml 文件中设置 myapp.name 属性如下:
1
2
myapp:
name: My Application
  1. 在其他组件(如服务、控制器等)中使用 MyAppProperties 类来获取配置的值。例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

@Autowired
private MyAppProperties myAppProperties;

@GetMapping("/name")
public String getName() {
return myAppProperties.getName();
}
}

通过上述步骤,我们可以将 YAML 文件中的配置映射到 properties.java 类中,并在其他组件中使用 @Autowired 注解将其注入。这样,就可以通过 MyAppProperties 类来访问配置的属性值了。

4.@Value结合Properties

在 Spring Boot 中,我们可以使用 @Value 注解将 YAML 文件中的配置值直接注入到一个属性或字段中。以下是一些示例步骤:

  1. 创建一个 properties.java 类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyAppProperties {
@Value("${myapp.name}")
private String name;

// Getter 和 Setter 方法

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
  1. 在 YAML 配置文件中设置属性。
1
2
myapp:
name: My Application
  1. 在其他组件(如服务、控制器等)中注入 MyAppProperties 类,然后使用 @Value 注解访问配置的属性值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

@Autowired
private MyAppProperties myAppProperties;

@Value("${myapp.version}")
private String version;

@GetMapping("/name")
public String getName() {
return myAppProperties.getName();
}

@GetMapping("/version")
public String getVersion() {
return version;
}
}

通过上述步骤,我们可以使用 @Value 注解直接将配置文件中的属性值注入到相应的属性或字段中。在上面的示例中,myapp.name 的值将注入到 MyAppProperties 类的 name 属性中,而 myapp.version 的值将注入到 version 字段中。我们可以在其他组件中使用这些属性或字段来访问配置的值。

5.jar包指定启动配置文件

启动时选择配置文件

如果想要在启动Spring Boot的JAR包时选择外部的配置文件,可以使用--spring.config.name--spring.config.location命令行参数来指定外部配置文件的名称和位置。以下是如何做到这一点的步骤:

  1. 将外部配置文件(例如,custom-config.yml)放在你希望的目录中,这可以是与JAR文件不同的目录。
  2. 使用以下命令来启动Spring Boot JAR包,并指定外部配置文件的名称和位置:
1
java -jar your-app.jar --spring.config.name=custom-config --spring.config.location=file:/path/to/external/config/
其中,`your-app.jar`是Spring Boot应用程序的JAR文件,`custom-config`是配置文件的名称(不包括扩展名,如`.yml`),`/path/to/external/config/`是包含外部配置文件的目录的路径。

这样,Spring Boot将加载名为custom-config.yml的外部配置文件,而不是默认的application.ymlapplication.properties,并且它会在指定的外部配置文件位置进行查找。这允许你覆盖应用程序的默认配置。

默认配置文件

spring.config.name选择配置文件

在Spring Boot的jar包启动时,如果想选择使用同目录下的application.yml配置文件,你可以使用spring.config.name属性并将其设置为application。这将告诉Spring Boot查找名为application.yml的配置文件,而且默认情况下,它会在jar包所在的目录下查找。

  1. application.yml配置文件放在与可执行的JAR文件相同的目录中。
  2. 启动应用时,不需要提供任何特定的命令行参数或环境变量,因为Spring Boot会默认查找名为application.yml的配置文件。

这样,Spring Boot将自动加载位于JAR文件相同目录下的application.yml配置文件。这是Spring Boot的默认行为,无需额外的配置。

jar包默认使用同路径的application.yml配置文件

如果你没有显式地配置spring.config.name,Spring Boot会默认使用同目录下的application.yml配置文件。这是Spring Boot的标准行为,因此如果没有提供spring.config.name,它将尝试加载名为application.yml的配置文件。这对于许多常见用例是方便的,因为绝大多数Spring Boot应用程序都使用application.ymlapplication.properties作为默认的配置文件。

因此,如果你的配置文件命名为application.yml,而且位于可执行JAR文件的同一目录中,你不需要额外的配置来告诉Spring Boot使用它,它将自动加载该配置文件。

jar包启动优先级

Spring Boot默认会在以下位置查找配置文件,按优先级从高到低的顺序:

  1. 外部配置文件(External Configuration File):这意味着如果你将application.yml文件放在可执行JAR文件的同一目录中,Spring Boot将首先使用这个外部配置文件。
  2. JAR包内部配置文件:如果在可执行JAR文件的内部也包含了一个application.yml文件,它将被视为备用配置文件,只有在找不到外部配置文件时才会被使用。

因此,如果同一目录下既有外部的application.yml文件又有JAR包内部的application.yml文件,Spring Boot会优先使用外部的配置文件。这为你提供了一个覆盖默认配置的机会,因为外部配置文件将覆盖JAR包内部的配置文件。


SpringBoot中的配置文件与配置读取(一)
http://wahoyu.xyz/2023/09/21/SpringBoot中的配置文件与手动读取/
作者
Wahoyu
发布于
2023年9月21日
许可协议