1

在我的应用程序中,我使用SpringBoot和Spring批处理(和管理)框架。我也使用一个application.yaml文件来存储我需要的所有属性。我在Properties中遇到了问题,因为在SpringBatchAdmin中创建的PropertyPlaceholderConfigurer bean的标记ignoreUnresolvablePlaceholders设置为false。这里是上述豆:IllegalArgumentException异常与Spring PropertyPlaceholderConfigurer属性标志

<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> 

我的问题是,目前春季这3个文件搜索,阅读全部使用@Value注释,被取出的属性。所以会发生的是,我有其他声明自己属性的依赖关系,并且Spring迫使我将这些属性放在SpringBatchAdmin中创建的PropertyPlaceholderConfigurer bean中声明的3个文件之一中。

因此,举例来说,下面的类/豆:

@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)。

我该如何解决这个问题?

回答

0

你可以尝试定义BeanPostProcessor组件设置ignoreUnresolvablePlaceholderstruePropertyPlaceholderConfigurer豆后已创建:

@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; 
    } 
} 
相关问题