33

我有一个带有布尔字段的 Spring带@Configuration注释的类MappingsClientConfig

 @Value("${mappings.enabled:true}")
 private boolean mappingsEnabled;

此类被导入另一个 Spring 注释类,如下所示:

@Configuration
@Import(MappingsClientConfig.class)
public class LookupManagerConfig {

在测试用例中通过 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)
    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

关于我错过了什么的任何线索?

4

4 回答 4

54

它是一个旧线程,但如果您仍想使用 @Value Spring 注释注入非字符串值,请执行以下操作:

@Value("#{new Boolean('${item.priceFactor}')}")
private Boolean itemFactorBoolean;

@Value("#{new Integer('${item.priceFactor}')}")
private Integer itemFactorInteger;

在带有 Java 8 的 Spring boot 1.5.9 上为我工作。

于 2018-02-16T00:28:00.820 回答
7

看起来您缺少 PropertyPlaceholderConfigurer。您需要将其注册为 bean factory 后处理器。理论上可以这样完成:

public class PostProcessorConfig {

    @Bean
    public BeanFactoryPostProcessor getPP() {
       PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
       configurer.setLocations(new Resource[]{new ClassPathResource("/my.properties")});
       return configurer;
    }
}

但是,似乎有一个错误会导致基于 java 的配置执行此操作时出现其他问题。有关解决方法,请参阅票证。

于 2012-11-27T14:40:40.717 回答
2

这就是在我们的项目中解决它的方式,因为其他答案对我们不起作用。我们也在使用春季批次。

主要工作配置:

@Configuration
@EnableBatchProcessing
@PropertySource("classpath:application.properties")
public class MainJobConfiguration {
    @Autowired
    PipelineConfig config;

    @Bean
    public PipelineConfig pipelineConfig(){
        return new PipelineConfig();
    }

    // To resolve ${} in @Value
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    // job stuff ...
}

属性配置加载器:

public class PipelineConfig {
    @Value("${option}")
    private boolean option;
}

请注意@ValuePipelineConfig 中的情况,但从中加载选项的实际属性是在作业类中指定的。

于 2015-10-19T16:15:20.287 回答
1

您甚至不需要属性文件,例如:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

于 2015-08-05T07:48:40.433 回答