創(chuàng)建一個(gè)spring boot 項(xiàng)目
基于 Spring Boot 創(chuàng)建 Web 應(yīng)用程序的方法有很多,我們選擇在idea中直接進(jìn)行創(chuàng)建,服務(wù)器URL選擇Spring Initializer 網(wǎng)站,類型選擇Maven項(xiàng)目,java版本根據(jù)jdk版本進(jìn)行選擇。
然后添加相應(yīng)依賴以及選擇spring boot版本
接下來(lái)我們寫一個(gè)Controller
@RestController
@RequestMapping("/user")
public class UserController {
// 請(qǐng)求映射,用于處理請(qǐng)求
@RequestMapping("/zcx")
public Userinfo getUserInfo() {
// 創(chuàng)建Userinfo對(duì)象
Userinfo userinfo = new Userinfo();
// 設(shè)置age屬性
userinfo.setAge("45");
// 設(shè)置name屬性
userinfo.setName("zcx-yyds");
// 返回Userinfo對(duì)象
return userinfo;
}
}
現(xiàn)在我們需要對(duì)這個(gè)應(yīng)用程序進(jìn)行打包,使用idea上的打包工具,點(diǎn)擊package進(jìn)行打包
我們將得到一個(gè)springboot1-0.0.1-SNAPSHOT.jar文件,而這個(gè) jar 文件就是可以直接運(yùn)行的可執(zhí)行文件,內(nèi)置了 Tomcat Web 服務(wù)器。我們直接使用如下命令進(jìn)行運(yùn)行這個(gè)Spring boot程序。
java -jar springboot1-0.0.1-SNAPSHOT.jar
然后我們使用postman來(lái)進(jìn)行項(xiàng)目訪問(wèn)測(cè)試,得到如下返回結(jié)果,證明我們的程序服務(wù)已經(jīng)啟動(dòng)成功了。
現(xiàn)在我們已經(jīng)明白如何構(gòu)建、打包以及運(yùn)行一個(gè)簡(jiǎn)單的 Web 應(yīng)用程序了。
spring boot 中的配置體系
在 Spring Boot 中,其核心設(shè)計(jì)理念是對(duì)配置信息的管理采用約定優(yōu)于配置,也就是說(shuō)約定大于配置。
Spring Boot中的Profile是一個(gè)非常有用的功能,它可以讓我們?cè)诓恍薷拇a的情況下,通過(guò)配置文件來(lái)控制程序的行為。
在Spring Boot中,Profile是一種用于控制應(yīng)用程序行為的機(jī)制。通過(guò)使用不同的Profile,我們可以根據(jù)不同的環(huán)境或場(chǎng)景來(lái)加載不同的配置信息,從而實(shí)現(xiàn)應(yīng)用程序的靈活配置。
配置文件與 Profile
根據(jù)環(huán)境的不同而存在很多套配置。假設(shè)我們存在如下所示的配置文件集合:
配置文件application-dev.properties中的代碼為:
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db
spring.datasource.username=root
spring.datasource.password=666666
配置文件application-prod.properties中的代碼為:
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/prod_db
spring.datasource.username=root
spring.datasource.password=666666
常見的配置文件命名方式有以下幾種:
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
在 Spring Boot 中,我們可以在主 application.properties 中使用如下的配置方式來(lái)激活當(dāng)前所使用的 Profile:
spring.profiles.active = dev
當(dāng)然還有以下幾種方式啟動(dòng)Profile:
- 在主 application.properties指定要激活的Profile。
spring.profiles.active = dev
- 在啟動(dòng)命令中指定Profile。
java -jar springboot1-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
- 在程序中顯示設(shè)置Profile。例如:
@SpringBootApplication
public class Springboot1Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Springboot1Application.class);
app.setAdditionalProfiles("dev");
app.run(args);
}
}
這將激活名為prod的Profile。
代碼控制與Profile
在 Spring Boot 中,Profile 這一概念的應(yīng)用場(chǎng)景還包括動(dòng)態(tài)控制代碼執(zhí)行流程。為此,我們需要使用 @Profile 注解,Profile注解可以用于指定某個(gè)類或方法在特定的配置環(huán)境下生效。只要是被@Component
或@Configuration
注解的類都可以使用@Profile
注解。
使用一個(gè)示例來(lái)了解Profile注解的使用方法:
@Configuration
public class DataSourceConfig {
@Bean
@Profile("dev")
public DataSource devDataSource() {
//創(chuàng)建 dev 環(huán)境下的 DataSource
return null;
}
@Bean()
@Profile("prod")
public DataSource prodDataSource() {
//創(chuàng)建 prod 環(huán)境下的 DataSource
return null;
}
}
通過(guò)這種方式,可以達(dá)到與使用配置文件相同的效果。
我們來(lái)看一個(gè)更為詳細(xì)具體的例子:
- 創(chuàng)建一個(gè)接口
MyService
:
public interface MyService {
void doSomething();
}
- 創(chuàng)建兩個(gè)不同的實(shí)現(xiàn)類,分別用于不同的配置文件。
@Component
@Profile("prod")
public class ProdMyService implements MyService {
@Override
public void doSomething() {
System.out.println("Prod service is running.");
}
}
@Component
@Profile("dev")
public class DevMyService implements MyService {
@Override
public void doSomething() {
System.out.println("Dev service is running.");
}
}
上述示例中,我們創(chuàng)建了兩個(gè)不同的實(shí)現(xiàn)類,一個(gè)用于 development
配置文件,另一個(gè)用于 production
配置文件。@Profile
注解分別標(biāo)記了它們,以便 Spring 知道在哪個(gè)配置文件下激活它們。
3. 在 Spring 配置文件(例如 application.properties
或 application.yml
)中指定要激活的配置文件,例如: application.properties
:
spring.profiles.active=prod
將 spring.profiles.active
設(shè)置為 development
,表示我們希望激活的開發(fā)配置文件。
4. 創(chuàng)建一個(gè)啟動(dòng)類,以演示如何使用 MyService
:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-708285.html
@SpringBootApplication
public class Springboot1Application implements CommandLineRunner {
// 聲明一個(gè)注入的MyService對(duì)象
@Autowired
private MyService myService;
// 聲明一個(gè)名為run的方法,用來(lái)啟動(dòng)Spring應(yīng)用
public static void main(String[] args) {
// 調(diào)用SpringApplication的run方法,傳入Springboot1Application類和args參數(shù)
SpringApplication.run(Springboot1Application.class, args);
}
// 方法run,用來(lái)執(zhí)行Spring應(yīng)用的業(yè)務(wù)邏輯
@Override
public void run(String... args) {
// 調(diào)用myService的doSomething方法
myService.doSomething();
}
}
通過(guò)結(jié)果我們發(fā)現(xiàn)運(yùn)行這個(gè)應(yīng)用程序時(shí),它會(huì)根據(jù)配置文件中的 spring.profiles.active
屬性來(lái)選擇相應(yīng)的實(shí)現(xiàn)類。
如果你還想了解更多內(nèi)容請(qǐng)參考spring boot官網(wǎng)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-708285.html
到了這里,關(guān)于解析Spring Boot中的Profile:配置文件與代碼的雙重掌控的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!