[英]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.