我试图通过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并且找不到它。
答案 0 :(得分:0)
将@PropertySource("classpath:applicationConfig.properties")
更改为@PropertySource("classpath:myProps.properties")
或使用@PropertySources
注释。