springboot profiles
Spring Profile 提供隔離部分配置的方法 并使它們僅僅在某些環(huán)境生效。通過(guò)@Component或@Configuration 配合 @Profile來(lái)實(shí)現(xiàn)。
@Configuration @Profile("production") public class ProductionConfiguration {// ... }通常,你是通過(guò)設(shè)置環(huán)境中的spring.profiles.active屬性來(lái)指定那些profiles生效。比如在application.properties中設(shè)置
spring.profiles.active=dev,hsqldb或通過(guò)命令行設(shè)置等。
1、添加active profile
當(dāng)多個(gè)命令同時(shí)設(shè)置spring.profiles.active屬性值的時(shí)候,高權(quán)限的命令會(huì)獲勝,也就是說(shuō),application.properties中對(duì)spring.profiles.active的設(shè)置會(huì)覆蓋默認(rèn)的,而命令行的會(huì)覆蓋application.properties中的。
但通常對(duì)于profiles不是替換他們而是添加他們,spring.profiles.include屬性作用就是添加profiles。SpringApplication同時(shí)提供了方法添加profiles,詳情見(jiàn)setAdditionalProfiles()方法。
2、程序設(shè)定profiles
在項(xiàng)目啟動(dòng)之前執(zhí)行SpringApplication.setAdditionalProfiles(…?),也可以通過(guò)ConfigurableEnvironment接口激活profiles。
3、Profiles配置文件
配置文件application.properties(或application.yml)和參照了@ConfigurationProperties注解的文件都被加載。(后面會(huì)詳細(xì)介紹)
轉(zhuǎn)載于:https://www.cnblogs.com/qins/p/7532495.html
總結(jié)
以上是生活随笔為你收集整理的springboot profiles的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 重构改善既有代码设计--重构手法11:M
- 下一篇: 第一个项目GeoQuiz开发总结