简体   繁体   中英

initialize context:property-placeholder depend on maven profile

I have a spring file named provider.xml

<context:property-placeholder location="classpath:META-INF/spring

/${build.profile.id}/config.properties" />

I have two different config.properties one in META-INF/spring/ws1 and the other in META-INF/spring/ws2

this is a part of my 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>

I get this error:

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

But if I try to read it without variable like this it is work:

 <context:property-placeholder location="classpath:META-INF/spring/ws1/config.properties" /> 

how can read it as a variable in the context:property-placeholder ?

After I added this code to 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>

I got this error:

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)

If you are asking how to set which profile to be used at runtime, then add the following to your run command like so:

java -jar -Dspring.profiles.active=ws1 myjar.jar

You can also set a default profile in your settings like so:

<profile>
    <id>ws1</id>
    <activation>
       <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <build.profile.id>ws1</build.profile.id>
    </properties>
</profile>

In your pom you will need to set the property with a default which can then be overwritten at runtime:

<properties>
    <build.profile.id>ws1</build.profile.id>
    ...
</properties>

I resolved the problem so: I used PropertySourcesPlaceholderConfigurer Bean instead of PropertyPlaceholderConfigurer like this:

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

And I removed:

<context:property-placeholder location="classpath:META-INF/spring/ws1/config.properties" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM