当前位置: 首页 > 知识库问答 >
问题:

@Value -> 无法将类型为“字符串”的值转换为所需的类型“java.lang.Integer”

魏翰
2023-03-14

您好,我正在使用Spring 4.1.1.RELEASE开发一个web应用程序。所有的Spring配置都是通过注释完成的,除了一点之外,它运行得很好:

>

  • 我在项目中有一个包含这些行的config.properties文件

    cases.caseList.filter=test
    cases.caseList.numberOfCasesPerPage=50
    

    我有一个配置类

    @Configuration
    @ComponentScan(basePackageClasses={CaseConfig.class})
    @PropertySource(value = "classpath:config.properties")
    public class CasesModuleTestContextConfig { ... }
    

    和另一个类

    @Component
    public class HttpRequestParamsToPaginationParams extends AbstractConverter<Map<String, String>, PaginationParams> {
    
        @Value("${cases.caseList.filter}")
        private String filter;
    
        @Value("${cases.caseList.numberOfCasesPerPage}")
        private Integer count;
    
        ...
    }
    

    属性“filter”的值已成功从属性资源中注入。但是我在属性“计数”上得到了一个例外:

         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]
         ...
    

    当我将属性“count”的类型更改为String时,它开始工作:

            @Value("${cases.caseList.numberOfCasesPerPage}")
            private String count;
    

    我相信当使用@Value将值从属性资源注入整数属性时,Spring能够将字符串转换为整数。我找到了人们使用而不抱怨的例子。你知道为什么它不适合我吗?

    非常感谢。

  • 共有2个答案

    淳于宏伯
    2023-03-14

    在我的例子中,我在注释中放弃了$符号

    @Value("{cases.caseList.filter}")
    

    而不是

    @Value("${cases.caseList.filter}")
    
    娄鹤轩
    2023-03-14

    如果尝试使用@Value(“”)注释访问属性值,则应声明PropertySourcesPlaceholderConfigurerBean。

    尝试在配置类中添加以下代码片段。

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
    

    如果您不想声明它,请尝试使用组织.Spring框架.core.env.环境类,方法是在您的类中自动布线它,以获取属性值。

    @Autowired
    private Environment environment;
    
    public void readValues() {
        System.out.println("Some Message:"
                + environment.getProperty("<Property Name>")); 
    
    }
    
     类似资料: