将spring @value注释评估为原始布尔值 [英] Evaluating spring @value annotation as primitive boolean
问题描述
我有一个spring @configuration注释类MappingsClientConfig,其布尔字段为:
I have a spring @configuration annotated class MappingsClientConfig with a boolean field as:
@Value("${mappings.enabled:true}")
private boolean mappingsEnabled;
该类被导入到另一个带有弹簧注释的类中,如下所示:
This class is imported into another spring annotated class like so :
@Configuration
@Import(MappingsClientConfig.class)
public class LookupManagerConfig {
在测试用例中通过Spring上下文实例化类时,容器无法将字符串解析为布尔字段mappingsEnabled,而我得到:
when instantiating the class via Spring context in a test-case the container is unable to parse the string into the boolean field mappingsEnabled and I get:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private boolean com.barcap.tcw.mappings.economic.client.config.EconomicMappingsClientConfig.economicMappingsEnabled; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'boolean'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [${mappings.enabled:true}]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
... 138 more
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'boolean'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [${mappings.enabled:true}]
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:61)
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:43)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:718)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 140 more
Caused by: java.lang.IllegalArgumentException: Invalid boolean value [${mappings.enabled:true}]
at org.springframework.beans.propertyeditors.CustomBooleanEditor.setAsText(CustomBooleanEditor.java:124)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:416)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:388)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:157)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:93)
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:49)
... 144 more
有人想念我想念什么吗?
Any leads as to what am I missing?
推荐答案
好像您缺少PropertyPlaceholderConfigurer.您需要将其注册为Bean工厂后处理器.从理论上讲,可以这样做:
Looks like you're missing the PropertyPlaceholderConfigurer. You need to register it as a bean factory post processor. Theoretically this could be done like this:
public class PostProcessorConfig {
@Bean
public BeanFactoryPostProcessor getPP() {
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setLocations(new Resource[]{new ClassPathResource("/my.properties")});
return configurer;
}
}
但是,似乎有一个错误会导致其他问题基于Java的配置.解决方法请参阅票证.
However, there seems to be a bug that causes other issues doing this from a java based configuration. See ticket for workarounds.
这篇关于将spring @value注释评估为原始布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!