2012-11-27 27 views
11

Tôi có một MappingsClientConfig lớp xuân @configuration chú thích với một lĩnh vực boolean như:Đánh giá mùa xuân @value chú thích như nguyên thủy boolean

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

Lớp này được nhập khẩu vào mùa xuân khác lớp chú thích như sau:

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

khi khởi tạo lớp thông qua ngữ cảnh mùa xuân trong trường hợp thử nghiệm, vùng chứa không thể phân tích cú pháp chuỗi thành trường boolean mappingsEnabled và tôi nhận được:

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 

Bất kỳ khách hàng tiềm năng nào về những gì tôi bị thiếu?

+0

Tôi nghĩ rằng vấn đề là do thực tế là bạn không tải tệp thuộc tính mà bạn đã xác định 'mappings.enabled', nhưng tôi không thể chắc chắn nếu không có thêm chi tiết. –

Trả lời

5

Có vẻ như bạn đang thiếu PropertyPlaceholderConfigurer. Bạn cần phải đăng ký nó như là một bộ xử lý bài nhà máy đậu. Về mặt lý thuyết điều này có thể được thực hiện như thế này:

public class PostProcessorConfig { 

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

Tuy nhiên, có vẻ là một bug gây các vấn đề khác làm điều này từ một cấu hình dựa java. Xem vé cho cách giải quyết.

+1

Thêm PropertyPlaceholderConfigurer đã thực hiện thủ thuật. Nhưng ban đầu khi tôi đã cố gắng để xác định một PropertyPlaceholderConfigurer mới, tôi đã nhận được lỗi cho thiếu giữ chỗ mà làm việc tốt trước khi. Như thể định nghĩa PropertyPlaceholderConfigurer mới đã ghi đè một số hành vi hiện có. Vì vậy, bây giờ, tôi đã xác định một PPC mới và thêm tất cả các thuộc tính của tôi vào một tệp propeties đơn được nạp bởi nó. Nhưng câu hỏi vẫn còn đó là những tài sản được nạp từ trước? – 212

1

Bạn thậm chí không cần phải nộp tài sản, ví dụ .:

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

+1

Bạn có thể làm rõ nơi đi dòng đó không? Trong xml của LookupManagerConfig? Nếu tôi hiểu rõ, nó sẽ tương đương với việc thêm '@Autowired PropertyPlaceholderConfigurer configurer;' trong LookupManagerConfig để sử dụng cấu hình java? – jmmut

+0

@jmmut rõ ràng nó sẽ là applicationContext.xml! – positivecrux

2

Đây là cách nó đã được giải quyết trong dự án của chúng tôi, như là câu trả lời khác không làm việc cho chúng tôi. Chúng tôi cũng đang sử dụng lô xuân.

chính cấu hình công việc:

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

tính cấu hình bộ nạp:

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

Lưu ý cách @Value là trong PipelineConfig, nhưng các thuộc tính thực tế để từ đó lựa chọn được nạp, được quy định trong lớp công việc.

0

nó một chủ đề cũ nhưng nếu bạn vẫn muốn tiêm giá trị Non-String sử dụng @value chú thích mùa xuân, làm điều này:

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

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

trình cho tôi vào mùa xuân khởi động 1.5.9 với Java 8.

Các vấn đề liên quan