Spring Boot profile文件(六)

一、多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)。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值