(17)环境的抽象

本章将描述一下Spring中针对环境的抽象。

Environment是一个集成到容器之中的特殊抽象,它针对应用的环境建立了两个关键的概念:profileproperties.

profile是命名好的,其中包含了多个Bean的定义的一个逻辑集合,只有当指定的profile被激活的时候,其中的Bean才会激活。无论是通过XML定义的还是通过注解解析的Bean都可以配置到profile之中。而Environment对象的角色就是跟profile相关联,然后决定来激活哪一个profile,还有哪一个profile为默认的profile

properties在几乎所有的应用当中都有着重要的作用,当然也可能导致多个数据源:property文件,JVM系统property,系统环境变量,JNDI,servlet上下文参数,ad-hoc属性对象,Map等。Environment对象和property相关联,然后来给开发者一个方便的服务接口来配置这些数据源,并正确解析。

1.Bean定义的profile

在容器之中,Bean定义profile是一种允许不同环境注册不同bean的机制。环境的概念就意味着不同的东西对应不同的开发者,而且这个特性能够在一下的一些场景很有效:

  • 解决一些内存中的数据源的问题,可以在不同环境访问不同的数据源,开发环境,QA测试环境,生产环境等。
  • 仅仅在开发环境来使用一些监视服务
  • 在不同的环境,使用不同的bean实现
  • @profile注解

例子:模拟两套环境,一个生产环境,一个是产品的环境,可以通过激活特定的环境。来动态使用不同的环境

1)@profile注解在类中的使用
模拟生产环境

/**
 * @Project: spring
 * @description:  模拟生产环境
 * @author: sunkang
 * @create: 2018-09-21 09:17
 * @ModificationHistory who      when       What
 **/
@Configuration
@Profile("production")
public class ProductionDataConfig {

    public String dataSource() throws Exception {
        return "我是生产环境";
    }
}

模拟开发环境

/**
 * @Project: spring
 * @description:   模拟开发环境
 * @author: sunkang
 * @create: 2018-09-21 09:15
 * @ModificationHistory who      when       What
 **/
@Configuration
@Profile("dev")
public class DevDataConfig {
    @Bean
    public String  dataSource() {
        return "我是开发环境";
    }
}

2)@Profile注解可以当做元注解来使用。比如,下面所定义的@Production注解就可以来替代@Profile("production")

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("production")
public @interface Production {
}

3)@Profile注解也可以在方法级别使用,可以声明在包含@Bean注解的方法之上:

/**
 * @Project: spring
 * @description:  @Profile在注解上的使用
 * @author: sunkang
 * @create: 2018-09-21 09:04
 * @ModificationHistory who      when       What
 **/
@Configuration
public class DatasourceConfig {
    @Bean
    @Profile("dev")
    public String  devDataSource() {
        return "我是开发环境";
    }
    @Profile("production")
    public String productionDataSource() throws Exception {
        return "我是生产环境";
    }
}

如果配置了@Configuration的类同时配置了@Profile,那么所有的配置了@Bean注解的方法和@Import注解的相关的类都会被传递为该Profile除非这个Profile激活了,否则Bean定义都不会激活。如果配置为@Component或者@Configuration的类标记了@Profile({"p1", "p2"}),那么这个类当且仅当Profile是p1或者p2的时候才会激活。如果某个Profile的前缀是!这个否操作符,那么@Profile注解的类会只有当前的Profile没有激活的时候才能生效。举例来说,如果配置为@Profile({"p1", "!p2"}),那么注册的行为会在Profile为p1或者是Profile为非p2的时候才会激活。

  • XML中Bean定义的profile

在XML中相对应配置是<beans/>中的profile属性。我们在前面配置的信息可以被重写到XML文件之中如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans  profile="dev"
        xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>

    <bean  id="devDataConfig" class="java.lang.String">
        <constructor-arg index="0"  value="我是开发环境"/>
    </bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans  profile="production"
        xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    <bean  id="devDataConfig" class="java.lang.String">
        <constructor-arg index="0"  value="我是开发环境"/>
    </bean>
</beans>

当然,也可以通过嵌套<beans/>标签来完成定义部分:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>

    <beans profile="dev">
        <bean  id="dev" class="java.lang.String">
            <constructor-arg index="0"  value="我是开发环境"/>
        </bean>
    </beans>

    <beans profile="production">
        <bean   id="production"   class="java.lang.String">
            <constructor-arg index="0"  value="我是生产环境"/>
        </bean>
    </beans>

</beans>
  • 激活profile

现在,我们已经更新了配置信息来使用环境抽象,但是我们还需要告诉Spring来激活具体哪一个Profile。如果我们直接启动应用的话,现在就回抛出NoSuchBeanDefinitionException异常,因为容器会找不到Spring的BeandataSource。

有多种方法来激活一个Profile,最直接的方式就是通过编程的方式来直接调用EnvironmentAPI,ApplicationContext中包含这个接口:

 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.getEnvironment().setActiveProfiles("dev");
        ctx.register(DevDataConfig.class,ProductionDataConfig.class);
        ctx.refresh();
        String dataSource=  ctx.getBean("dataSource",String.class);
        System.out.println(dataSource);

额外的,Profile还可以通过spring.profiles.active中的属性来通过系统环境变量,JVM系统变量,servlet上下文中的参数,甚至是JNDI的一个参数等来写入。在集成测试中,激活Profile可以通过spring-test中的@ActiveProfiles来实现

需要注意的是,Profile的定义并不是一种互斥的关系,我们完全可以在同一时间激活多个Profile的。编程上来说,为setActiveProfile()方法提供多个Profile的名字即可:

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.getEnvironment().setActiveProfiles("dev","production");

也可以通过spring.profiles.active来指定,逗号分隔的多个Profile的名字:

-Dspring.profiles.active="profile1,profile2"
  • 默认profile

@Configuration
@Profile("default")
public class DefalutDataConfig {
    @Bean
    public String  dataSource() {
        return "我是默认环境";
    }
}

如果没有其他的Profile被激活,那么上面代码定义的dataSource就会被创建,这种方式就是为默认情况下提供Bean定义的一种方式。一旦任何一个Profile激活了,那么默认的Profile就不会激活

默认的Profile的名字可以通过Environment中的setDefaultProfiles()方法或者是通过spring.profiles.default属性来更改。

2.属性源抽象

Spring的Environment的抽象提供了一些搜索选项,来层次化配置的源信息。具体的内容,参考如下代码:

ApplicationContext ctx = new GenericApplicationContext();
Environment env = ctx.getEnvironment();
boolean containsFoo = env.containsProperty("foo");
System.out.println("Does my environment contain the 'foo' property? " + containsFoo);

在上面的代码片段之中,我们看到一个high-level的查找Spring的foo属性是否定义的一种方式。为了知道Spring中是否包含这个属性,Environment对象会针对PropertySource的集合进行查找。PropertySource是针对一些key-value的属性对的简单抽象而Spring的StandardEnvironment是由两个PropertySource对象所组成的,一个代表的是JVM的系统属性(可以通过System.getProperties()来获取),而另一种则是系统的环境变量(通过System.getenv()来获取。

这些默认的属性源都是StandardEnvironment的代表,可以用在任何应用之中。StandardServletEnvironment则是包含Servlet配置的环境信息,其中会包含很多Servlet的配置和Servlet上下文参数。StandardPortletEnvironment类似于StandardServletEnvironment,能够配置portlet上下文参数。

具体的说,当使用StandardEnvironment的时候,调用env.containsProperty("foo")将返回一个foo的系统属性,或者是foo的运行时环境变量。

查询配置属性是按层次来查询的。默认情况下,系统属性优优于系统环境变量,所以如果foo属性在两个环境中都有配置的话,那么在调用env.getProperty("foo")期间,系统属性值会优先返回。需要注意的是,属性的值是不会合并的,而是完全覆盖掉
在一个普通的StandardServletEnvironment之中,查找的顺序如下,优先查找* ServletConfig参数(比如DispatcherServlet上下文),然后是* ServletContext参数(web.xml中的上下文参数),再然后是* JNDI环境变量JVM系统变量(”-D”命令行参数)以及JVM环境变量(操作系统环境变量)。

最重要的是,整个的机制是可以配置的。也许开发者自己有些定义的配置源信息想集成到配置检索的系统中去。没问题,只要实现开发者自己的PropertySource并且将其加入到当前Environment的PropertySources之中即可

ConfigurableApplicationContext ctx = new GenericApplicationContext();
MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
sources.addFirst(new MyPropertySource());

在上面的代码之中,MyPropertySource被添加到检索配置的第一优先级之中如果存在一个foo属性,它将由于其他的PropertySource之中的foo属性优先返回。MutablePropertySourcesAPI提供一些方法来允许精确控制配置源。

3.@PropertySource注解

@PropertySource注解提供了一种方便的机制来将PropertySource增加到Spring的Environment之中
配置文件app.properties在classpath的environment目录下,具体配置如下:

defalutPath=environment
name=sun
age=21
addr=hangzhou

java的配置如下:

/**
 * @Project: spring
 * @description:  @PropertySource读取属性配置文件
 * @author: sunkang
 * @create: 2018-09-22 22:33
 * @ModificationHistory who      when       What
 **/
@Configuration
@PropertySource("classpath:/environment/app.properties")
public class AppConfig {
    @Autowired
    Environment environment;
    @Bean
    public String nameValue(){
        return environment.getProperty("name");
    }
    @Bean
    public String ageValue(){
        return environment.getProperty("age");
    }
    @Bean
    public String addrValue(){
        return environment.getProperty("addr");
    }
}

测试如下:

/**
 * @Project: spring
 * @description:  @PropertySource的测试如下
 * @author: sunkang
 * @create: 2018-09-22 22:37
 * @ModificationHistory who      when       What
 **/
public class TestBean {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext();
        context.register(AppConfig.class);
        context.refresh();
        String value = (String) context.getBean("nameValue");
        System.out.println(value);
    }
}

测试结果:

sun

任何的@PropertySource之中形如${...}的占位符,都可以被解析成Environment中的属性资源,比如:

/**
 * @Project: spring
 * @description:   用了占位符,但是占位符的属性,要提前注册
 * @author: sunkang
 * @create: 2018-09-22 22:33
 * @ModificationHistory who      when       What
 **/
@Configuration
@PropertySource("classpath:/${placeHolder.environment:environment}/app.properties")
public class AppConfig {

    @Autowired
    Environment environment;
    @Bean
    public String nameValue(){
        return environment.getProperty("name");
    }
    @Bean
    public String ageValue(){
        return environment.getProperty("age");
    }
    @Bean
    public String addrValue(){
        return environment.getProperty("addr");
    }
}

假设上面的placeHolder.environment是我们已经注册到Environment之中的资源,举例来说,JVM系统属性或者是环境变量的话,占位符会解析成对象的值。如果没有的话,default/path会来作为默认值。也就是environment,如果没有指定默认值,而且占位符也解析不出来的话,就会抛出IllegalArgumentException。

测试类需要记性调整:

public class TestBean {
   public static void main(String[] args) {
       //比如先注册一些属性
       Properties properties= System.getProperties();
       properties.setProperty("placeHolder.environment","environment");
       AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext();
       context.register(AppConfig.class);
       context.refresh();
       String value = (String) context.getBean("nameValue");
       System.out.println(value);
   }
}

4.占位符解析

从历史上来说,占位符的值是只能针对JVM系统属性或者环境变量来解析的。但是现在不是了,因为环境抽象已经继承到了容器之中,现在很容易将占位符解析。这意味着开发者可以任意的配置占位符:

  • 调整系统变量还有环境变量的优先级
  • 增加自己的属性源信息

具体的说,下面的XML配置不会在意customer属性在哪里定义,只有这个值在Environment之中有效即可:

<beans>
    <import resource="com/bank/service/${customer}-config.xml"/>
</beans>

5. 注册LoadTimeWeaver

Spring使用LoadTimeWeaver当将类加载进JVM时进行动态转换。
为了使得加载时间织入,在你的@Configuration类上加入@EnableLoadTimeWeaving。

@Configuration
@EnableLoadTimeWeaving
public class AppConfig {
}

或者是使用元素context:load-time-weaver

<beans>
    <context:load-time-weaver/>
</beans>

只要配置了ApplicationContext。ApplicationContext中的任何Bean可能实现LoadTimeWearveAware,从而接受load-time weaver 实例的引用。与Spring 的JPA一起使用是很有用的。这里load-time weaving对于 Spring’s JPA support 类转换是很必要的。详情参考Load-time weaving with AspectJ in the Spring Framework

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容