除了使用默认配置文件,我们还可以以外部配置文件的方式来指定配置文件信息。
一、外部配置文件的方式
1.spring.config.location=xxxx.yml/properties
使用该方式指定配置文件后,会使项目默认配置文件(application.properties 或 application.yml )失效,Spring Boot 将只加载指定的外部配置文件。例如:
java -jar spring-boot-study-project-1.0-SNAPSHOT.jar
--spring.config.location=D:\myConfig\application.yml
指定使用d盘myConfig文件夹下面的application.yml配置文件,且项目的默认配置文件会失效。
2.spring.config.additiaonal-location=xxx.yml/properties
这种方式与spring.config.location不同,不会使项目默认的配置文件失效,使用spring.config.addtional-location命令行参数添加的外部配置文件会与项目默认的配置文件共同生效,形成互补配置,且其优先级是最高的,比所有默认配置文件的优先级都高。例如:
java -jar spring-boot-study-project-1.0-SNAPSHOT.jar
--spring.config.additional-location=D:\myConfig\application.yml
指定使用d盘myConfig文件夹下面的application.yml配置文件,但是项目的默认配置文件不会失效。
3.配置信息在命令行上指定
这种方式直接在执行jar文件时直接指定配置信息,且不会使项目默认的配置文件失效,会与项目默认的配置文件共同生效,形成互补配置,且其优先级是最高的,比所有默认配置文件的优先级都高。例如:
java -jar spring-boot-study-project-1.0-SNAPSHOT.jar --server.port=8088
--server.servlet.context-path=/abc
指定端口号为8088,访问地址为/abc。
二、项目举例
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.PersonFour;
import com.xj.entity.PersonOne;
import com.xj.entity.PersonThree;
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;
@Autowired
private PersonThree personThree;
@Autowired
private PersonFour personFour;
@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;
}
@ResponseBody
@RequestMapping(value = "/personThree")
public PersonThree getPersonThree(){
return personThree;
}
@ResponseBody
@RequestMapping(value = "/personFour")
public PersonFour getPersonFour(){
return personFour;
}
}
config\application.yml:
server:
port: 8081
servlet:
context-path: /helloworld1
application.yml:
server:
port: 8082
servlet:
context-path: /helloworld2
D:\myConfig\application.yml:
server:
port: 8083
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>
3.运行结果
(1)使用spring.config.location=xxxx.yml/properties方式
(2)使用spring.config.additiaonal-location=xxx.yml/properties方式