[SPRING] 스프링 @value 주석을 프리미티브 부울로 평가하기
SPRING스프링 @value 주석을 프리미티브 부울로 평가하기
나는 봄 @configuration 주석 클래스 MappingsClientConfig 부울 필드로 :
@Value("${mappings.enabled:true}")
private boolean mappingsEnabled;
이 클래스는 다음과 같이 다른 스프링 주석 클래스로 가져옵니다.
@Configuration
@Import(MappingsClientConfig.class)
public class LookupManagerConfig {
테스트 케이스에서 스프링 컨텍스트를 통해 클래스를 인스턴스화 할 때 컨테이너는 문자열을 부울 필드 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
내가 뭘 놓치고있는지도 몰라?
해결법
-
==============================
1.PropertyPlaceholderConfigurer가 누락 된 것 같습니다. 빈 공장 포스트 프로세서로 등록해야합니다. 이론적으로 이것은 다음과 같이 할 수 있습니다 :
PropertyPlaceholderConfigurer가 누락 된 것 같습니다. 빈 공장 포스트 프로세서로 등록해야합니다. 이론적으로 이것은 다음과 같이 할 수 있습니다 :
public class PostProcessorConfig { @Bean public BeanFactoryPostProcessor getPP() { PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); configurer.setLocations(new Resource[]{new ClassPathResource("/my.properties")}); return configurer; } }
그러나 Java 기반 구성에서이 작업을 수행하는 다른 문제가 발생하는 버그가있는 것으로 보입니다. 해결 방법은 티켓을 참조하십시오.
-
==============================
2.오래된 스레드이지만 여전히 @Value Spring 주석을 사용하여 비 문자열 값을 주입하려면 다음을 수행하십시오.
오래된 스레드이지만 여전히 @Value Spring 주석을 사용하여 비 문자열 값을 주입하려면 다음을 수행하십시오.
@Value("#{new Boolean('${item.priceFactor}')}") private Boolean itemFactorBoolean; @Value("#{new Integer('${item.priceFactor}')}") private Integer itemFactorInteger;
Java 8을 사용하는 스프링 부트 1.5.9에서 저를 위해 작동합니다.
-
==============================
3.다른 답변이 우리를 위해 작동하지 않았던 것처럼 이것이 프로젝트에서 어떻게 해결되었는지입니다. 우리는 봄 배치도 사용했습니다.
다른 답변이 우리를 위해 작동하지 않았던 것처럼 이것이 프로젝트에서 어떻게 해결되었는지입니다. 우리는 봄 배치도 사용했습니다.
기본 작업 설정 :
@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; }
@Value가 PipelineConfig에있는 방법에 유의하십시오. 그러나 옵션이로드되는 실제 속성은 작업 클래스에 지정됩니다.
-
==============================
4.속성 파일도 필요하지 않습니다 (예 :
속성 파일도 필요하지 않습니다 (예 :
from https://stackoverflow.com/questions/13585259/evaluating-spring-value-annotation-as-primitive-boolean by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] WebSocket 핸드 셰이크 - 예기치 않은 응답 코드 200 - AngularJs 및 Spring Boot (0) | 2019.07.06 |
---|---|
[SPRING] 예외는 봄 MVC에서 대상의 인증서를 확인할 수 없습니다. (0) | 2019.07.06 |
[SPRING] LocalContainerEntityManagerFactoryBean과 LocalEntityManagerFactoryBean의 차이점은 무엇입니까? (0) | 2019.07.05 |
[SPRING] 통합 테스트에서 빈 재정의 (0) | 2019.07.05 |
[SPRING] 스프링 부트 REST 애플리케이션에서 gzipped 요청 처리하기 (0) | 2019.07.03 |