0

I am trying to inject properties file into my spring component using CdI and keeps throwing the exception below.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Properties ValidationService.ProjectProperties; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.util.Properties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @PropertiesFromFile(value=config.properties)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

Here is my code:

@Component
public class ValidationService {
    @Autowired
    @PropertiesFromFile("config.properties")
    private Properties configProperties;

    @Autowired
    @PropertiesFromFile("custom.properties")
    private Properties customProperties;
}

Another piece of code that I tried was:

@Component
public class ValidationService {
    @Inject
    @PropertiesFromFile("config.properties")
    private Properties configProperties;

    @Inject
    @PropertiesFromFile("custom.properties")
    private Properties customProperties;
}

Although Spring now supports Inject under AutoWired I still tried both.

I am new to Spring, I would really appreciate if someone could point me to the right direction

P.S. The code for the Generic annotation can be found here: http://slackspace.de/articles/injecting-properties-in-java-ee-applications/

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
Nick Div
  • 5,338
  • 12
  • 65
  • 127
  • Do you have the Producer method configured with the @PropertiesFromFile in your project as mentioned in the link you provided? From your error log, it seems it is not able to find the producer method with the PropertiesFromFile qualifier. – Manish Jun 17 '16 at 11:19
  • @Manish I copy-pasted everything that was in the link provided. Is there anything else that I need to make this work? – Nick Div Jun 17 '16 at 13:07
  • Nick - Sorry for the late answer. I think there might be a problem with the Producer method. Can you please look into this link, and see if this helps - http://stackoverflow.com/questions/25876402/which-is-the-spring-equivalent-for-the-cdi-produces-annotation - The answer describes an equivalent for @Produces in Spring beans, – Manish Jun 20 '16 at 11:03
  • @Manish Dont worry about it, ill read the link that youve sent me, Thanks a lot, – Nick Div Jun 20 '16 at 14:21

0 Answers0