简体   繁体   English

带有Spring PropertyPlaceholderConfigurer属性标志的IllegalArgumentException

[英]IllegalArgumentException with Spring PropertyPlaceholderConfigurer properties flag

In my app I am using SpringBoot and the Spring Batch(and admin) frameworks. 在我的应用程序中,我使用的是SpringBoot和Spring Batch(和admin)框架。 I am also using an application.yaml file to store all of the Properties I need. 我还使用application.yaml文件来存储我需要的所有属性。 I am having trouble with Properties because there is a PropertyPlaceholderConfigurer bean created in SpringBatchAdmin that has the flag ignoreUnresolvablePlaceholders set to false . 我在使用Properties时遇到问题,因为在SpringBatchAdmin中创建了一个PropertyPlaceholderConfigurer bean,其标志ignoreUnresolvablePlaceholders设置为false Here is the aforementioned bean: 这是前面提到的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>

My problem is that currently Spring searches in these 3 files to read ALL OF the properties that are fetched using the @Value annotation. 我的问题是,当前Spring在这3个文件中搜索所有使用@Value注释获取的属性。 So what happens is that I have other dependencies that have declared their own Properties and Spring is forcing me to put these Properties inside one of the 3 files declared in the PropertyPlaceholderConfigurer bean created in SpringBatchAdmin. 所以会发生的事情是我有其他依赖项已经声明了自己的属性,并且Spring强迫我将这些属性放在SpringBatchAdmin中创建的PropertyPlaceholderConfigurer bean中声明的3个文件之一中。

So, for example, the following class/bean: 因此,例如,以下类/ bean:

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

   ...
}

will have to look in the following 3 files: 将不得不查看以下3个文件:

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

and if the property find.me is not in one of these files, then I get the following exception: 如果属性find.me不在其中一个文件中,那么我得到以下异常:

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

I want to add that the problem DOES NOT come from using yaml or Spring not finding the "find.me" property, since if I don't use SpringBatchAdmin that creates the PropertyPlaceholderConfigurer the property "find.me" is effectively found in my application.yaml file. 我想补充一点,问题不是来自使用yaml或Spring没有找到“find.me”属性,因为如果我不使用SpringBatchAdmin创建PropertyPlaceholderConfigurer ,则在我的application.yaml有效地找到属性“find.me” application.yaml文件。

Also, I cannot modify the PropertyPlaceholderConfigurer in question because it comes from an external source(not mine, but SpringBatchAdmin). 此外,我无法修改有问题的PropertyPlaceholderConfigurer ,因为它来自外部源(不是我的,而是SpringBatchAdmin)。

How can I solve this? 我怎么解决这个问题?

You can try defining a BeanPostProcessor component that sets the ignoreUnresolvablePlaceholders field to true after the PropertyPlaceholderConfigurer bean has been created: 您可以尝试定义一个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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从PropertyPlaceholderConfigurer类Spring中读取属性 - Not able to read properties from PropertyPlaceholderConfigurer class spring Spring:无法使用PropertyPlaceholderConfigurer加载junit上的属性 - Spring: can't load properties on junit with PropertyPlaceholderConfigurer 打印通过Spring PropertyPlaceholderConfigurer设置的所有属性 - Printing all properties set via Spring PropertyPlaceholderConfigurer 春天的多种特性。 PropertyPlaceholderConfigurer类中的属性 - multiple properties in spring. property in PropertyPlaceholderConfigurer class Spring PropertyPlaceholderConfigurer问题加载属性文件 - Spring PropertyPlaceholderConfigurer Issue to Load A Properties File 如何在Junit中为Spring propertyPlaceholderConfigurer创建临时属性文件 - How to create temp properties file for Spring propertyPlaceholderConfigurer within Junit Spring Spring PropertyPlaceholderConfigurer可以忽略属性位置路径中的未设置属性吗? - Can Spring PropertyPlaceholderConfigurer ignore unset properties in a property location path? 如何覆盖从Spring PropertyPlaceHolderConfigurer加载的特定属性? - How to override specific properties loaded from spring PropertyPlaceHolderConfigurer? 春季:PropertyPlaceHolderConfigurer设置非字符串/整数属性的值 - Spring: PropertyPlaceHolderConfigurer to set values for non-string/integer properties Spring的PropertyPlaceholderConfigurer可以用代码而不是bean读取整个属性文件吗? - Can Spring's PropertyPlaceholderConfigurer read an entire properties file in code, not as a bean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM