0

I have strange error on attempt to inject int primitive via Spring @Value annotation (Spring version 3.2.13). Short description: Spring is trying to inject bean of primitive type (int in my case) instead of primitive itself.

  • I have property file "myProps.properties" with property

    number.of.search.log.events.in.queue=4
    
  • Configuration class on scanned path with content

    @Configuration
    @ComponentScan(basePackages = {
            "com.search.log"
    })
    @PropertySource("classpath:myProps.properties")
    @EnableAspectJAutoProxy
    public class SearchLogConfiguration {
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer  propertySourcesPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    
    }
    
  • Aspect where primitive should be injected

    @Aspect
    @Component
    public class SearchLogAspectHandler {
    
        @Value("${number.of.search.log.events.in.queue:2}")
        public int numberOfSearchLogEventsInQueue;
    ...
    }
    

Every time on application start I got this exception:

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)

Also primitives can't be injected into other beans as well.

Question: Please, help to find out why Spring can't inject primitive, but instead is trying to inject bean of type [int] and can't found one.

dim1902
  • 1,706
  • 4
  • 18
  • 31

1 Answers1

0

Change @PropertySource("classpath:applicationConfig.properties") into @PropertySource("classpath:myProps.properties") or use @PropertySources annotation.

Arek
  • 3,106
  • 3
  • 23
  • 32
  • Thank you for comment, my mistake - in both cases myProps.properties are used. Unfortunately it is not source of bug – dim1902 Apr 16 '15 at 08:56