一文掌握 Spring Boot Profiles
- 2019 年 10 月 3 日
- 笔记
Spring Boot Profiles ??
Profile ???????? Spring Framework ????? Spring Framework 3.1 ??????? @Profile
? Environment
??????????? Spring Boot ???????? Profiles ????????????? Spring Boot ?????????? ???? 25. Profiles ?????????? Spring Boot Profiles ??? Spring Profiles?
?????? Spring Profiles???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????Spring Profiles ????????????????????????????????????????? Profile????? Profile ???????????? Profile ??????? Bean ??? Spring ????
Spring Profiles ?????????????????????????????
?? Spring ???? Profile ????????????????????????????????????????????????????? Profile ???????????????????? Profile?
Spring Profiles ??
? Spring ?????????? Profiles?XML ????? @Profile
?
XML ???? Profile
???? XML ???????????????????????? XML ????? Bean ?????? <beans>
?? beans
????????? profile
???????????????? profile
??? dev,????? profile
??prod?
???????????? Spring XML Beans Schema ??? 4.0 ????? profile
???? XML ???????????????? Profile ?????????? Bean ????? Spring ?????????????
-
Java API ??????? Spring ????? Bean???
activeProfiles
????????? -
????????????????
-Dspring.profiles.active=dev
?? @Profiles ??Profile
?????? Profile ?????????????? @Profiles
??? @Profiles
?? @Component
?? @Configuration
????????
?? Profile ????????????? Profile ???????? Bean ?????? Spring ??? Profile ??? default????????? spring.profiles.default
?????? org.springframework.core.env.AbstractEnvironment#setDefaultProfiles
API ?????
Spring Boot Profile ??
???????????? Spring Boot ??????? Profile????? Spring Boot ???????? yml ?? properties ????? yml ???????????????????????????? application.yml
?? Profile ???????
?yml???????????? Profile?????? properties ?????????????? applications-${profile}.properties
????????????????? Profile ?????????????????????????????? Profile ???????? /enviroment
???????????????
???????????????? Profile ????? application.yml
????????????????? Profile ? prod????????????????????
???????curl ???? http://localhost:9000/enviroment
???????????
??????? active
?????? dev
??????? current app enviroment is prod
?
Spring Boot API ???? Profile
? Spring Boot ???????????? Profile ??????? Spring Boot API ?????
-
SpringApplication.setAdditionalProfiles(…)
-
SpringApplicationBuilder.profiles(...)
????????? Spring Boot API ???????????????? SpringApplication.run(...)
????????????? ?? Spring Boot API ?????Profile ???????? Profile????????????? spring.profiles.activie
??? Profile?
??
?Spring Boot ???????????? Profiles ??????? application-{profile}.properties
???????? application.properties
??? spring.profiles.active=dev
?????????????????????????????? jar -Dspring.profiles.active=prod xxx.jar
??????? Profile ?????????????????????????? Spring Boot Profiles ????????????????????????????????????????????????????????????????????????????????????????
?????????????????? Spring Boot Profiles ????????????
????
???????????????????
- springboot-actions-profiles?https://github.com/wrcj12138aaa/springboot-actions-profiles
?????
- JDK 8
- SpringBoot 2.1.6
- Maven 3.6.0
????
- How to use profiles in Spring Boot Application?http://1t.click/yUj
- Spring Boot Doc?http://1t.click/yUh
- Spring Doc?http://1t.click/yUg
- ???? Spring Profile ????https://mp.weixin.qq.com/s/0iWpGefYPqnkly4EmaPAug