我有一个spring @configuration注释类MappingsClientConfig,其布尔字段为:
@Value("${mappings.enabled:true}")
private boolean mappingsEnabled;
这个类被导入到另一个带注释的类中,如下所示:
@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
关于我缺少什么的任何线索?
答案 0 :(得分:10)
它是一个旧线程,但如果您仍想使用@Value Spring注释注入非String值,请执行以下操作:
@Value("#{new Boolean('${item.priceFactor}')}")
private Boolean itemFactorBoolean;
@Value("#{new Integer('${item.priceFactor}')}")
private Integer itemFactorInteger;
使用Java 8在Spring Boot 1.5.9上为我工作。
答案 1 :(得分:5)
您似乎错过了PropertyPlaceholderConfigurer。您需要将其注册为bean工厂后处理器。从理论上讲,这可以这样做:
public class PostProcessorConfig {
@Bean
public BeanFactoryPostProcessor getPP() {
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setLocations(new Resource[]{new ClassPathResource("/my.properties")});
return configurer;
}
}
但是,似乎有bug导致其他问题从基于java的配置中执行此操作。查看解决方法的票证。
答案 2 :(得分: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 ...
}
properties config loader:
public class PipelineConfig {
@Value("${option}")
private boolean option;
}
注意@Value
在PipelineConfig中的位置,但是加载选项的实际属性是在作业类中指定的。
答案 3 :(得分:1)
您甚至不需要属性文件,例如:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />