Voj*_*ech 16 java spring spring-mvc
美好的一天,我正在使用Spring 4.1.1.RELEASE开发Web应用程序.所有Spring配置都是使用注释完成的,除了一件事外它工作正常:
我在项目中有这些行的config.properties文件
cases.caseList.filter=test
cases.caseList.numberOfCasesPerPage=50
Run Code Online (Sandbox Code Playgroud)我有一个配置类
@Configuration
@ComponentScan(basePackageClasses={CaseConfig.class})
@PropertySource(value = "classpath:config.properties")
public class CasesModuleTestContextConfig { ... }
Run Code Online (Sandbox Code Playgroud)另一堂课
@Component
public class HttpRequestParamsToPaginationParams extends AbstractConverter<Map<String, String>, PaginationParams> {
@Value("${cases.caseList.filter}")
private String filter;
@Value("${cases.caseList.numberOfCasesPerPage}")
private Integer count;
...
}
Run Code Online (Sandbox Code Playgroud)属性'过滤器'的值是从属性资源中成功注入的.但我在财产'计数'上得到例外:
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]
...
Run Code Online (Sandbox Code Playgroud)
当我将属性'count'的类型更改为String时,它开始工作:
@Value("${cases.caseList.numberOfCasesPerPage}")
private String count;
Run Code Online (Sandbox Code Playgroud)
我相信Spring能够在使用@Value将属性资源的值注入Integer属性时将String转换为Integer.我找到了人们在没有抱怨的情况下使用的例子.你有任何想法,为什么它对我不起作用?
非常感谢提前.
Lov*_*abu 20
如果您尝试使用@Value("")注释访问属性值,则应声明PropertySourcesPlaceholderConfigurerBean.
尝试在配置类中添加以下代码段.
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)
如果您不想声明它,请通过在org.springframework.core.env.Environment类中自动装配来尝试使用类,以获取属性值.
@Autowired
private Environment environment;
public void readValues() {
System.out.println("Some Message:"
+ environment.getProperty("<Property Name>"));
}
Run Code Online (Sandbox Code Playgroud)
就我而言,我忘记了注释中的 $ 符号
@Value("{cases.caseList.filter}")
Run Code Online (Sandbox Code Playgroud)
代替
@Value("${cases.caseList.filter}")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62576 次 |
| 最近记录: |