title: Spring Boot - Environment @Autowired throws NullPointerException
body: <p>I have a project setup using <strong>Spring Boot 0.5.0.M5</strong>.</p>
  
  <p>In one of the configuration files I am trying to <code>@Autowire Environment</code> but that fails with a <code>NullPointerException</code>.</p>
  
  <p>Here's what I have so far:</p>
  
  <p><strong>Application.java</strong></p>
  
  <pre><code>@EnableAutoConfiguration
  @Configuration
  @ComponentScan
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p><strong>JpaConfig.java</strong> where I am trying to <code>@Autowire Environment</code></p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = "com.ui.persistence.repository")
  public class JpaConfig {
      private static final String DATABASE_DRIVER = "db.driver";
      private static final String DATABASE_PASSWORD = "db.password";
      private static final String DATABASE_URL = "db.url";
      private static final String DATABASE_USERNAME = "db.username";
      private static final String HIBERNATE_DIALECT = "hibernate.dialect";
      private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
      private static final String ENTITYMANAGER_PACKAGES_TO_SCAN 
          = "entitymanager.packages.to.scan";
  
      @Autowired
      private Environment env;
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(env.getProperty(DATABASE_DRIVER));
          dataSource.setUrl(env.getProperty(DATABASE_URL));
          dataSource.setUsername(env.getProperty(DATABASE_USERNAME));
          dataSource.setPassword(env.getProperty(DATABASE_PASSWORD));
          return dataSource;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean 
                  = new LocalContainerEntityManagerFactoryBean();
          entityManagerFactoryBean.setDataSource(dataSource());
          entityManagerFactoryBean.setPersistenceProviderClass(
                  HibernatePersistence.class);
          entityManagerFactoryBean.setPackagesToScan(
                  env.getProperty(ENTITYMANAGER_PACKAGES_TO_SCAN));
          entityManagerFactoryBean.setJpaProperties(hibernateProperties());
          return entityManagerFactoryBean;
      }
  }
  </code></pre>
  
  <p>I am trying to load the database properties configured in a properties file. However, the <code>Environment</code> is not injected and the code fails with <code>NullPointerException</code>. I do not have any configuration in XML files.</p>
  
  <p>For the properties file I have configured <code>PropertySourcesPlaceholderConfigurer</code> this way:</p>
  
  <pre><code>@Configuration
  @PropertySource("classpath:database.properties")
  public class PropertyConfig {
      @Bean
      public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
          return new PropertySourcesPlaceholderConfigurer();
      }
  }
  </code></pre>
  
  <p>I have tried swapping <code>@Autowired</code>, <code>@Resource</code> and <code>@Inject</code> but nothing has worked so far. Would appreciate any help. Thanks.</p>