title: Spring Wire a Static Class
body: <p>I'm dealing with a legacy code base where a class which is not wired up in spring needs to obtain a class that <em>is</em> wired up in spring.  I was hoping to create a factory class that was wired up on startup and then I could just call the getInstance() method to obtain a wired up object.  What is the best way to go about this?</p>
  
  <p>Example:</p>
  
  <pre><code>public class LegacyA {
      public void doSomething() {
          ...
          Foo foo = FooFactory.getInstance();
          ...
      }
  }
  
  public class FooFactory {
      private static Foo foo;
  
      public static Foo getInstance() {
          if (foo == null) throw new IllegalStateException();
          return foo;
      }
  }
  </code></pre>
  
  <p>I need FooFactory to be wired up on startup so that LegacyA can simply call getInstance() so that it returns an instance of Foo (which is also a bean defined in the application context).</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean id="legacyA" class="LegacyA"/&gt;
  
  &lt;bean id="foo" class="Foo"/&gt;
  
  &lt;!-- I need this bean to be injected with foo so that the FooFactory can return a foo --&gt;
  &lt;bean id="fooFactory" class="FooFactory"/&gt;
  </code></pre>
  
  <p>Edit: I had to re-work my example a bit as I got it a bit confuzzled in my own head...</p>