title: How to autowire a Spring Environment object
body: <p>I'm attempting to get properties from a .properties file within an object of my application.  My only knowledge of how to do this is via the <code>Environment</code> object.</p>
  
  <p>It works great in my configuration...</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = {
          "com.production"
  })
  @PropertySource(value = {
          "classpath:/application.properties",
          "classpath:/environment-${FETTER_ENVIRONMENT}.properties"
  })
  @EnableJpaRepositories("com.production.repositories")
  @EnableTransactionManagement
  public class Config {
      private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
      private static final String PROPERTY_NAME_DATABASE_USER = "db.user";
      private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
  
      private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
      private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
      private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
      private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
  
      @Resource
      Environment environment;
  </code></pre>
  
  <p>But I try to do it in another class and it's null.  I've tried using <code>@Resource</code> and <code>@Autowired</code> and manual injection.</p>
  
  <p>Is there something special about <code>Environment</code> that's preventing this?</p>
  
  <pre><code>@Component
  public class Vendor {
      private String token;
  
      @Autowired
      private Environment environment;
  
      public Vendor() {
          //get token from config
          //Environment environment = (Environment) ApplicationContextProvider.getApplicationContext().getBean("environment");
          setToken(environment.getProperty("api.vendor.token"));
      }
  
      public String getToken() {
          return token;
      }
  
      public void setToken(String token) {
          this.token = token;
      }
  }
  </code></pre>
  
  <p><strong>Update:</strong> I found <a href="http://learningviacode.blogspot.com/2012/04/property-files-and-value.html" rel="noreferrer">this resource</a> which highlights the usage of <code>@Value</code>, but I don't use any XML configuration in my application.</p>
  
  <p>It looks like this might be what I'm looking for: <a href="https://stackoverflow.com/questions/14167332/loading-properties-in-spring-3-1-programmatically">Loading properties in Spring 3.1 programmatically</a></p>