2012-11-27 19 views
11

Ho un MappingsClientConfig molla classe @configuration annotato con un campo booleano come:Valutare primavera @value annotazione come primitiva booleano

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

Questa classe viene importato in un'altra primavera classe annotata in questo modo:

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

durante l'istanziazione della classe tramite il contesto Spring in un caso di test, il contenitore non è in grado di analizzare la stringa nei mapping di campi booleaniEnabled e ottengo:

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 

Eventuali suggerimenti su cosa mi manca?

+0

Penso che il problema sia causato dal fatto che non si sta caricando il file delle proprietà in cui è stato definito 'mappings.enabled', ma non posso essere sicuro senza ulteriori dettagli. –

risposta

5

Sembra manchi il PropertyPlaceholderConfigurer. È necessario registrarlo come post processor di bean factory. Teoricamente questo potrebbe essere fatto in questo modo:

public class PostProcessorConfig { 

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

Tuttavia, sembra che ci sia un bug che provoca altri problemi facendo questo da una configurazione basato su Java. Vedi ticket per soluzioni alternative.

+1

L'aggiunta di PropertyPlaceholderConfigurer ha fatto il trucco. Ma inizialmente quando ho provato a definire un nuovo PropertyPlaceholderConfigurer, ricevevo degli errori per i segnaposto mancanti che funzionavano bene prima. Come se la definizione del nuovo PropertyPlaceholderConfigurer stava sovrascrivendo alcuni comportamenti esistenti. Quindi per ora, ho definito un nuovo PPC e aggiunto tutte le mie proprietà a un singolo file di proprietà caricato da esso. Ma la domanda rimane dove sono state caricate quelle proprietà da prima? – 212

1

È anche non hanno bisogno di un file delle proprietà, ad es .:

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

+1

Potrebbe chiarire dove va quella linea? Nel xml di LookupManagerConfig? Se ho capito bene, dovrebbe essere equivalente aggiungere "@Autowired PropertyPlaceholderConfigurer configureer" in LookupManagerConfig per usare java config? – jmmut

+0

@jmmut ovviamente sarebbe applicationContext.xml! – positivecrux

2

Questo è come è stato risolto nel nostro progetto, come le altre risposte non ha funzionato per noi. Stavamo usando anche il batch di primavera.

lavoro principale config:

@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 ... 
} 

proprietà loader config:

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

noti come il @Value è nel PipelineConfig, ma le proprietà effettive da cui l'opzione è caricata, è specificato nel classe di lavoro

0

Il suo vecchio thread, ma se si vuole ancora di iniettare valori non stringa utilizzando @Value Primavera annotazione, fare questo:

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

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

funziona per me al boot primavera 1.5.9 con Java 8.

Problemi correlati