javascript
精通Spring Boot——第十一篇:使用自定义配置
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
今天這篇文章給大家介紹自定義配置的兩種方式 第一式: 使用@ConfigurationProperties,且看代碼
package com.developlee.customconfig.config;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.context.annotation.Configuration;/*** @author Lensen* @desc* @since 2018/8/22 12:59*/ @Configuration @ConfigurationProperties(prefix = "one-app") public class OneAppConfig {@NestedConfigurationPropertypublic Account account = new Account();public String appName;public Account getAccount() {return account;}public void setAccount(Account account) {this.account = account;}public String getAppName() {return appName;}public void setAppName(String appName) {this.appName = appName;}public class Account {private String username;private String password;private String age;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}} }很明顯,這就是我們要在properties文件中要配置的配置項(xiàng)。 再看第二種方式
/*** @author Lensen* @desc* @since 2018/8/22 13:19*/ @Configuration public class TwoAppConfig {@Value("${two-app.welcome.message}")public String twoAppWelcomeMessage;@Value("${two-app.welcome.person}")public String twoAppWelcomePerson;public String getTwoAppWelcomeMessage() {return twoAppWelcomeMessage;}public void setTwoAppWelcomeMessage(String twoAppWelcomeMessage) {this.twoAppWelcomeMessage = twoAppWelcomeMessage;}public String getTwoAppWelcomePerson() {return twoAppWelcomePerson;}public void setTwoAppWelcomePerson(String twoAppWelcomePerson) {this.twoAppWelcomePerson = twoAppWelcomePerson;} }這個就簡單粗暴啦。沒有第一種方式結(jié)構(gòu)那么清晰,具體怎么使用,完全取決于項(xiàng)目配置項(xiàng)的關(guān)聯(lián)關(guān)系和復(fù)雜度,需要大家根據(jù)實(shí)際情況權(quán)衡。 接下來我寫了個簡單的測試類,來獲取我們的配置信息 先看配置文件:
one-app:app-name: OneAPPaccount:username: Lensenpassword: Orclage: 22two-app:welcome:message: welcome to lensen's bolgperson: LENSEN一個簡單的Controller類
package com.developlee.customconfig.controller;import com.developlee.customconfig.config.OneAppConfig; import com.developlee.customconfig.config.TwoAppConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;/*** @author Lensen* @desc* @since 2018/8/22 16:40*/ @RestController public class AppController {@Autowiredprivate OneAppConfig oneAppConfig;@Autowiredprivate TwoAppConfig twoAppConfig;@GetMapping("/hello")public ResponseEntity getConfig() {String str1 = "oneAppConfig: " + oneAppConfig.getAppName() + oneAppConfig.getAccount().getUsername()+ oneAppConfig.getAccount().getPassword() + oneAppConfig.getAccount().getAge();String str2 = "twoAppConfig: " + twoAppConfig.getTwoAppWelcomePerson() + twoAppConfig.getTwoAppWelcomeMessage();return new ResponseEntity(str1 +"~~~~~~~"+ str2, HttpStatus.OK);} }在地址欄輸入http:localhost:8080/hello, 回車 也可以自己指定文件,只需在類上加上注解@PropertySource注解就好了~~
@Configuration @PropertySource("classpath:my.properties") public class ThreeConfig {@Value("${my.name}")private String myName;public String getMyName() {return myName;}public void setMyName(String myName) {this.myName = myName;} }my.properties文件內(nèi)容:
my.name=developlee測試結(jié)果:
如果配置文件是yml格式的,則要使用YamlPropertiesFactoryBean來加載并設(shè)置到PropertySourcesPlaceholderConfigurer中
// 加載YML格式自定義配置文件@Beanpublic static PropertySourcesPlaceholderConfigurer properties() {PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();yaml.setResources(new FileSystemResource("config.yml"));//File引入 // yaml.setResources(new ClassPathResource("youryml.yml"));//class引入configurer.setProperties(yaml.getObject());return configurer;end... 浮躁的社會,浮躁的人生,唯有代碼,寧靜致遠(yuǎn)。(又開始裝13了,見諒.....)
最后,以上示例代碼可在我的github.com中找到。 我的個人公眾號:developlee的瀟灑人生。 關(guān)注了也不一定更新,更新就不得了了。
轉(zhuǎn)載于:https://my.oschina.net/liululee/blog/2250330
總結(jié)
以上是生活随笔為你收集整理的精通Spring Boot——第十一篇:使用自定义配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: React开发(173):ant des
- 下一篇: 计算机学数字电子基础知识,什么是数字电路