我有一个名为provider.xml的spring文件
<context:property-placeholder location="classpath:META-INF/spring
/${build.profile.id}/config.properties" />
我在META-INF / spring / ws1中有两个不同的config.properties,另一个在META-INF / spring / ws2中。
这是我pom.xml的一部分
<profiles>
<profile>
<id>ws1</id>
<properties>
<build.profile.id>ws1</build.profile.id>
</properties>
</profile>
<profile>
<id>ws2</id>
<properties>
<build.profile.id>ws2</build.profile.id>
</properties>
</profile>
</profiles>
我收到此错误:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring/${build.profile.id}/config.properties] cannot be opened because it does not exist
但是,如果我尝试不使用变量来读取它,那是可行的:
<context:property-placeholder location="classpath:META-INF/spring/ws1/config.properties" />
如何在context:property-placeholder中将其读取为变量?
将这段代码添加到provider.xml之后
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true" />
</bean>
我收到此错误:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0' defined in null: Could not resolve placeholder 'build.profile.id' in value "classpath:META-INF/spring/${build.profile.id}/config.properties"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'build.profile.id' in value "classpath:META-INF/spring/${build.profile.id}/config.properties"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:223)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:222)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:283)
答案 0 :(得分:0)
如果您询问如何设置运行时使用哪个配置文件,则将以下内容添加到运行命令中,如下所示:
java -jar -Dspring.profiles.active=ws1 myjar.jar
您还可以在设置中设置默认配置文件,如下所示:
<profile>
<id>ws1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>ws1</build.profile.id>
</properties>
</profile>
在pom中,您需要将属性设置为默认值,然后可以在运行时将其覆盖:
<properties>
<build.profile.id>ws1</build.profile.id>
...
</properties>
答案 1 :(得分:0)
我解决了这个问题,所以: 我像这样使用PropertySourcesPlaceholderConfigurer Bean代替PropertyPlaceholderConfigurer:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/application.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
我删除了:
<context:property-placeholder location="classpath:META-INF/spring/ws1/config.properties" />