【问题标题】:IllegalArgumentException with Spring PropertyPlaceholderConfigurer properties flag带有 Spring PropertyPlaceholderConfigurer 属性标志的 IllegalArgumentException
【发布时间】:2016-08-29 09:47:15
【问题描述】:

在我的应用程序中,我使用 SpringBoot 和 Spring Batch(和管理)框架。我还使用 application.yaml 文件来存储我需要的所有属性。我遇到了属性问题,因为在 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 没有找到“find.me”属性,因为如果我不使用创建 PropertyPlaceholderConfigurer 的 SpringBatchAdmin 属性“find.me”是在我的application.yaml 文件中有效地找到。

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

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

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

    @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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-14
      • 2011-08-23
      • 2010-12-08
      • 1970-01-01
      • 2011-10-04
      • 2013-08-21
      • 1970-01-01
      相关资源
      最近更新 更多