java - 带有 Spring PropertyPlaceholderConfigurer 属性标志的 IllegalArgumentException

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

在我的应用程序中,我使用 SpringBoot 和 Spring Batch(和管理)框架。我还使用 application.yaml 文件来存储我需要的所有属性。我在属性方面遇到问题,因为在 SpringBatchAdmin 中创建了一个 PropertyPlaceholderConfigurer bean,该 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 注释获取的所有属性。因此,发生的情况是,我有其他依赖项声明了自己的 Properties,并且 Spring 强制我将这些 Properties 放入 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,则可以在我的 application.yaml 文件中有效地找到属性“find.me”。

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

我该如何解决这个问题?

最佳答案

您可以尝试定义一个 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;
    }
}

关于java - 带有 Spring PropertyPlaceholderConfigurer 属性标志的 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39203196/

相关文章:

java - 在运行时更新 JTree 节点背景颜色

java - 如何将值从 Thymeleaf 传递到 Ajax 调用

java - 在 Spring Boot 中获取所有日志作为 HTTP 响应

ios - 为什么 Info.plist 在我的 Cocoa Touch 框架中被覆盖?

java - 更新 Web 应用程序中的属性文件

java - 如何在提交之前将 hibernate 查询从插入更改为更新

java - 在Java中编写密码并获取所有属性的正确方法

spring-boot - 如何在 Spring Boot 中禁用日志记录的自动配置?

java - 如何将字段名称传递给属性文件中的全局消息

java - 保留 GWT 可视化 API 数据表