SpringBoot的pom.xml文件中設(shè)置多環(huán)境配置信息方法詳解
前言
Java項(xiàng)目開發(fā)中會(huì)用到多種中間件,比如MySQL、Redis、RocketMQ等,每個(gè)項(xiàng)目又存在開發(fā)環(huán)境、測(cè)試環(huán)境、驗(yàn)收環(huán)境、生產(chǎn)環(huán)境等。有種方案是把這些配置信息放到pom.xml文件中,方便Jenkins進(jìn)行打包,也方便開發(fā)。
1. 示例代碼結(jié)構(gòu)

2. 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
</parent>
<groupId>vip.buddha</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<profiles>
<!-- 開發(fā)環(huán)境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<db.url>jdbc:mysql://localhost:3306/dev_db</db.url>
<db.username>dev_user</db.username>
<db.password>dev_password</db.password>
</properties>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<id>prod</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<db.url>jdbc:mysql://localhost:3306/prod_db</db.url>
<db.username>prod_user</db.username>
<db.password>prod_password</db.password>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<!-- 添加以下配置,明確包含YAML文件 -->
<includes>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>${}</delimiter> <!-- 使用 ${} 作為占位符 -->
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters> <!-- 禁用默認(rèn)的 @..@ -->
</configuration>
</plugin>
</plugins>
</build>
</project>
3. application.yml文件
spring:
datasource:
url: ${db.url}
username: ${db.username}
password: ${db.password}
4. TestController文件
package vip.buddha.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@RequestMapping("/test")
public void test() {
System.out.println("url:" + url);
System.out.println("username:" + username);
System.out.println("password:" + password);
}
}5. Main文件
package vip.buddha;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}6. 驗(yàn)證結(jié)果

通過上面步驟,啟動(dòng)項(xiàng)目

啟動(dòng)后,target/classes下的application.yml文件內(nèi)容就被替換過來了。

訪問http://localhost:8080/test后,配置信息就在接口控制臺(tái)按照預(yù)期給顯示出來。如果想測(cè)試另外的一套配置信息,maven這邊勾選另外的配置即可。

總結(jié)
到此這篇關(guān)于SpringBoot的pom.xml文件中設(shè)置多環(huán)境配置信息方法的文章就介紹到這了,更多相關(guān)SpringBoot pom.xml多環(huán)境配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
完美解決idea創(chuàng)建文件時(shí),文件不分級(jí)展示的情況
這篇文章主要介紹了完美解決idea創(chuàng)建文件時(shí),文件不分級(jí)展示的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java 如何將表格數(shù)據(jù)導(dǎo)入word文檔中
這篇文章主要介紹了Java將表格數(shù)據(jù)導(dǎo)入word文檔中的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序
最近在學(xué)習(xí)Java,然后上個(gè)月迷上了九宮格數(shù)獨(dú),玩了幾天,覺得實(shí)在有趣,就想著能不能用編程來解決,于是就自己寫了個(gè),還真解決了。下面這篇文章就給大家主要介紹了Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序,需要的朋友可以參考借鑒。2017-01-01
Java靜態(tài)方法和實(shí)例方法區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了Java靜態(tài)方法和實(shí)例方法的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

