title: How to access non-static properties in the creation of a Spring Factory Bean?
body: <p>Looking for any type of solution here, but I'll propose my thought process along the way.<br>
  Working with Spring-Mybatis (over a MySql database) and running it through the Maven embedded jetty plugin.</p>
  
  <p>So it's a pretty simple issue I'm running into and easy to re-create.  Basically, Mybatis-Spring requires one to define a Spring bean for the <code>SqlSessionFactoryBean</code> class.  This class implements Spring's <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/FactoryBean.html" rel="nofollow">FactoryBean</a> interface.  To wire it up properly, one should supply it, among other things, a <a href="http://docs.oracle.com/javase/6/docs/api/javax/sql/DataSource.html" rel="nofollow">DataSource</a>.  I reference the JDBC connection details elsewhere in code (for Mybatis-Generator purposes) and would like to keep those details consolidated to one place: property file or something else.</p>
  
  <p>For now, I have a property file, and everything built pretty standard from a Spring point-of-view.  I start seeing issues when trying to boot up the Jetty server though, Mybatis-Spring seems to catch itself in a circular dependency.</p>
  
  <p>I cannot say I quite know exactly what causes this circular reference, but I do know the point of failure: I set the JDBC driver to <code>null</code> when trying to call (from an autowired <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/env/Environment.html" rel="nofollow">Environment</a> <code>env.getRequiredProperty("jdbc.driver")</code>.  It's an NPE (on the <code>env</code> object); the property later gets initialized as I print it out in a PostConstructor.</p>
  
  <p>My understanding of this issue is that since the <code>SqlSessionFactoryBean</code> is a Spring factory bean (does this make it of BFPP type?), that normal autowiring and bean instantiation cannot be assumed to be available.  What further solidifies this point is that if I hard-code the JDBC details (or otherwise make the <code>SqlSessionFactoryBean</code> method static), then Jetty is able to start-up just fine and the application works as expected.  Obviously hard-coding in the JDBC details is not desired here -- I reference these details in one other spot.</p>
  
  <p><strong>tl;dr</strong><br>
  How can I statically access Spring's environment property values, say, in a Factory Bean method?  I have tried an @autowired+postconstruct route and a implement-application-context-aware approach; both to no avail.</p>
  
  <p><strong>code samples</strong></p>
  
  <pre><code>public abstract class AbstractMybatisConfig {
      @Autowired
      Environment env;
  
      String jdbcDriver;
      String jdbcUrl;
      String jdbcUsername;
      String jdbcPassword;
  
      // if I try the below statement, the env object throws an NPE
      // jdbcDriver = env.getRequiredProperty( "jdbc.driver" );
  
      @PostConstruct
      protected void postConstruct() {
          jdbcDriver = env.getRequiredProperty( "jdbc.driver" );
          // this statement prints out "com.mysql.jdbc.Driver" as expected
          System.out.println( jdbcDriver );
          jdbcUrl = env.getRequiredProperty( "jdbc.url" );
          jdbcUsername = env.getRequiredProperty( "jdbc.username" );
          jdbcPassword = env.getRequiredProperty( "jdbc.password" );
      }
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName( jdbcDriver );
          // this statement prints out null
          System.out.println( jdbcDriver );
          dataSource.setUrl( jdbcUrl );
          dataSource.setUsername( jdbcUsername );
          dataSource.setPassword( jdbcPassword );
          return dataSource;
      }
  
      @Bean  // circular reference happens here b/c dataSource() sets null properties
      public SqlSessionFactoryBean sqlSessionFactory() {
          SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
          sqlSessionFactory.setDataSource( dataSource() );
          // ...other (unrelated) configuration goes here...
          return sqlSessionFactory;
      }
  
      private static getJdbcProperties() {
          Properties jdbcProps = new Properties();
          // when this method is invoked, Spring complains the resource does not exist
          // (and it is the same String used to identify this property file in my PropertySources annotation found in another config)
          jdbcProps.load( new ClassPathResource( "classpath:META-INF/properties/mybatis.properties" ).getInputStream() );
          // ...code to grab property values omitted for brevity...
          return jdbcProps
      }
  </code></pre>