title: Using Spring @Value in bean construction
body: <p>When my SpringApplicationContext is being boostrapped together, I want to get some values for that out of a properties file. From what I've read, it's best to use <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html" rel="nofollow">PropertySourcesPlaceholderConfigurer</a> for this. I'm creating that bean in my spring java config and it is properly resolving the properties.</p>
  
  <p>The problem is when I'm trying to use those properties in the construction of my other spring beans, like in my hibernate config. In my hibernate config, I have the following field:</p>
  
  <pre><code>    @Value(value = "${myapp.database-access.url}")
      public static String DB_ACCESS_URL;
  </code></pre>
  
  <p>Then I use that property to declare the url to access the database in my datasource bean like this:</p>
  
  <pre><code>    @Bean
      public DataSource dataSource() {
  
          BasicDataSource dataSource = new BasicDataSource();
  
          dataSource.setDriverClassName(DRIVER_CLASS_NAME);
          dataSource.setUrl(DATABASE_URL);
          dataSource.setUsername(DATABASE_USERNAME);
          dataSource.setPassword(DATABASE_PASSWORD);
  
          return dataSource;
      }
  </code></pre>
  
  <p>Unfortunately the value is null at that point so my application bootstrapping fails. Post-construction the value is available, but apparently not mid-construction. Is there a way to get the @Value annotation to work for me in this circumstance, or do I have to use something else?</p>
  
  <p>If I have to use something else, does anyone have any suggestions?</p>