инициализировать контекст: свойство-заполнитель зависит от профиля maven - PullRequest
0 голосов
/ 10 сентября 2018

У меня есть файл пружины с именем provider.xml

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

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

У меня есть два разных config.properties, один в META-INF / spring / ws1, а другой в 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" /> 

как можно прочитать его как переменную в контексте: свойство-заполнитель?

После того, как я добавил этот код в 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)

Ответы [ 2 ]

0 голосов
/ 13 сентября 2018

Я решил проблему так: Я использовал 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" /> 
0 голосов
/ 10 сентября 2018

Если вы спрашиваете, как установить, какой профиль будет использоваться во время выполнения, добавьте следующее к вашей команде запуска, например:

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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...