spring boot環(huán)境抽象的實(shí)現(xiàn)方法
在實(shí)際開發(fā)中,開發(fā)人員在編寫springboot的時(shí)候通常要在本地環(huán)境測(cè)試然后再部署到Production環(huán)境,這兩種環(huán)境一般來講是不同的,最主要的區(qū)別就是數(shù)據(jù)源的不同。
在應(yīng)用環(huán)境中,集成在容器的抽象環(huán)境模型有兩個(gè)方面:profiles和properties。只有給出的profile被激活,一組邏輯命名的bean定義才會(huì)在容器中注冊(cè)。
環(huán)境變量對(duì)象角色和profiles的關(guān)系來決定哪個(gè)profiles(如果有)處于當(dāng)前激活狀態(tài),哪個(gè)profiles默認(rèn)被激活。
@Profile
基于Java類的環(huán)境配置
@Profile注解可以用來標(biāo)注@Configuration注解的類。表示該特定環(huán)境下激活該類下的所有bean。當(dāng)然也可以專門用來標(biāo)注@Bean,因?yàn)樵S多時(shí)候本地環(huán)境和Production環(huán)境的區(qū)別只是數(shù)據(jù)源不同罷了。
@Configuration
public class ProfileConf {
@Bean
@Profile("dev")
public UserInfo devUserInfo() {
UserInfo userInfo = new UserInfo();
userInfo.setId(1);
userInfo.setName("dev");
return userInfo;
}
@Bean
@Profile("production")
public UserInfo productionUserInfo() {
UserInfo userInfo = new UserInfo();
userInfo.setId(1);
userInfo.setName("production");
return userInfo;
}
}
激活profile
現(xiàn)在我們已經(jīng)更新了我們的配置,我們?nèi)匀恍枰f明哪個(gè)profile是激活的。如果直接注冊(cè)@Configuration標(biāo)注的類,這將會(huì)看到一個(gè)NoSuchBeanDefinitionException被拋出,因?yàn)槿萜髡也坏揭粋€(gè)對(duì)應(yīng)的環(huán)境下的bean。
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("dev");
context.register(UserConf.class);
context.refresh();
System.out.println(context.getBean(UserInfo.class));
}
默認(rèn)的profile
默認(rèn)配置文件表示默認(rèn)啟用的配置文件。
@Bean
@Profile("default")
public UserInfo defaultUserInfo() {
UserInfo userInfo = new UserInfo();
userInfo.setId(1);
userInfo.setName("default");
return userInfo;
}
如果沒有profile是激活狀態(tài),上面的bean將會(huì)被創(chuàng)建;這種方式可以被看做是對(duì)一個(gè)或者多個(gè)bean提供了一種默認(rèn)的定義方式。如果啟用任何的profile,那么默認(rèn)的profile都不會(huì)被應(yīng)用。
屬性源抽象
Spring 環(huán)境抽象提供了可配置的屬性源層次結(jié)構(gòu)的搜索操作。
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// context.getEnvironment().setActiveProfiles("dev");
context.getEnvironment().setActiveProfiles("dev");
context.register(ProfileConf.class);
context.refresh();
ConfigurableEnvironment environment = context.getEnvironment();
Map<String, Object> maps = environment.getSystemProperties();
maps.keySet().forEach(k -> System.out.println(k + "->" + maps.get(k)));
System.out.println("===========================");
Map<String, Object> environment1 = environment.getSystemEnvironment();
environment1.keySet().forEach(k -> System.out.println(k + "->" + environment1.get(k)));
System.out.println(environment.containsProperty("java.vm.version"));
}
在上面的例子中可以獲取Environment的兩個(gè)系統(tǒng)變量以及環(huán)境變量。
一個(gè)PropertySource是對(duì)任何key-value資源的簡單抽象,并且Spring 的標(biāo)準(zhǔn)環(huán)境是由兩個(gè)PropertySource配置的,一個(gè)表示一系列的JVM 系統(tǒng)屬性(System.getProperties()),一個(gè)表示一系列的系統(tǒng)環(huán)境變量(System.getenv())。
具體的說,當(dāng)使用StandardEnvironment時(shí),如果在運(yùn)行時(shí)系統(tǒng)屬性或者環(huán)境變量中包括foo,那么調(diào)用env.containsProperty(“java.vm.version”)方法將會(huì)返回true。
更重要的是,整個(gè)機(jī)制都是可配置的。也許你有個(gè)自定義的屬性來源,你想把它集成到這個(gè)搜索里面。這也沒問題,只需簡單的實(shí)現(xiàn)和實(shí)例化自己的PropertySource,并把它添加到當(dāng)前環(huán)境的PropertySources集合中:
ConfigurableApplicationContext ctx = new GenericApplicationContext(); MutablePropertySources sources = ctx.getEnvironment().getPropertySources(); sources.addFirst(new MyPropertySource());
@PropertySource
上一篇文章講到,基于Java的配置很多時(shí)候會(huì)和xml混合使用。其中@Import還可以導(dǎo)入其他Java配置類,這里要說的@PropertySource注解表示導(dǎo)入.properties文件。
@Configuration
@PropertySource("classpath:user.properties")
public class UserConf {
@Autowired
Environment environment;
@Bean
//每次調(diào)用就創(chuàng)建一個(gè)新的bean
@Scope("prototype")
public UserInfo userInfo() {
UserInfo userInfo = new UserInfo();
userInfo.setId(Integer.valueOf(environment.getProperty("user.id")));
System.out.println(environment.getProperty("user.name"));
userInfo.setName(environment.getProperty("user.name"));
return userInfo;
}
}
user.id=11 user.name=asdasd
任何出現(xiàn)在@PropertySource中的資源位置占位符都會(huì)被注冊(cè)在環(huán)境變量中的資源解析。
假設(shè)”user.name”已經(jīng)在其中的一個(gè)資源中被注冊(cè),例如:系統(tǒng)屬性或環(huán)境變量,占位符將會(huì)被正確的值解析。
如果沒有,”default/path”將會(huì)使用默認(rèn)值。如果沒有默認(rèn)值,而且無法解釋屬性,則拋出IllegalArgumentException異常。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SVN導(dǎo)入maven項(xiàng)目報(bào)錯(cuò)解決方案
這篇文章主要介紹了SVN導(dǎo)入maven項(xiàng)目報(bào)錯(cuò)解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過程解析
這篇文章主要介紹了Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
nacos服務(wù)注冊(cè)服務(wù)發(fā)現(xiàn)依賴配置詳解
這篇文章主要為大家介紹了nacos服務(wù)注冊(cè)服務(wù)發(fā)現(xiàn)依賴配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
ReentrantLock 非公平鎖實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了ReentrantLock 非公平鎖實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
基于Springboot的高校社團(tuán)管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)
本文將基于Springboot+Mybatis開發(fā)實(shí)現(xiàn)一個(gè)高校社團(tuán)管理系統(tǒng),系統(tǒng)包含三個(gè)角色:管理員、團(tuán)長、會(huì)員。文中采用的技術(shù)有Springboot、Mybatis、Jquery、AjAX、JSP等,感興趣的可以了解一下2022-07-07
使用Spring Cloud Feign遠(yuǎn)程調(diào)用的方法示例
這篇文章主要介紹了使用Spring Cloud Feign遠(yuǎn)程調(diào)用的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

