title: Read file properties using Spring annotations
body: <p>I'm trying to learn how to read properties file using spring. After an internet searching I found that I can use <code>@value</code> and <code>@PropertySource</code> annotations to achieve that. I created a project which has the following structure and classes codes:</p>
  
  <p><strong>Structure of the project:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/5zlZv.png" alt="enter image description here"></p>
  
  <p><strong>AppConfigMongoDB.java implementation:</strong></p>
  
  <pre><code>package com.mongodb.properties;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.PropertySource;
  
  
  @PropertySource("classpath:config/config.properties")
  public class AppConfigMongoDB {
  
  @Value("#{mongodb.url}")
  private String mongodbUrl;
  
  
  @Value("#{mongodb.db}")
  private String defaultDb;
  
  public String getMongoDb()
  {
      return defaultDb;
  }
  
  public String getMongoDbUrl()
  {
      return mongodbUrl;
  }
  }
  </code></pre>
  
  <p><strong>SpringConfiguration.java implementation:</strong></p>
  
  <pre><code> package com.mongodb.properties;
  
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
  
   @Configuration
   public class SpringConfiguration {
   @Bean
   public AppConfigMongoDB getAppConfigMongoDB(){
          return new AppConfigMongoDB();
    }
   }
  </code></pre>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>package com.mongodb.properties;
  
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  
  public class Main {
  
  public static void main(String[] args) {
      ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
      AppConfigMongoDB mongo = applicationContext.getBean(AppConfigMongoDB.class);
      System.out.println("db= "+mongo.getMongoDb());
      System.out.println("URL= "+mongo.getMongoDbUrl());
    }
  }
  </code></pre>
  
  <p>The properties file that I'm reading from called config.properties, it contains the following lines:</p>
  
  <pre><code>mongodb.url=1.2.3.4
  mongodb.db=dataBase
  </code></pre>
  
  <p>I tested this small project and I got a stack trace that contains the following exception:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getAppConfigMongoDB': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.mongodb.properties.AppConfigMongoDB.mongodbUrl; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'mongodb' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
  at org.springframework.context.annotation.AnnotationConfigApplicationContext.&lt;init&gt;(AnnotationConfigApplicationContext.java:84)
  at com.mongodb.properties.Main.main(Main.java:9)
  
   Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'mongodb' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
  at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:226)
  at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:93)
  at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
  at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51)
  at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
  at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
  at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
  at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)
  ... 18 more
  </code></pre>
  
  <p>Is it a problem of Spring calling beans? Or maybe is it a problem of properties file path or something else?</p>