我有一个 spring 应用程序,我试图为不同的部署场景进行不同的配置。例如,对于集成测试,我希望我的应用程序使用内存数据库中的 H2,但对于部署到我们的预生产环境,我想配置一个远程 MySql 数据源。在进行了一些挖掘之后,我找到了一种方法,该方法依赖于在我的应用程序上下文中使用元素来指定我想要加载的属性文件。这是我的 applicationContext.xml 中的相关片段:
<context:property-placeholder location="classpath:META-INF/spring/database_${spring.profiles.active}.properties"/>
然后我在我的 Maven POM 文件中定义 spring.profiles.active 属性,如下所示:
<properties>
<spring.profiles.active>localdev</spring.profiles.active>
...
</properties>
当我运行“mvn test”时,这将允许我的应用程序可以使用属性值(这就是我执行单元测试并尝试解决此问题的方式)
我有几个不同的数据库配置属性文件,它们并排存在于我的 /src/main/resources/META-INF/spring 文件夹中,这是我的项目文件夹结构的相关子集:
META-INF
|
+--spring
| |
| +--database_build.properties
| +--database_localdev.propertes
| +--applicationContext.xml
|
+--persistence.xml
以下是两个属性文件的内容:
database_build.properties
#Updated at Thu Apr 26 17:35:43 PDT 2012
#Thu Apr 26 17:35:43 PDT 2012
database.persistenceUnit=persistenceUnitH2
database.driverClassName=org.h2.Driver
database.url=jdbc\:h2\:mem\:;MODE=MySQL;INIT=create schema IF NOT EXISTS TTS_ADMIN;TRACE_LEVEL_SYSTEM_OUT=3
database.username=sa
database.password=
database_localdev.properties
#Updated at Thu Apr 26 17:35:43 PDT 2012
#Thu Apr 26 17:35:43 PDT 2012
database.persistenceUnit=persistenceUnitMySql
database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc\:mysql\://localhost\:3306/TTS_ADMIN
database.username=ttsaSchemaMgrUsr
database.password=password
这里是引用这些属性值来设置数据源的 applicationContext.xml 的相关部分
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
...
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="liquibase" id="entityManagerFactory">
<property name="persistenceUnitName" value="${database.persistenceUnit}"/>
<property name="dataSource" ref="dataSource"/>
</bean>
从这个配置中可以看出,要加载的持久性单元由 database.persistenceUnit 属性的值决定。这是我的 persistence.xml 文件,其中定义了两个持久性单元:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<!-- An MySql persistence unit -->
<persistence-unit name="persistenceUnitMySql" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<!-- Uncomment the following two properties for JBoss only -->
<!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
<!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
</properties>
</persistence-unit>
<!-- An in-memory persistence unit -->
<persistence-unit name="persistenceUnitH2" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<!-- Uncomment the following two properties for JBoss only -->
<!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
<!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
</properties>
</persistence-unit>
</persistence>
现在,问题是无论我是否将 spring.profiles.active 属性设置为“build”或“localdev”,我总是会获取 database_localdev.properties 文件的属性值。我已打开跟踪日志记录,当活动配置文件为“localdev”时,我在日志文件中看到以下行:
2012-05-07 17:47:17,155 [main] INFO org.springframework.context.support.PropertySourcesPlaceholderConfigurer - Loading properties file from class path resource [META-INF/spring/database_localdev.properties]
当活动配置文件为“构建”时,我在日志文件中看到了这一点
2012-05-07 17:47:17,155 [main] INFO org.springframework.context.support.PropertySourcesPlaceholderConfigurer - Loading properties file from class path resource [META-INF/spring/database_build.properties]
因此,由于我的 context:property-placeholder 设置,似乎正在尊重 spring.active.profile 值并且正在加载正确的文件。但是,“构建”文件似乎总是被加载,并且似乎总是优先于“localdev”文件中定义的属性。我能够在“localdev”文件中获得要兑现的属性值的唯一方法是注释掉“build”文件中的所有行。
我目前对正在发生的事情感到困惑。是否有其他一些我没有考虑到的规则会导致我的两个属性文件都被加载?
编辑 我的 POM 中与资源过滤和属性处理相关的部分:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/META-INF/spring/database_build.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>