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;

Java8を使用したSpringboot1.5.9で動作します。

于 2018-02-16T00:28:00.820 に答える
7

PropertyPlaceholderConfigurerが欠落しているようです。Beanファクトリポストプロセッサとして登録する必要があります。理論的には、これは次のように実行できます。

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

がPipelineConfigにどのように含まれているかに注意してください。ただし@Value、オプションがロードされる実際のプロパティは、ジョブクラスで指定されています。

于 2015-10-19T16:15:20.287 に答える
1

プロパティファイルも必要ありません。例:

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

于 2015-08-05T07:48:40.433 に答える