gpt4 book ai didi

java - @Value -> 无法将类型 'java.lang.String' 的值转换为所需的类型 'java.lang.Integer'

转载 作者:IT老高 更新时间:2023-10-28 13:52:47 25 4
gpt4 key购买 nike

美好的一天,我正在使用 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 { ... }
  • 还有一个类(class)

    @Component
    public class HttpRequestParamsToPaginationParams extends AbstractConverter<Map<String, String>, PaginationParams> {

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

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

    ...
    }

属性“过滤器”的值已成功从属性资源中注入(inject)。但我在属性“计数”上遇到了一个异常(exception):

     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;

我相信 Spring 能够在使用 @Value 将属性资源中的值注入(inject) Integer 属性时将 String 转换为 Integer。我找到了人们使用而不提示的例子。你有什么想法为什么它对我不起作用?

非常感谢。

最佳答案

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

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

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

如果您不想声明它,请尝试通过在您的类中 Autowiring org.springframework.core.env.Environment 类来获取属性值。

@Autowired
private Environment environment;

public void readValues() {
System.out.println("Some Message:"
+ environment.getProperty("<Property Name>"));

}

关于java - @Value -> 无法将类型 'java.lang.String' 的值转换为所需的类型 'java.lang.Integer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27106055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com