Spring PropertyPlaceholderConfigurer属性标志

时间:2016-08-29 09:47:16

标签: java spring-boot properties-file illegalargumentexception spring-batch-admin

在我的应用程序中,我使用SpringBoot和Spring Batch(和admin)框架。我还使用application.yaml文件来存储我需要的所有属性。我在使用Properties时遇到问题,因为在SpringBatchAdmin中创建了一个PropertyPlaceholderConfigurer bean,其标志ignoreUnresolvablePlaceholders设置为false。这是前面提到的bean:

<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
                <value>classpath:batch-default.properties</value>
                <value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="order" value="1" />
    </bean>

我的问题是,当前Spring在这3个文件中搜索所有使用@Value注释获取的属性。所以会发生的是我有其他依赖项已经声明了自己的属性,而Spring正在强迫我将这些属性放在SpringBatchAdmin中创建的PropertyPlaceholderConfigurer bean中声明的3个文件之一中。

因此,例如,以下类/ bean:

@Component
public class Example{
   @Value("${find.me}")
   private String findMe;

   ...
}

必须查看以下3个文件:

batch.properties 
batch-default.properties 
batch-sqlserver.properties

如果属性find.me不在其中一个文件中,则会出现以下异常:

java.lang.IllegalArgumentException: Could not resolve placeholder 'find.me' in string value "${find.me}"

我想补充一点,问题不是来自使用yaml或Spring没有找到&#34; find.me&#34;属性,因为如果我不使用SpringBatchAdmin创建PropertyPlaceholderConfigurer属性&#34; find.me&#34;在我的application.yaml文件中有效找到。

此外,我无法修改有问题的PropertyPlaceholderConfigurer,因为它来自外部来源(不是我的,而是SpringBatchAdmin)。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以尝试定义BeanPostProcessor组件,在ignoreUnresolvablePlaceholders bean创建后将true字段设置为PropertyPlaceholderConfigurer

@Component
class PropertyPlaceholderConfig extends BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof PropertyPlaceholderConfigurer && beanName.equals("placeholderProperties")) {
            ((PropertyPlaceholderConfigurer) bean).setIgnoreUnresolvablePlaceholders(true);
        }

        return bean;
    }
}
相关问题