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

标签 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 文件的属性值。我已打开跟踪日志记录,当 Activity 配置文件为“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]

当 Activity 配置文件为“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]

因此,由于我的 context:property-placeholder 设置, spring.active.profile 值似乎得到了尊重,并且正确的文件正在加载。然而,“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>

最佳答案

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

这意味着 applicationContext.xml 中的占位符由 Maven 在构建过程中使用这些属性填充,并且占位符配置程序没有留下任何内容来真正配置。 Spring 只读取您告诉它的属性文件,但到那时就已经太晚了。

关于java - Spring 应用程序正在从我的所有属性文件加载属性,而不仅仅是我在上下文 :property-placeholder 中指定的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10494226/

相关文章:

java - JPQL : Access inherited Attributes in a Query

hibernate 错误 java.lang.NoSuchMethodError

java - JPA 实体批量删除不起作用

java - 方法注入(inject)是如何实现的?

java - 404 未找到 : Something wrong with access to a servlet deployed on Tomcat

java - 使用 Active Directory SSO 从 Microsoft IE 到 Java 服务器时,为什么会收到 GSSException?

java - java中抽象类的一些实际例子是什么?

spring - 如何将 [DDL] 从生成类型标识迁移到 postgres db 的生成类型序列

用于读取用户的 Outlook 日历详细信息的 Java 程序

java - 为什么我的构造函数没有实例化变量?