【发布时间】:2015-04-16 08:27:41
【问题描述】:
我在尝试通过 Spring @Value 注释(Spring 版本 3.2.13)注入 int 原语时遇到奇怪的错误。简短描述:Spring 正在尝试注入原始类型的 bean(在我的例子中是 int)而不是原始类型本身。
-
我有带有属性的属性文件“myProps.properties”
number.of.search.log.events.in.queue=4 -
带有内容的扫描路径上的配置类
@Configuration @ComponentScan(basePackages = { "com.search.log" }) @PropertySource("classpath:myProps.properties") @EnableAspectJAutoProxy public class SearchLogConfiguration { @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } -
应该注入原语的方面
@Aspect @Component public class SearchLogAspectHandler { @Value("${number.of.search.log.events.in.queue:2}") public int numberOfSearchLogEventsInQueue; ... }
每次应用程序启动时我都会遇到这个异常:
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public int numberOfSearchLogEventsInQueue; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [int] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Value(value=${number.of.search.log.events.in.queue})}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
也不能将原语注入到其他 bean 中。
问题:请帮忙找出为什么 Spring 不能注入原语,而是试图注入类型为 [int] 的 bean 却找不到。
【问题讨论】:
-
哪个版本的 Spring? 3.0.5 有一个可能导致此问题的错误:jira.spring.io/browse/SPR-8574
-
您是否配置了
PropertySourcePropertyPlaceholderConfigurer? -
是的,已配置,但结果相同
-
stackoverflow.com/questions/56030501/… 试试这个它的工作原理
标签: java spring dependency-injection aspectj