通常情况下,Spring Boot 在启动时会将 resources 目录下的 application.properties 或 apllication.yml 作为其默认配置文件,我们可以在该配置文件中对项目进行配置,如果当项目中存在多个 application.properties 或 application.yml时,那应该优先加载哪个配置文件呢?
一、默认配置文件加载顺序
当Spring Boot 项目中可以存在多个 application.properties 或 apllication.yml时,Spring Boot 启动时会扫描以下 5 个位置的 application.properties 或 apllication.yml 文件,并将它们作为 Spring boot 的默认配置文件。以下是加载默认配置文件的优先级顺序,从高到底,高优先级的配置可以覆盖低优先级的配置信息:
1> file:./config/*/
2> file:./config/
3> file:./
4> classpath:/config/
5> classpath:/
其中,file表示项目的根目录;classpath表示项目的类路径,即resources目录。如下举例:
二、项目举例
1.项目结构
2.代码实现
MainApplication.java:
package com.xj.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* @Author : xjfu
* @Date : 2022/6/8 8:38
* @Description :Spring Boot 启动类
*/
@ComponentScan("com.xj")
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
HelloController.java:
package com.xj.controller;
import com.xj.entity.PersonOne;
import com.xj.entity.PersonTwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Author : xjfu
* @Date : 2022/6/8 8:47
* @Description :
*/
@Controller
public class HelloController {
@Autowired
private PersonOne personOne;
@Autowired
private PersonTwo personTwo;
@ResponseBody
@RequestMapping(value = "/hello")
public String sayHello(){
return "Hello ! Spring Boot Project Jar Test!";
}
@ResponseBody
@RequestMapping(value = "/personOne")
public PersonOne getPersonOne(){
return personOne;
}
@ResponseBody
@RequestMapping(value = "/personTwo")
public PersonTwo getPersonTwo(){
return personTwo;
}
}
PersonOne.java:
package com.xj.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Author : xjfu
* @Date : 2022/6/24 9:01
* @Description :
*/
/**
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
* prefix = "person":配置文件中哪个下面的所有属性进行一一映射
*/
@Component
@ConfigurationProperties(prefix = "person")
public class PersonOne {
private String name;
private Integer age;
private List<String> pets;
private List<String> food;
private Child child;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<String> getPets() {
return pets;
}
public void setPets(List<String> pets) {
this.pets = pets;
}
public List<String> getFood() {
return food;
}
public void setFood(List<String> food) {
this.food = food;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
}
Child.java:
package com.xj.entity;
/**
* @Author : xjfu
* @Date : 2022/7/5 8:31
* @Description :
*/
public class Child {
private String name;
private Integer age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xj.study</groupId>
<artifactId>spring-boot-study-project</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<!--build标签描述了如何来编译及打包项目,而具体的编译和打包工作是通过build中配置的 plugin 来完成-->
<build>
<plugins>
<!--使用SpringBoot的打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
五个配置文件:
1>SpringBootStudyProject\config\release\application.yml:
person:
name: ss
age: 25
pets: [pig1,dog1,cat1]
food:
- HuiMian1
- YouPoMian1
- HeLuo1
2>SpringBootStudyProject\config\application.yml
server:
servlet:
context-path: /abc
3>SpringBootStudyProject\application.yml
server:
port: 8084
servlet:
context-path: /helloworld
4>resources\config\application.yml
server:
port: 8081
5>resources\application.yml
server:
servlet:
context-path: /hello
person:
name: dave
age: 18
pets: [pig,dog,cat]
food:
- HuiMian
- YouPoMian
- HeLuo
child:
name: ss
age: 13
gender: female
3.运行结果
各个默认配置文件的使用情况:
三、补充说明
1.如果项目打包运行的话,项目的根目录下的默认配置文件(即1>、2>、3>)是不会被打包的,所以也不会生效。只有项目的类路径,即resources目录下的默认配置文件才会被打包进去,并生效。