What are the parameters and configuration of the spring boot externalized configuration source code

SpringBoot Wai Hua Pei Zhi Yuan Ma De Can Shu Ji Pei Zhi Shi Zen Yang De , Zhen Dui Zhe Ge Wen Ti , Zhe Pian Wen Zhang Xiang Xi Jie Shao Liao Xiang Dui Ying De Fen Xi He Jie Da , Xi Wang Ke Yi Bang Zhu Geng Duo Xiang Jie Jue Zhe Ge Wen Ti De Xiao Huo Ban Zhao Dao Geng Jian Dan Yi Xing De Fang Fa 。

Zong He Shi Zhan

Wo Men Jiang Jie Liao Guan Yu Spring Boot Wai Hua Pei Zhi De Yuan Li Ji Yuan Ma Fen Xi , Wo Men Jiang Tong Guo Yi Ge Ju Ti De Li Zi Lai Jian Dan Yan Shi Zai Spring Boot   Zhong Ru He Shi Yong Bu Tong Lei Xing De Can Shu Ji Pei Zhi 。 Wo Men Hui Yong Dao Ming Ling Xing Chuan Di Can Shu 、 Mo Ren Pei Zhi Wen Jian application.properties Ji Ji Yu profile Pei Zhi Can Shu 、@Value   Zhu Jie Huo Qu Can Shu 、 Ji Yu Lei Xing An Quan De @ConfigurationProperties Zhu Jie Guan Lian Bean Deng Gong Neng 。

You Yu Spring Boot Yi Jing Dui Wai Hua Pei Zhi Jin Xing Liao Jian Hua Chu Li , Wo Men Zai Shi Jian Zhong Shi Yong Qi Lai Shi Fei Chang Fang Bian De 。 Zhe Li Wo Chuang Jian Liao Yi Ge Biao Zhun De  Spring Boot Xiang Mu , Ban Ben Cai Yong 2.2.1.RELEASE。 Shou Xian Wo Men Kan Yi Xia Xiang Mu De Mu Lu Jie Gou 。

SpringBoot Wai  Hua  Pei  Zhi  Yuan  Ma  De  Can  Shu  Ji  Pei  Zhi  Shi  Zen  Yang  De

Zai pom.xml Zhong Yin Ru De He Xin Yi Lai Wei spring-boot-starter-web, Dui Ying Yi Lai Yuan Ma Ru Xia 。

<dependency> <groupId>org. springframework . boot</ groupId> <artifactId>spring- boot- starter-web</artifactId> </ dependency>

SpringbootConfigApplication Lei Wei Spring Boot Xiang Mu De Qi Dong Lei , Wo Men Bu Zai Zuo Guo Duo Jie Shao 。

ConfigController Lei Wei Jie Shou Qing Qiu De Controller, Zai Qi Nei Bu Ding Yi Liao Yi Ge Mo Ren De getConfigParams   Fang Fa , Zai Gai Fang Fa Nei Da Yin Liao Bu Tong Tu Jing Huo De De Can Shu Zhi , Xiang Guan Yuan Ma Ru Xia 。

@RestController public class ConfigController { @Value( "${user . username}") private String username ; @Value("${user . password}") private String password; @Resource private LoginUserConfig loginUserConfig; @Value("${projectName :unknown}") private String projectName ; @RequestMapping("/") public String getConfigParams() { // Qi  Dong  Ming  Ling  Chuan  Di  Can  Shu  System. out . println("Command config projectName:" + projectName);// Tong  Guo  appl ication  Pei  Zhi  Wen  Jian  Pei  Zhi  De  Can  Shu  System. out . println("Application config Username System. out . println("Application config Password :”+ password); // Tong  Guo @ConfigurationProperties  Zhu  Jie  Pei  Zhi  De  Can  Shu  System. out. println("ConfigurationProperties config Username :”+ login UserConfig. getUsername()); System . out . println("Configurat ionProperties config Password :”+ login UserConfig. getPassword()); return ""; }

Qi Zhong Tong Guo @RestController Zhu Jie Zhi Ding Gai Lei Wei Ke Jie Shou Qing Qiu De Controller, Bing Jin Xing Shi Li Hua 。 Zai Gai Lei Nei Bu Fen Bie Tong Guo @Value   Zhu Jie 、@Resource Zhu Jie Lai Huo Qu Bu Tong Tu Jing She Zhi De Can Shu 。 Tong Guo getConfigParams Fang Fa Dui Wai Ti Gong Fang Wen Qing Qiu ,   Dang Qian Jie Shou Dao Qing Qiu Zhi Hou Hui Da Yin Bu Tong Tu Jing Huo De Can Shu De Zhi 。

Shou Xian Wo Men Lai Kan Tong Guo @Value Huo Qu Dao De Zhi De Lai Yuan , Zai Gai Shi Li Zhong You Liang Ge Tu Jing Lai She Zhi Dui Ying De Zhi :

application.properties Pei Zhi Wen Jian He Ming Ling Xing Can Shu 。

Guan Yu Ming Ling Xing Can Shu , Wo Men Zhi Qian Ye Yi Jing Ti Dao Guo , Ji Ben Chuan Di Fang Shi Jiu Shi Zai Zhi Xing Qi Dong Xiang Mu De Ming Ling Shi Tong Guo “ Yi name=value'   De Xing Shi Jin Xing Chuan Di 。 Jie He Bing Shi Li , Chuan Di Fang Shi Ru Xia 。

java -jar springboot-config-0. 0.1- SNAPSHOT. jar -- projectName=SpringBoot

Zai ConfigController Lei Zhong , Wo Men Ke Yi Kan Dao @Value   De Shi Yong Ji Ben Ge Shi Wei @Value("${param}"), Dan Zhen Dui Ming Ling Xing Can Shu Huo Qu Shi Wo Men Cai Yong Liao @Value("${param:default}") Fang Shi 。 Zai Shi Jian Zhong Zhe Liang Zhong Fang Shi Du Bi Jiao Chang Yong , Er Di Er Zhong Tong Guo Mao Hao Fen Ge Fu Jin Xing Chuan Di Mo Ren Zhi , Dang  param Can Shu Bu Cun Zai Huo Wei Zai application Zhong Pei Zhi Shi , Hui Shi Yong Zhi Ding De Mo Ren Zhi 。

Yi Dang Qian Shi Li Wei Li , Ru Guo Qi Dong Ming Ling Zhong Wei Zhi Ding projectName Can Shu , Tong Shi @Value   Huo Qu Shi Ye Wei Zhi Ding Mo Ren Zhi "unknown", Na Me Zai Zhi Xing Qi Dong Ming Ling Shi Bian Hui Pao Chu Yi Chang Wu Fa Qi Dong 。 Zhe Shi Wo Men Zai Shi Yong @Value De Guo Cheng Zhong Xu Yao Zhu Yi De Yi Zhong Qing Kuang 。

Guan Yu application.properties Pei Zhi Wen Jian Zhong Can Shu De She Zhi Geng Jian Dan , Zhi Jie Zai Dui Ying Wen Jian Zhong She Zhi Dui Ying De key=value Zhi Ji Ke , Bi Ru Ben Li Zhong  application.properties Zhong De Pei Zhi Yuan Ma Ru Xia 。

# Gong  Gong  Pei  Zhi , Ren  He  Huan  Jing  Qi  Dong  Jun  Cai  Yong  8080  Duan  server. port=8080 spring. profiles . active=dev

Dan Zai Shi Jian De Guo Cheng Zhong , Wo Men Jing Chang Hui Yu Dao Bu Tong Huan Jing Xu Yao Bu Tong Pei Zhi Wen Jian De Qing Kuang , Ru Guo Mei Huan Yi – Ge Huan Jing Jiu Zhong Xin Xiu Gai Pei Zhi Wen Jian Huo Zhong Xin Da Bao Yi Ci Hui Bi Jiao Ma Fan , Zhe Shi Jiu Ke Yi Yong Spring  Boot Ti Gong De Profile Pei Zhi Gong Neng Lai Jie Jue Wen Ti Liao 。 Er Wo Men Shi Li Zhong Ti Gong De 3 Ge properties Pei Zhi Wen Jian Jiu Shi Wei Liao Zhan Shi Profile   Pei Zhi De Ji Ben Shi Yong 。

Tong Chang Qing Kuang Xia , Xiang Mu Zhong Gen Ju Huan Jing De Duo Shao Hui Chuang Jian 1 Ge Dao Duo Ge properties Pei Zhi Wen Jian , Yi Ban Qing Kuang Xia Ta Men Dui Ying De Ming Ming Ge Shi He Xiang Guan Gong Neng Ru Xia 。

  • *applcation.properties: Gong Gong Pei Zhi 。

  • *application-dev.properties: Kai Fa Huan Jing Pei Zhi 。

  • .application-test.properties: Ce Shi Huan Jing Pei Zhi 。

  • application-prod.properties: Sheng Chan Huan Jing Pei Zhi 。

Dang Ran , Ming Ming Zhong De “dev'"test He "prod” Shi Ke Yi Zi Ding Yi De , Er Zhe Xie Pei Zhi Zai Shi Me Shi Hou Hui Bei Shi Yong , Ze Ke Tong Guo Ji Huo application.properties   Pei Zhi Wen Jian Zhong De spring.profiles. active Can Shu Lai Kong Zhi 。

Bi Ru , Zai applcation.properties Zhong Jin Xing Gong Gong Pei Zhi , Ran Hou Tong Guo Ru Xia Pei Zhi Ji Huo Zhi Ding Huan Jing De Pei Zhi 。

spring. profiles.active = prod

Qi Zhong “prod” Dui Zhao Wen Jian Ming Zhong application-prod.properties。Spring Boot Zai Chu Li Shi Hui Huo Qu Pei Zhi Wen Jian  applcation.properties, Ran Hou Tong Guo Zhi Ding De profile De Zhi “prod" Jin Xing Pin Jie , Huo   De application-prod.properties Wen Jian De Ming Cheng He Lu Jing 。 Ju Ti Jia Zai Pin Jie De Bu Zhou He Yuan Li , Wo Men Zai Qian Mian De Zhang Jie Zhong Yi Jing Jiang Guo , Ke Dui Zhao Shi Li Hui Gu Yi Xia 。

Zai Shang Shu Shi Li Zhong , Wo Men Ji Huo Liao dev De Pei Zhi Huan Jing ,application-dev.properties Zhong De Pei Zhi Ru Xia 。

# Ce  Shi  Huan  Jing  Yong  Hu  Ming  He  Zhang  Hu  user. username=test - admin user. password=test-pwd

Ci Shi , Tong Guo Fang Wen Dui Ying De Qing Qiu ,getConfigParams Fang Fa Zhong Dui Ying Da Yin De Ri Zhi Ru Xia 。

Application config Username : test- admin Application config Password : test - pwd

Ru Guo Xiang Ji Huo Sheng Chan Huan Jing De Pei Zhi , Zhi Xu Zai application.properties Zhong Pei Zhi spring.profiles. active=prod   Ji Ke 。

@Value Can Shu Zhi De Huo Qu He Ji Yu Profile De Can Shu Pei Zhi Wo Men Jiu Tuo Zhan Zhe Me Duo ,@Value   De Shi Yong Huan Bao Kuo Zhu Ru Pu Tong Zi Fu Chuan 、 Cao Zuo Xi Tong Shu Xing 、 Biao Da Shi Jie Guo 、 Wen Jian Zi Yuan 、URL Zi Yuan Deng Nei Rong , Da Jia Ke Cha Yue Guan Fang Wen Dang He Xiang Guan Shi Li Jin Yi Bu Xue Xi 。

Zai Shang Shu @Value Shi Yong Zhong , Wo Men Ke Yi Dui Dan Ge Shu Xing Jin Xing Zhu Ru Pei Zhi , Dan Ru Guo You Hen Duo Pei Zhi Shu Xing Huo Zhe Pei Zhi Shu Xing Ben Shen Yong You Ceng Ji Jie Gou , Bian Xian De Bu Gou Fang Bian Ling Huo 。 Yin Ci ,Spring  Boo Ti Gong Liao Ji Yu Lei Xing An Quan De Pei Zhi Fang Shi 。

Zai ConfigController Zhong Wo Men Tong Guo @Resource Zhu Ru Liao Yi Ge LoginUserConfig   Lei , Gai Lei Bian Shi Tong Guo @ConfigurationProperties Zhu Jie Jiang properties Shu Xing He LoginUserConfig   De Shu Xing Jin Xing Guan Lian , Cong Er Shi Xian Lei Xing An Quan Pei Zhi 。LoginUserConfig De Yuan Ma Ru Xia 。

@Component@Configurat ionProperties(prefix = "user") public class LoginUserConfig { private String username ; private String password; // Sheng  Lue  getter/setter  Fang  Fa  }

Zai LoginUserConfig Lei De Yuan Dai Ma Zhong , Tong Guo @ConfigurationProperties Zhu Jie Zhi Ding Zai Shi Li Hua Shi Jiang Qian Zhui Wei user   De Pei Zhi Shu Xing Bang Ding Dao LoginUserConfig Lei De Dui Ying Shu Xing Shang , Er Tong Guo @Component Jiang Gai Lei Shi Li Hua 。

Zhe Li You Yu Zhi Ding Pei Zhi Wen Jian Wei dev , Ze Hui Jiang Shang Shu dev Pei Zhi Wen Jian Zhong De user.username   He user.password De Zhi Fen Bie Bang Ding Dao LoginUserConfig Lei De username He password Shu Xing Shang 。 Er Zai  ConfigController Zhong Zhu Ru Zhi Hou , Bian Ke Huo De Dui Ying De Shu Xing Zhi 。 Tong Yang Zai Zhi Xing Qing Qiu Shi ,getConfigParams   Fang Fa Zhong Dui Ying Da Yin De Ri Zhi Ru Xia 。

  • ConfigurationProperties config Username : test – admin

  • ConfigurationProperties config Password : test- pwd

Shang Shu Shi Li Zhi Yan Shi Liao @ConfigurationProperties Bang Ding Shu Xing De Yi Zhong Qing Kuang ,Spring Boot Jiang Environment   Shu Xing Bang Ding Dao @ConfigurationProperties Biao Zhu De Bean Shi , Huan Ke Yi Shi Yong Yi Xie Kuan Song De Gui Ze , Ye Jiu Shi Shuo Environment Shu Xing Ming He Bean   Shu Xing Ming Bu Xu Yao Jing Que Pi Pei 。

Bi Ru Zai Dui Xiang User Zhong You Yi – Ge firstName Shu Xing , Na Me Zai Pei Zhi Wen Jian Zhong Dui Ying Ru Xia Pei Zhi Xiang Jun Hui Pi Pei 。

  • user. firstName // Biao Zhun Tuo Feng Ming Ming Yu Fa

  • user. first-name // Duan Heng Xian Ge Kai Biao Shi , Tui Jian Yong Yu . properties He . yml Wen Jian Zhong

  • user. first_ name // Xia Hua Xian Biao Shi , Yong Yu . properties He yml Wen Jian De Ke Xuan Ge Shi

  • USER_ FIRST _NAME // Da Xie Xing Shi , Tui Jian Yong Yu Xi Tong Huan Jing Bian Liang

Tong Shi , Ji Yu Lei Xing An Quan De Shu Xing Pei Zhi Huan Ke Yi Jie He @Validated   Zhu Jie Jin Xing Shu Xing De Yue Shu Xiao Yan , Bi Ru Pan Duan Shi Fou Fei Kong 、 Shi Fou Shi Zheng Que De Shou Ji Hao ( You Xiang ) Ge Shi 、 Shi Fou Shi Zheng Que De Ri Qi Deng , Zhe Li Jiu Bu Jin Xing Zhan Kai Liao 。

Da Jia Ke Yi Jie He Ben Shi Li Chang Shi Tuo Zhan 。

Zui Hou , Wo Men Zai Zheng Ti Hui Gu Yi – Xia Ben Jie Shi Li De Zhong Dian Nei Rong , Shou Xian Ji Yu Profile Ji Zhi Wo Men She Ding Liao Duo Ge Huan Jing De Pei Zhi Wen Jian ; Ran Hou Tong Guo spring. profiles.  active Pei Zhi Zhi Ding Ju Ti Shi Yong Na Xie Huan Jing De Can Shu Zhi ; Jie Zhuo Tong Guo @Value He @ConfigurationProperties Zhu Jie Jiang Zhe Xie Pei Zhi Shu Xing Bang Ding Dao Lei Shu Xing Huo  Bean Dui Xiang Shang ; Zui Hou Zai Ju Ti De Chang Jing Zhong Huo Qu Bing Shi Yong ( Ben Shi Li Wei Da Yin )。

Zai Ju Ti Shi Jian Zhong Wo Men Huan Hui Yu Dao You Xian Ji De Wen Ti , Bi Ru Mou Xie Can Shu Zhi Jie Tong Guo Ming Ling Xing Can Shu Jin Xing Zhi Ding , Na Me Ta Jiang Fu Gai Tong Ming De Pei Zhi Wen Jian Zhong De Can Shu 。 Zai Bi Ru , Ru Guo Jiang application   Pei Zhi Wen Jian Fang Zhi Zai Xiang Mu Tong Ji Mu Lu Xia , Ta De You Xian Ji Gao Yu jar Bao Nei De Pei Zhi Deng 。 Zhe Xie Nei Rong Wo Men Zai Yuan Li Pian Du You She Ji , Du Zhe Ke Can Kao Ben Shi Li Jin Xing Zhu Yi Yan Zheng Xue Xi 。


Zhu Yao Jie Shao Liao Spring Boot Zhong Can Shu De Chuan Di Guo Cheng He Pei Zhi Wen Jian De Jia Zai , Te Bie Shi Ji Yu profile   De Jia Zai Ji Zhi 。 Er Guan Yu Jia Zai 、 Mo Ren Pei Zhi 、 Pei Zhi You Xian Ji Deng Cao Zuo , Du Wei Yu  ConfigFileApplicationListener Lei Zhong , Gai Lei Huan Shi Zhi De Du Zhe Peng You Hua Shi Jian Yan Jiu Yi Xia De 。

Zui Hou , You Yu Ben Zhang She Ji Yuan Ma Jiao Duo , Luo Ji Ceng Ci Jiao Shen , Bu Tong De Pei Zhi Mo Shi You Hui Xing Cheng Bu Tong De Zu He , Xing Cheng Jiao Duo De Chang Jing , Yin Ci Jian Yi Zai Xue Xi Guo Cheng Zhong Tong Guo debug   Lai Gen Zong Mei Yi Bu De Cao Zuo , Yi Bian Neng Gou Geng Hao Di Li Jie Zheng Ge Liu Cheng 。

Guan Yu SpringBoot Wai Hua Pei Zhi Yuan Ma De Can Shu Ji Pei Zhi Shi Zen Yang De Wen Ti De Jie Da Jiu Fen Xiang Dao Zhe Li Liao , Xi Wang Yi Shang Nei Rong Ke Yi Dui Da Jia You Yi Ding De Bang Zhu , Ru Guo Ni Huan You Hen Duo Yi Huo Mei You Jie Kai , Ke Yi Guan Zhu Tumi Yun Xing Ye Zi Xun Pin Dao Liao Jie Geng Duo Xiang Guan Zhi Shi 。

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment