Spring中的Environment外部化配置管理详解

Environment的中文意思是环境,它表示整个spring应用运行时的环境信息,它包含两个关键因素

profiles

properties

profiles

profiles这个概念相信大家都已经理解了,最常见的就是不同环境下,决定当前spring容器中的不同配置上下文的解决方案。比如针对开发环境、测试环境、生产环境,构建不同的application.properties配置项,这个时候我们可以通过profiles这个属性来决定当前spring应用上下文中生效的配置项。

实际上,通过profiles可以针对bean的配置进行逻辑分组。 简单来说,我们可以通过profiles来针对不同的bean进行逻辑分组,这个分组和bean本身的定义没有任何关系,无论是xml还是注解方式,都可以配置bean属于哪一个profile分组。

当存在多个profile分组时,我们可以指定哪一个profile生效,当然如果不指定,spring会根据默认的profile去执行。我们来通过一个代码演示一下。

ProfileService

创建一个普通的类,代码如下

public class ProfileService { private String profile; public ProfileService(String profile) { this.profile = profile; } @Override public String toString() { return "ProfileService{" + "profile=\'" + profile + \'\\'\' + \'}\'; } } 声明一个配置类

在配置类中,构建两个bean,配置不同的profile。

@Configuration public class ProfileConfiguration { @Bean @Profile("dev") public ProfileService profileServiceDev(){ return new ProfileService("dev"); } @Bean @Profile("prod") public ProfileService profileServiceProd(){ return new ProfileService("prod"); } } 定义测试方法 public class ProfileMain { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(); // applicationContext.getEnvironment().setActiveProfiles("prod"); applicationContext.register(ProfileConfiguration.class); applicationContext.refresh(); System.out.println(applicationContext.getBean(ProfileService.class)); } }

可以通过很多种方式来激活配置,默认情况下不添加applicationContext.getEnvironment().setActiveProfiles("prod");时,会发现bean没有被装载。添加了之后,会根据当前激活的profiles来决定装载哪个bean。

除此之外,我们还可以在启动参数中增加-Dspring.profiles.active=prod来决定当前激活哪个profile。该属性可以配置在系统环境变量、JVM系统属性、等。

注意配置文件不是单选;可能会同时激活多个配置文件,编程式的使用方法setActiveProfiles(),该方法接收String数组参数,也就是多个配置文件名

applicationContext.getEnvironment().setActiveProfiles("prod","dev");

如果没有任何profile配置被激活,默认的profile将会激活。
默认profile配置文件可以更改,通过环境变量的setDefaultProfiles方法,或者是声明的spring.profiles.default属性值

profiles总结

简单总结一下profiles,通过profiles可以最一组bean进行逻辑分组,这些逻辑分组的bean会根据Environment上下文中配置的激活的profile来进行加载,也就是Environment对于profiles配置来说,它能决定当前激活的是哪个profile配置。

一个profile就是一组Bean定义的逻辑分组。

这个分组,也就 这个profile,被赋予一个命名,就是这个profile名字。

只有当一个profile处于active状态时,它对应的逻辑上组织在一起的这些Bean定义才会被注册到容器中。

Bean添加到profile可以通过XML定义方式或者annotation注解方式。

Environment对于profile所扮演的角色是用来指定哪些profile是当前活跃的缺省。

Properties

properties的作用就是用来存放属性的,它可以帮我们管理各种配置信息。这个配置的来源可以是properties文件、JVM properties、系统环境变量、或者专门的Properties对象等。

我们来看一下Environment这个接口,它继承了PropertyResolver,这个接口和属性的操作有关,也就是我们可以通过Environment来设置和获得相关属性。

public interface Environment extends PropertyResolver { String[] getActiveProfiles(); String[] getDefaultProfiles(); /** @deprecated */ @Deprecated boolean acceptsProfiles(String... var1); boolean acceptsProfiles(Profiles var1); }

至此,我们可以可以简单的总结Environment的作用,Environment提供了不同的profile配置,而PropertyResolver提供了配置的操作,由此我们可以知道,Spring 容器可以根据不同的profile来获取不同的配置信息,从而实现Spring容器中运行时环境的处理。

environment的应用

在spring boot应用中,修改application.properties配置

env=default

创建一个Controller进行测试

@RestController public class EnvironementController { @Autowired Environment environment; @GetMapping("/env") public String env(){ return environment.getProperty("env"); } }

指定profile属性

在前面的内容中我们介绍了profile和property这两个概念,现在我们来结合使用加深对这两者的理解。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzgxgj.html