在實際開發中,開發人員在編寫springboot的時候通常要在本地環境測試然後再部署到Production環境,這兩種環境一般來講是不同的,最主要的區別就是數據源的不同。本文主要介紹了這兩種,感興趣的可以瞭解一下
在實際開發中,開發人員在編寫springboot的時候通常要在本地環境測試然後再部署到Production環境,這兩種環境一般來講是不同的,最主要的區別就是數據源的不同。
在應用環境中,集成在容器的抽象環境模型有兩個方面:profiles和properties。只有給出的profile被激活,一組邏輯命名的bean定義纔會在容器中註冊。
環境變量對象角色和profiles的關係來決定哪個profiles(如果有)處於當前激活狀態,哪個profiles默認被激活。
@Profile
基於Java類的環境配置
@Profile註解可以用來標註@Configuration註解的類。表示該特定環境下激活該類下的所有bean。當然也可以專門用來標註@Bean,因爲許多時候本地環境和Production環境的區別只是數據源不同罷了。
@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
現在我們已經更新了我們的配置,我們仍然需要說明哪個profile是激活的。如果直接註冊@Configuration標註的類,這將會看到一個NoSuchBeanDefinitionException被拋出,因爲容器找不到一個對應的環境下的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)); }
默認的profile
默認配置文件表示默認啓用的配置文件。
@Bean @Profile("default") public UserInfo defaultUserInfo() { UserInfo userInfo = new UserInfo(); userInfo.setId(1); userInfo.setName("default"); return userInfo; }
如果沒有profile是激活狀態,上面的bean將會被創建;這種方式可以被看做是對一個或者多個bean提供了一種默認的定義方式。如果啓用任何的profile,那麼默認的profile都不會被應用。
屬性源抽象
Spring 環境抽象提供了可配置的屬性源層次結構的搜索操作。
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的兩個系統變量以及環境變量。
一個PropertySource是對任何key-value資源的簡單抽象,並且Spring 的標準環境是由兩個PropertySource配置的,一個表示一系列的JVM 系統屬性(System.getProperties()),一個表示一系列的系統環境變量(System.getenv())。
具體的說,當使用StandardEnvironment時,如果在運行時系統屬性或者環境變量中包括foo,那麼調用env.containsProperty(“java.vm.version”)方法將會返回true。
更重要的是,整個機制都是可配置的。也許你有個自定義的屬性來源,你想把它集成到這個搜索裏面。這也沒問題,只需簡單的實現和實例化自己的PropertySource,並把它添加到當前環境的PropertySources集合中:
ConfigurableApplicationContext ctx = new GenericApplicationContext(); MutablePropertySources sources = ctx.getEnvironment().getPropertySources(); sources.addFirst(new MyPropertySource());
@PropertySource
上一篇文章講到,基於Java的配置很多時候會和xml混合使用。其中@Import還可以導入其他Java配置類,這裏要說的@PropertySource註解表示導入.properties文件。
@Configuration @PropertySource("classpath:user.properties") public class UserConf { @Autowired Environment environment; @Bean //每次調用就創建一個新的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
任何出現在@PropertySource中的資源位置佔位符都會被註冊在環境變量中的資源解析。
假設”user.name”已經在其中的一個資源中被註冊,例如:系統屬性或環境變量,佔位符將會被正確的值解析。
如果沒有,”default/path”將會使用默認值。如果沒有默認值,而且無法解釋屬性,則拋出IllegalArgumentException異常。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。