【发布时间】:2014-08-19 14:01:59
【问题描述】:
我使用的是 Spring 3.2。我正在尝试根据以下 Maven 配置文件加载属性文件。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
applicationContext.xml
<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:props/connection-${env}.properties"/>
</bean>
这里的{env} 应该替换为 maven profile.but 它没有替换。低于异常。
org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常是 java.io.FileNotFoundException:类路径资源 [props/connection-{env}.properties] 无法打开,因为它不存在。
我正在加载应用程序上下文:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
当我做 clean install {env} 值时,必须用 maven 配置文件替换。
有什么帮助吗?
【问题讨论】:
-
在这种情况下您使用 Maven(构建时)配置文件而不是 Spring(运行时)配置文件的任何原因,或者 Maven 配置文件是否也用于其他用途?