一、多profile文件
主配置文件默认使用的是application.properties/yml。但是为了方便不同环境的配置文件需要,我们也可以以application-{profile}.properties/yml命名配置文件。例如:
另外,我们也可以利用yml支持多文档块方式实现多profile,例如:
二、激活指定profile方式
1.在配置文件中指定spring.profiles.active=profile
spring:
profiles:
active: dev3 #指定使用哪个profile
2.配置虚拟机方式:-Dspring.profiles.active=profile
-Dspring.profiles.active=dev4
3.命令行方式:java -jar xxx-SNAPSHOT.jar --spring.profiles.active=profile
--spring.profiles.active=dev1
三、项目举例
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;
}
}
application.yml:
server:
port: 8080
spring:
profiles:
active: dev3 #指定使用哪个profile
---
server:
port: 8083
spring:
profiles: dev3 #指定属于哪个profile
---
server:
port: 8084
spring:
profiles: dev4 #指定属于哪个profile
application-dev1.yml:
server:
port: 8081
application-dev2.yml:
server:
port: 8082
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).使用在配置文件中指定profile方式
(2).使用配置虚拟机方式
(3).使用命令行方式
说明:
需要说明的是,如果三个都同时配置了,那么是有优先级之分的:(3)> (2) > (1)。