Spring应用程序正在加载我所有属性文件中的属性,而不仅仅是我在上下文中指定的属性:property-placeholder

时间:2012-05-08 07:13:45

标签: java spring jpa maven properties

我有一个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]

当活动配置文件为“build”

时,我在日志文件中看到了这一点
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]

所以看起来spring.active.profile值正在被尊重,并且由于我的上下文而导致正在加载正确的文件:property-placeholder settings。但是,“build”文件似乎总是被加载,并且似乎总是优先于“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>

1 个答案:

答案 0 :(得分:1)

从POM片段看,您正在使用始终从database_build.properties文件中读取的属性来过滤资源。

这意味着在使用这些属性进行构建期间,applicationContext.xml中的占位符由maven填充,并且没有留下让占位符配置器真正配置。 Spring只读取你告诉它的属性文件,但到那时已经太晚了。