Buon giorno, sto lavorando a un'applicazione Web utilizzando Spring 4.1.1.RELEASE. Tutta la configurazione della molla è fatto con le annotazioni e funziona benissimo tranne una cosa:@Value -> Impossibile convertire il valore di tipo 'java.lang.String' nel tipo richiesto 'java.lang.Integer'
Ho un file config.properties nel progetto con queste righe
cases.caseList.filter=test cases.caseList.numberOfCasesPerPage=50
ho una classe di configurazione
@Configuration @ComponentScan(basePackageClasses={CaseConfig.class}) @PropertySource(value = "classpath:config.properties") public class CasesModuleTestContextConfig { ... }
E un'altra classe
@Component public class HttpRequestParamsToPaginationParams extends AbstractConverter<Map<String, String>, PaginationParams> { @Value("${cases.caseList.filter}") private String filter; @Value("${cases.caseList.numberOfCasesPerPage}") private Integer count; ... }
Il valore del "filtro" proprietà viene inserito correttamente dalla risorsa di proprietà. Ma sto un'eccezione sulla proprietà 'count':
13:58:45.274 [main] WARN o.s.c.s.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cz.pokus.core.test.config.ConversionServiceTestConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List cz.pokus.core.test.config.ConversionServiceTestConfig.converterList; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'httpRequestParamsToPaginationParams': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.Integer cz.pokus.core.cases.converter.HttpRequestParamsToPaginationParams.count; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${cases.caseList.numberOfCasesPerPage}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
...
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${cases.caseList.numberOfCasesPerPage}"
...
Caused by: java.lang.NumberFormatException: For input string: "${cases.caseList.numberOfCasesPerPage}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_20]
at java.lang.Integer.parseInt(Integer.java:569) ~[na:1.8.0_20]
at java.lang.Integer.valueOf(Integer.java:766) ~[na:1.8.0_20]
...
Quando cambio tipo di proprietà 'count' per stringa è iniziare a lavorare:
@Value("${cases.caseList.numberOfCasesPerPage}")
private String count;
Credo primavera è in grado di convertire String a Integer quando si inserisce il valore dalla risorsa di proprietà in una proprietà Integer usando @Value. Ho trovato esempi in cui le persone usano senza lamentarsi. Per favore hai qualche idea del perché non funziona per me?
Grazie mille in anticipo.
Sta funzionando o si sta avviando senza errori. Mi aspetto che il valore sia letteralmente '$ {cases.caseList.numberOfCasesPerPage}' in questo caso. Assicurati di aver registrato un 'PropertySourcesPlaceHolderConfigurer' come un bean' public static'. –