【问题标题】:Strange exception on injection of primitive via Spring @Value annotation通过 Spring @Value 注释注入原语的奇怪异常
【发布时间】: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 却找不到。

【问题讨论】:

标签: java spring dependency-injection aspectj


【解决方案1】:

@PropertySource("classpath:applicationConfig.properties") 更改为@PropertySource("classpath:myProps.properties") 或使用@PropertySources 注释。

【讨论】:

  • 感谢您的评论,我的错误 - 在这两种情况下都使用了 myProps.properties。不幸的是,它不是错误的来源
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 2018-01-24
相关资源
最近更新 更多