本章将描述一下Spring中针对环境的抽象。
Environment
是一个集成到容器之中的特殊抽象,它针对应用的环境建立了两个关键的概念:profile
和properties
.
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