小编Axx*_*ris的帖子

将spring @value注释评估为原始布尔值

我有一个spring @configuration注释类MappingsClientConfig,其布尔字段为:

 @Value("${mappings.enabled:true}")
    private boolean mappingsEnabled;
Run Code Online (Sandbox Code Playgroud)

这个类被导入另一个spring注释类,如下所示:

@Configuration
@Import(MappingsClientConfig.class)
public class LookupManagerConfig {
Run Code Online (Sandbox Code Playgroud)

当在测试用例中通过Spring上下文实例化类时,容器无法将字符串解析为布尔字段mappingsEnabled,我得到:

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) …
Run Code Online (Sandbox Code Playgroud)

spring annotations

19
推荐指数
2
解决办法
4万
查看次数

标签 统计

annotations ×1

spring ×1