(17)環(huán)境的抽象

本章將描述一下Spring中針對(duì)環(huán)境的抽象。

Environment是一個(gè)集成到容器之中的特殊抽象,它針對(duì)應(yīng)用的環(huán)境建立了兩個(gè)關(guān)鍵的概念:profileproperties.

profile是命名好的,其中包含了多個(gè)Bean的定義的一個(gè)邏輯集合,只有當(dāng)指定的profile被激活的時(shí)候,其中的Bean才會(huì)激活。無論是通過XML定義的還是通過注解解析的Bean都可以配置到profile之中。而Environment對(duì)象的角色就是跟profile相關(guān)聯(lián),然后決定來激活哪一個(gè)profile,還有哪一個(gè)profile為默認(rèn)的profile。

properties在幾乎所有的應(yīng)用當(dāng)中都有著重要的作用,當(dāng)然也可能導(dǎo)致多個(gè)數(shù)據(jù)源:property文件,JVM系統(tǒng)property,系統(tǒng)環(huán)境變量,JNDI,servlet上下文參數(shù),ad-hoc屬性對(duì)象,Map等。Environment對(duì)象和property相關(guān)聯(lián),然后來給開發(fā)者一個(gè)方便的服務(wù)接口來配置這些數(shù)據(jù)源,并正確解析。

1.Bean定義的profile

在容器之中,Bean定義profile是一種允許不同環(huán)境注冊(cè)不同bean的機(jī)制。環(huán)境的概念就意味著不同的東西對(duì)應(yīng)不同的開發(fā)者,而且這個(gè)特性能夠在一下的一些場(chǎng)景很有效:

  • 解決一些內(nèi)存中的數(shù)據(jù)源的問題,可以在不同環(huán)境訪問不同的數(shù)據(jù)源,開發(fā)環(huán)境,QA測(cè)試環(huán)境,生產(chǎn)環(huán)境等。
  • 僅僅在開發(fā)環(huán)境來使用一些監(jiān)視服務(wù)
  • 在不同的環(huán)境,使用不同的bean實(shí)現(xiàn)
  • @profile注解

例子:模擬兩套環(huán)境,一個(gè)生產(chǎn)環(huán)境,一個(gè)是產(chǎn)品的環(huán)境,可以通過激活特定的環(huán)境。來動(dòng)態(tài)使用不同的環(huán)境

1)@profile注解在類中的使用
模擬生產(chǎn)環(huán)境

/**
 * @Project: spring
 * @description:  模擬生產(chǎn)環(huán)境
 * @author: sunkang
 * @create: 2018-09-21 09:17
 * @ModificationHistory who      when       What
 **/
@Configuration
@Profile("production")
public class ProductionDataConfig {

    public String dataSource() throws Exception {
        return "我是生產(chǎn)環(huán)境";
    }
}

模擬開發(fā)環(huán)境

/**
 * @Project: spring
 * @description:   模擬開發(fā)環(huán)境
 * @author: sunkang
 * @create: 2018-09-21 09:15
 * @ModificationHistory who      when       What
 **/
@Configuration
@Profile("dev")
public class DevDataConfig {
    @Bean
    public String  dataSource() {
        return "我是開發(fā)環(huán)境";
    }
}

2)@Profile注解可以當(dāng)做元注解來使用。比如,下面所定義的@Production注解就可以來替代@Profile("production")

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

3)@Profile注解也可以在方法級(jí)別使用,可以聲明在包含@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 "我是開發(fā)環(huán)境";
    }
    @Profile("production")
    public String productionDataSource() throws Exception {
        return "我是生產(chǎn)環(huán)境";
    }
}

如果配置了@Configuration的類同時(shí)配置了@Profile,那么所有的配置了@Bean注解的方法和@Import注解的相關(guān)的類都會(huì)被傳遞為該P(yáng)rofile除非這個(gè)Profile激活了,否則Bean定義都不會(huì)激活。如果配置為@Component或者@Configuration的類標(biāo)記了@Profile({"p1", "p2"}),那么這個(gè)類當(dāng)且僅當(dāng)Profile是p1或者p2的時(shí)候才會(huì)激活。如果某個(gè)Profile的前綴是!這個(gè)否操作符,那么@Profile注解的類會(huì)只有當(dāng)前的Profile沒有激活的時(shí)候才能生效。舉例來說,如果配置為@Profile({"p1", "!p2"}),那么注冊(cè)的行為會(huì)在Profile為p1或者是Profile為非p2的時(shí)候才會(huì)激活。

  • XML中Bean定義的profile

在XML中相對(duì)應(yīng)配置是<beans/>中的profile屬性。我們?cè)谇懊媾渲玫男畔⒖梢员恢貙懙絏ML文件之中如下:

<?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="我是開發(fā)環(huán)境"/>
    </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="我是開發(fā)環(huán)境"/>
    </bean>
</beans>

當(dāng)然,也可以通過嵌套<beans/>標(biāo)簽來完成定義部分:

<?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="我是開發(fā)環(huán)境"/>
        </bean>
    </beans>

    <beans profile="production">
        <bean   id="production"   class="java.lang.String">
            <constructor-arg index="0"  value="我是生產(chǎn)環(huán)境"/>
        </bean>
    </beans>

</beans>
  • 激活profile

現(xiàn)在,我們已經(jīng)更新了配置信息來使用環(huán)境抽象,但是我們還需要告訴Spring來激活具體哪一個(gè)Profile。如果我們直接啟動(dòng)應(yīng)用的話,現(xiàn)在就回拋出NoSuchBeanDefinitionException異常,因?yàn)槿萜鲿?huì)找不到Spring的BeandataSource。

有多種方法來激活一個(gè)Profile,最直接的方式就是通過編程的方式來直接調(diào)用EnvironmentAPI,ApplicationContext中包含這個(gè)接口:

 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中的屬性來通過系統(tǒng)環(huán)境變量,JVM系統(tǒng)變量,servlet上下文中的參數(shù),甚至是JNDI的一個(gè)參數(shù)等來寫入。在集成測(cè)試中,激活Profile可以通過spring-test中的@ActiveProfiles來實(shí)現(xiàn)

需要注意的是,Profile的定義并不是一種互斥的關(guān)系,我們完全可以在同一時(shí)間激活多個(gè)Profile的。編程上來說,為setActiveProfile()方法提供多個(gè)Profile的名字即可:

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

也可以通過spring.profiles.active來指定,逗號(hào)分隔的多個(gè)Profile的名字:

-Dspring.profiles.active="profile1,profile2"
  • 默認(rèn)profile

@Configuration
@Profile("default")
public class DefalutDataConfig {
    @Bean
    public String  dataSource() {
        return "我是默認(rèn)環(huán)境";
    }
}

如果沒有其他的Profile被激活,那么上面代碼定義的dataSource就會(huì)被創(chuàng)建,這種方式就是為默認(rèn)情況下提供Bean定義的一種方式。一旦任何一個(gè)Profile激活了,那么默認(rèn)的Profile就不會(huì)激活

默認(rèn)的Profile的名字可以通過Environment中的setDefaultProfiles()方法或者是通過spring.profiles.default屬性來更改。

2.屬性源抽象

Spring的Environment的抽象提供了一些搜索選項(xiàng),來層次化配置的源信息。具體的內(nèi)容,參考如下代碼:

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

在上面的代碼片段之中,我們看到一個(gè)high-level的查找Spring的foo屬性是否定義的一種方式。為了知道Spring中是否包含這個(gè)屬性,Environment對(duì)象會(huì)針對(duì)PropertySource的集合進(jìn)行查找。PropertySource是針對(duì)一些key-value的屬性對(duì)的簡單抽象,而Spring的StandardEnvironment是由兩個(gè)PropertySource對(duì)象所組成的,一個(gè)代表的是JVM的系統(tǒng)屬性(可以通過System.getProperties()來獲取),而另一種則是系統(tǒng)的環(huán)境變量(通過System.getenv()來獲取。

這些默認(rèn)的屬性源都是StandardEnvironment的代表,可以用在任何應(yīng)用之中。StandardServletEnvironment則是包含Servlet配置的環(huán)境信息,其中會(huì)包含很多Servlet的配置和Servlet上下文參數(shù)。StandardPortletEnvironment類似于StandardServletEnvironment,能夠配置portlet上下文參數(shù)。

具體的說,當(dāng)使用StandardEnvironment的時(shí)候,調(diào)用env.containsProperty("foo")將返回一個(gè)foo的系統(tǒng)屬性,或者是foo的運(yùn)行時(shí)環(huán)境變量。

查詢配置屬性是按層次來查詢的。默認(rèn)情況下,系統(tǒng)屬性優(yōu)優(yōu)于系統(tǒng)環(huán)境變量,所以如果foo屬性在兩個(gè)環(huán)境中都有配置的話,那么在調(diào)用env.getProperty("foo")期間,系統(tǒng)屬性值會(huì)優(yōu)先返回。需要注意的是,屬性的值是不會(huì)合并的,而是完全覆蓋掉
在一個(gè)普通的StandardServletEnvironment之中,查找的順序如下,優(yōu)先查找* ServletConfig參數(shù)(比如DispatcherServlet上下文),然后是* ServletContext參數(shù)(web.xml中的上下文參數(shù)),再然后是* JNDI環(huán)境變量,JVM系統(tǒng)變量(”-D”命令行參數(shù))以及JVM環(huán)境變量(操作系統(tǒng)環(huán)境變量)。

最重要的是,整個(gè)的機(jī)制是可以配置的。也許開發(fā)者自己有些定義的配置源信息想集成到配置檢索的系統(tǒng)中去。沒問題,只要實(shí)現(xiàn)開發(fā)者自己的PropertySource并且將其加入到當(dāng)前Environment的PropertySources之中即可

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

在上面的代碼之中,MyPropertySource被添加到檢索配置的第一優(yōu)先級(jí)之中。如果存在一個(gè)foo屬性,它將由于其他的PropertySource之中的foo屬性優(yōu)先返回。MutablePropertySourcesAPI提供一些方法來允許精確控制配置源。

3.@PropertySource注解

@PropertySource注解提供了一種方便的機(jī)制來將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");
    }
}

測(cè)試如下:

/**
 * @Project: spring
 * @description:  @PropertySource的測(cè)試如下
 * @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);
    }
}

測(cè)試結(jié)果:

sun

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

/**
 * @Project: spring
 * @description:   用了占位符,但是占位符的屬性,要提前注冊(cè)
 * @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");
    }
}

假設(shè)上面的placeHolder.environment是我們已經(jīng)注冊(cè)到Environment之中的資源,舉例來說,JVM系統(tǒng)屬性或者是環(huán)境變量的話,占位符會(huì)解析成對(duì)象的值。如果沒有的話,default/path會(huì)來作為默認(rèn)值。也就是environment,如果沒有指定默認(rèn)值,而且占位符也解析不出來的話,就會(huì)拋出IllegalArgumentException。

測(cè)試類需要記性調(diào)整:

public class TestBean {
   public static void main(String[] args) {
       //比如先注冊(cè)一些屬性
       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.占位符解析

從歷史上來說,占位符的值是只能針對(duì)JVM系統(tǒng)屬性或者環(huán)境變量來解析的。但是現(xiàn)在不是了,因?yàn)?code>環(huán)境抽象已經(jīng)繼承到了容器之中,現(xiàn)在很容易將占位符解析。這意味著開發(fā)者可以任意的配置占位符:

  • 調(diào)整系統(tǒng)變量還有環(huán)境變量的優(yōu)先級(jí)
  • 增加自己的屬性源信息

具體的說,下面的XML配置不會(huì)在意customer屬性在哪里定義,只有這個(gè)值在Environment之中有效即可:

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

5. 注冊(cè)LoadTimeWeaver

Spring使用LoadTimeWeaver當(dāng)將類加載進(jìn)JVM時(shí)進(jìn)行動(dòng)態(tài)轉(zhuǎn)換。
為了使得加載時(shí)間織入,在你的@Configuration類上加入@EnableLoadTimeWeaving。

@Configuration
@EnableLoadTimeWeaving
public class AppConfig {
}

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

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

只要配置了ApplicationContext。ApplicationContext中的任何Bean可能實(shí)現(xiàn)LoadTimeWearveAware,從而接受load-time weaver 實(shí)例的引用。與Spring 的JPA一起使用是很有用的。這里load-time weaving對(duì)于 Spring’s JPA support 類轉(zhuǎn)換是很必要的。詳情參考Load-time weaving with AspectJ in the Spring Framework

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容