一、項(xiàng)目進(jìn)行多環(huán)境配置的必要性
在實(shí)際開發(fā)中,應(yīng)用程序通常需要部署到不同的運(yùn)行環(huán)境中,例如開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境等。不同的環(huán)境可能需要不同的環(huán)境配置,針對這種情況,不可能手動(dòng)變更配置文件來適應(yīng)不同的開發(fā)環(huán)境,通常需要對項(xiàng)目進(jìn)行多環(huán)境配置,Spring Boot框架提供了兩種多環(huán)境配置的方式,分別是Profile文件多環(huán)境配置和@Profile注解多環(huán)境配置。同時(shí),會(huì)額外講解在Spring Boot配置文件中設(shè)置屬性時(shí),除了可以像前面示例中顯示的配置屬性值外,還可以使用隨機(jī)值和參數(shù)間引用對屬性值進(jìn)行設(shè)置。
二、使用Profile文件進(jìn)行多環(huán)境配置
(一)創(chuàng)建Spring Boot項(xiàng)目
使用Spring Initializr模板創(chuàng)建Spring Boot項(xiàng)目——ProfileDemo01,配置好后,單擊【Next】按鈕
選擇Spring Boot版本,添加相關(guān)依賴
單擊【Create】按鈕
(二)創(chuàng)建多環(huán)境配置文件
配置文件命名格式:application-xxx.yaml
此例僅演示端口號(hào)與虛擬路徑的配置,實(shí)際應(yīng)用中可以配置許多內(nèi)容
1、全局配置文件改名
將application.properties更名為application.yaml
2、模擬開發(fā)環(huán)境
在resources里創(chuàng)建配置文件 - application-dev.yaml
# 配置服務(wù)器
server:
port: 8081
servlet:
context-path: /lzy01
3、模擬測試環(huán)境
在resources里創(chuàng)建配置文件 - application-test.yaml
# 配置服務(wù)器
server:
port: 8082
servlet:
context-path: /lzy02
4、模擬生產(chǎn)環(huán)境
在resources里創(chuàng)建配置文件 - application-prod.yaml
# 配置服務(wù)器
server:
port: 8083
servlet:
context-path: /lzy03
(三)創(chuàng)建控制器
在net.army.boot包里創(chuàng)建controller子包,在子包里創(chuàng)建ProfileController類
package net.army.boot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 作者:梁辰興
* 日期:2023/5/31
* 功能:概況控制器
*/
@RestController
public class ProfileController {
@GetMapping("/welcome")
public String welcome() {
return "<h1 style='color: red; text-align: center'>歡迎訪問Spring Boot世界~</h1>";
}
}
(四)采用默認(rèn)環(huán)境
默認(rèn)采用配置文件application.yaml,啟動(dòng)服務(wù)器,訪問:http://localhost:8080/welcome
(五)指定使用環(huán)境
方式1、使用配置文件全局指定使用環(huán)境
(1) 在全局配置文件里指定當(dāng)前使用環(huán)境 - 開發(fā)環(huán)境
spring: profiles: active: dev
,表明當(dāng)前生效的環(huán)境配置文件是application-dev.yaml
啟動(dòng)項(xiàng)目,查看采用的使用環(huán)境:服務(wù)器端口號(hào)與虛擬路徑
訪問:http://localhost:8081/lzy01/welcome
(2) 在全局配置文件里指定當(dāng)前使用環(huán)境 - 測試環(huán)境
spring: profiles: active: test
,表明當(dāng)前生效的環(huán)境配置文件是application-test.yaml
啟動(dòng)項(xiàng)目,查看采用的使用環(huán)境:服務(wù)器端口號(hào)與虛擬路徑
訪問:http://localhost:8082/lzy02/welcome
(3) 在全局配置文件里指定當(dāng)前使用環(huán)境 - 生產(chǎn)環(huán)境
spring: profiles: active: prod
,表明當(dāng)前生效的環(huán)境配置文件是application-prod.yaml
啟動(dòng)項(xiàng)目,查看采用的使用環(huán)境:服務(wù)器端口號(hào)與虛擬路徑
訪問:http://localhost:8083/lzy03/welcome
方式2、通過命令行方式指定使用環(huán)境
(1)使用IDEA將Maven項(xiàng)目打成jar包
Maven - ProfileDemo01 - LifeCycle - package,單擊右鍵,在快捷菜單里執(zhí)行“Run Maven Build”
(2)在終端執(zhí)行jar包,選擇使用環(huán)境 - 開發(fā)環(huán)境
D:\Projects\SpringBootProjects\ProfileDemo01> cd target
D:\Projects\SpringBootProjects\ProfileDemo01\target> java -jar ProfileDemo01-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
(3)在終端執(zhí)行jar包,選擇使用環(huán)境 - 測試環(huán)境
D:\Projects\SpringBootProjects\ProfileDemo01> cd target
D:\Projects\SpringBootProjects\ProfileDemo01\target> java -jar ProfileDemo01-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
(4)在終端執(zhí)行jar包,選擇使用環(huán)境 - 生產(chǎn)環(huán)境
D:\Projects\SpringBootProjects\ProfileDemo01> cd target
D:\Projects\SpringBootProjects\ProfileDemo01\target> java -jar ProfileDemo01-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
三、使用@Profile注解進(jìn)行多環(huán)境配置
如果項(xiàng)目可能用到三種不同的數(shù)據(jù)庫環(huán)境,比如MySQL、Oracle和Sybase,那么我們?nèi)绾卫聾Profile注解來實(shí)現(xiàn)多數(shù)據(jù)庫環(huán)境配置呢?
(一)創(chuàng)建Spring Boot項(xiàng)目ProfileDemo02
使用Spring Initializr模板創(chuàng)建Spring Boot項(xiàng)目——ProfileDemo02,配置好后,單擊【Next】按鈕
選擇Spring Boot版本,添加相關(guān)依賴
單擊【Create】按鈕
將項(xiàng)目進(jìn)行熱部署
(二)創(chuàng)建數(shù)據(jù)庫配置接口DatabaseConfig
在net.army.boot里創(chuàng)建config子包,在子包里創(chuàng)建DatabaseConfig接口
package net.army.boot.config;
/**
* 作者:梁辰興
* 日期:2023/5/31
* 功能:數(shù)據(jù)庫配置接口
*/
public interface DatabaseConfig {
void connect();
}
(三)創(chuàng)建三個(gè)數(shù)據(jù)庫配置實(shí)現(xiàn)類
1、創(chuàng)建數(shù)據(jù)庫配置實(shí)現(xiàn)類MySQLConfig
在net.army.boot.config包里創(chuàng)建impl子包,在子包里創(chuàng)建MySQLConfig類
package net.army.boot.config.impl;
import net.army.boot.config.DatabaseConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
/**
* 作者:梁辰興
* 日期:2023/5/31
* 功能:MySQL數(shù)據(jù)庫配置實(shí)現(xiàn)類
*/
@Configuration // 標(biāo)識(shí)為配置類
@Profile("mysql") // 指定使用環(huán)境名稱
public class MySQLConfig implements DatabaseConfig {
@Override
public void connect() {
System.out.println("項(xiàng)目使用MySQL數(shù)據(jù)庫環(huán)境~");
}
}
2、創(chuàng)建數(shù)據(jù)庫配置實(shí)現(xiàn)類OracleConfig
在net.army.boot.config.impl包里創(chuàng)建OracleConfig類
package net.army.boot.config.impl;
import net.army.boot.config.DatabaseConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
/**
* 作者:梁辰興
* 日期:2023/5/31
* 功能:Oracle數(shù)據(jù)庫配置類
*/
@Configuration // 標(biāo)識(shí)為配置類
@Profile("oracle") // 指定使用環(huán)境名稱
public class OracleConfig implements DatabaseConfig {
@Override
public void connect() {
System.out.println("項(xiàng)目使用Oracle數(shù)據(jù)庫環(huán)境~");
}
}
3、創(chuàng)建數(shù)據(jù)庫配置實(shí)現(xiàn)類SybaseConfig
在net.army.boot.config.impl包里創(chuàng)建SybaseConfig類
package net.army.boot.config.impl;
import net.army.boot.config.DatabaseConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
/**
* 作者:梁辰興
* 日期:2023/5/31
* 功能:Sybase數(shù)據(jù)庫配置類
*/
@Configuration // 標(biāo)識(shí)為配置類
@Profile("sybase") // 指定使用環(huán)境名稱
public class SybaseConfig implements DatabaseConfig {
@Override
public void connect() {
System.out.println("項(xiàng)目使用Sybase數(shù)據(jù)庫環(huán)境~");
}
}
(四)在全局配置文件里設(shè)置使用環(huán)境
在全局配置文件application.properties里配置使用環(huán)境
(五)打開測試類,編寫測試方法
打開自帶的測試類ProfileDemo02ApplicationTests
注入數(shù)據(jù)配置實(shí)體,調(diào)用數(shù)據(jù)庫配置實(shí)體的方法
package net.army.boot;
import net.army.boot.config.DatabaseConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ProfileDemo02ApplicationTests {
@Autowired // 注入數(shù)據(jù)庫配置實(shí)體
private DatabaseConfig databaseConfig;
@Test
void contextLoads() {
// 調(diào)用數(shù)據(jù)庫配置實(shí)體的方法
databaseConfig.connect();
}
}
(六)運(yùn)行測試方法,查看結(jié)果
運(yùn)行contextLoads()測試方法,查看結(jié)果
練習(xí)
1、設(shè)置使用環(huán)境 - MySQL數(shù)據(jù)庫環(huán)境,并測試
2、設(shè)置使用環(huán)境 - Sybase數(shù)據(jù)庫環(huán)境,并測試
四、隨機(jī)值設(shè)置以及參數(shù)間引用
(一)創(chuàng)建Spring Boot Web項(xiàng)目RandomSetDemo
使用Spring Initializr模板創(chuàng)建Spring Boot項(xiàng)目
單擊【Next】按鈕,選擇Spring Boot版本,添加相關(guān)依賴
單擊【Create】按鈕
(二)在全局配置文件里配置隨機(jī)值
在application.properties文件里配置
#一個(gè)隨機(jī)值
lzy.value=${random.value}
#一個(gè)隨機(jī)整數(shù)
lzy.integer=${random.int}
#一個(gè)長整型隨機(jī)數(shù)
lzy.long=${random.long}
#獲取一個(gè)隨機(jī)UUID值
lzy.uuid=${random.uuid}
#小于10的隨機(jī)整數(shù)
lzy.number.less=${random.int(10)}
#隨機(jī)產(chǎn)生1024至65535之間的數(shù)
lzy.number.range=${random.int[1024,65535]}
(三)在測試類里測試配置的隨機(jī)數(shù)
打開自帶的測試類 - RandomSetDemoApplicationTests
1、測試隨機(jī)數(shù)my.number
注入配置文件里的屬性
輸出配置文件里的屬性
package net.army.boot;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RandomSetDemoApplicationTests {
// 注入配置文件里的隨機(jī)值
@Value("${lzy.value}")
private String value;
@Test
public void testRandomValue() {
// 輸出配置文件里的隨機(jī)值
System.out.println("隨機(jī)值:" + value);
}
}
運(yùn)行testRandonValue()方法,查看結(jié)果大家可以看到,產(chǎn)生的是32位的十六進(jìn)制數(shù)對應(yīng)的字符串,思考一下,能否將其變成128位的二進(jìn)制串?
2、測試隨機(jī)整數(shù)my.integer
注入配置文件里的屬性
輸出配置文件里的屬性
運(yùn)行testRandomInteger()方法,查看結(jié)果
再運(yùn)行testRandomInteger()方法,查看結(jié)果
練習(xí)
1、測試長整型隨機(jī)數(shù)my.long
2、測試隨機(jī)UUID值my.uuid
3、測試小于10的隨機(jī)整數(shù)my.number.less
4、測試1024至65535之間的隨機(jī)整數(shù)my.number.range
(四)演示參數(shù)間的引用
在appication.properties文件里,后定義的屬性可引用前面定義的屬性
定義三個(gè)屬性year、month和day
定義屬性user.name
定義屬性user.birthday,引用屬性year、month和day
# 配置日期數(shù)據(jù)
year=2001
month=8
day=28
# 配置用戶信息
user.name=梁辰興
user.birthday=${year}年${month}月${day}日
在測試類里編寫testUser()方法
運(yùn)行testUser()方法,查看結(jié)果
有點(diǎn)問題,明明在配置文件里,user.name=梁辰興,但結(jié)果并非如此,原因何在?
user.name得到是系統(tǒng)用戶名,而不是配置文件里定義的用戶名。文章來源:http://www.zghlxwxcb.cn/news/detail-477938.html
修改配置文件,不使用user前綴,改用person前綴
修改測試代碼
運(yùn)行testUser()方法,查看結(jié)果文章來源地址http://www.zghlxwxcb.cn/news/detail-477938.html
到了這里,關(guān)于Spring Boot 多環(huán)境配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!