Java Spring应用程序正在从我的所有属性文件加载属性,而不仅仅是我在context:property placeholder中指定的那些文件

Java Spring应用程序正在从我的所有属性文件加载属性,而不仅仅是我在context:property placeholder中指定的那些文件,java,spring,jpa,maven,properties,Java,Spring,Jpa,Maven,Properties,我有一个spring应用程序,我正在尝试为不同的部署场景进行不同的配置。例如,对于集成测试,我希望我的应用程序使用内存中的H2数据库,但对于部署到预生产环境,我希望配置远程MySql数据源。在做了一些深入研究之后,我找到了一种方法,该方法依赖于在应用程序上下文中使用元素来指定要加载的属性文件。以下是my applicationContext.xml中的相关代码段: <context:property-placeholder location="classpath:META-INF/spri

我有一个spring应用程序,我正在尝试为不同的部署场景进行不同的配置。例如,对于集成测试,我希望我的应用程序使用内存中的H2数据库,但对于部署到预生产环境,我希望配置远程MySql数据源。在做了一些深入研究之后,我找到了一种方法,该方法依赖于在应用程序上下文中使用元素来指定要加载的属性文件。以下是my applicationContext.xml中的相关代码段:

<context:property-placeholder location="classpath:META-INF/spring/database_${spring.profiles.active}.properties"/>
以下是两个属性文件的内容:

数据库_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=
#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
数据库\u localdev.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=
#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>
当活动配置文件为“build”时,我在日志文件中看到了这一点

因此,根据“我的上下文:属性占位符”设置,似乎正在执行spring.active.profile值并加载正确的文件。但是,“build”文件似乎总是被加载的,并且似乎总是优先于“localdev”文件中定义的属性。我能够获得“localdev”文件中的属性值的唯一方法是注释掉“build”文件中的所有行

我目前对正在发生的事情感到困惑。是否有其他一些我不考虑的规则可以解释要加载的两个属性文件

编辑 my POM中与资源筛选和属性processin相关的部分:

<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>

src/main/resources
真的
...
org.codehaus.mojo
属性maven插件
1.0-α-2
初始化
读取项目属性
${basedir}/src/main/resources/META-INF/spring/database_build.properties

从POM片段中可以看出,您正在使用总是从database_build.properties文件读取的属性筛选资源


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

您能展示一下maven pom配置吗?当然可以。起初我并不认为这有什么关系,但我会编辑我的帖子,把POM的那个部分包括进去。当你搜索错误时,你必须向各个方向看,但这看起来很好。是的,就是这样。maven插件在“构建”配置中读取属性,这使得我试图从applicationContext.xml中选择配置的尝试毫无结果。有趣的是,我甚至不记得当初为什么要使用这个插件。
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]
<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>