2019獨角獸企業重金招聘Python工程師標準>>>
今天這篇文章給大家介紹自定義配置的兩種方式 第一式: 使用@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文件中要配置的配置項。 再看第二種方式
/*** @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;}
}
這個就簡單粗暴啦。沒有第一種方式結構那么清晰,具體怎么使用,完全取決于項目配置項的關聯關系和復雜度,需要大家根據實際情況權衡。 接下來我寫了個簡單的測試類,來獲取我們的配置信息 先看配置文件:
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文件內容:
my.name=developlee
測試結果:
如果配置文件是yml格式的,則要使用YamlPropertiesFactoryBean來加載并設置到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... 浮躁的社會,浮躁的人生,唯有代碼,寧靜致遠。(又開始裝13了,見諒.....)
最后,以上示例代碼可在我的github.com中找到。 我的個人公眾號:developlee的瀟灑人生。 關注了也不一定更新,更新就不得了了。