0%

Spring 使用 Properties

Spring 使用 Properties

业务场景

应用在开发、测试、生产等阶段的资源往往是不同的,如:某个 API 接口的 URL,可能存在以下三个 URL:

  1. 开发环境:dev.api.xxx
  2. 测试环境:test.api.xxx
  3. 生产环境:proc.api.xxx

如果每次在部署应用时,手动修改配置,在企业工作中显然不可行。幸运的是,我们有很多工具可以实现自动地的为不同环境加载不同的配置,一种常见的方法是:将配置按环境分别放在不同的 properties 文件(dev.properties, test.properties, proc.properties 等)每次在不同环境部署时,使用 Maven 打包时,加上环境变量参数将对应的配置文件拷贝至classpath,最终通过 Spring 加载并使用classpath 中的配置。

本文主要讲解 Spring 如何使用 Properties,用于备忘。

注册 Properties

Spring 要能使用到 Properties,首先得将 Properties 进行注册,注册方式有两种:

  1. XML 方式
  2. Java Configuration 方式

通过 XML 注册

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<context:property-placeholder location="classpath:foo.properties" />

</beans>

上述 XML 配置指定使用 foo.properties,如果有多个 properties,可以多配置几个,也可以简单的将 foo.properties 改成 *.properties,按需选取即可。

老码农应该比较习惯使用 XML 的方式。

通过 Java Configuration注册

1
2
3
4
5
@Configuration
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {
//...
}

上述效果与 XML 方式一致。

使用 Properties

通过 XML 或者 Java Configuration 完成Properties 注册后,即可在 Spring 中开始使用它们。使用方式有两种:

  1. 通过 @Value 注解使用
  2. 通过 XML 使用

通过 @Value 注解使用

1
2
@Value( "${jdbc.url}" )
private String jdbcUrl;

通过 XML 使用

1
2
3
<bean id="dataSource">
<property name="url" value="${jdbc.url}" />
</bean>

注:Properties 无论使用哪种方式注册,均可使用以上两个方式使用 Properties。

Properties 加载顺序

当有多个 Properties 文件,特别是不同的 Properties 文件内部存在相同的 properties 的 key 时,后加载的 properties 会覆盖掉先加载的 properties,所以理解并控制 Properties 加载顺序,才能避免踩坑。

Properties 加载顺序一般也有 2 种方式

  1. 按 properties 出现的先后顺序
  2. 显示使用 order 指定顺序

按 properties 出现的先后顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<context:property-placeholder location="classpath:foo.properties" />
<context:property-placeholder location="classpath:bar.properties" />

</beans>

这样的话,会先加载 foo.properties,再加载 bar.properties。

显示使用 order 指定顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<context:property-placeholder location="classpath:foo.properties"
order="1" ignore-unresolvable="true"/>
<context:property-placeholder location="classpath:bar.properties"
order="2"/>
</beans>

通过 order 属性可以显示指定加载顺序,需要注意的是除去最后一个加载的配置,其他均需添加 ignore-unresolvable=”true”,以防中间加载出现异常,导致后面的配置无法加载。