title: Spring Dependency Injection into serializable beans
body: <p>I have a service class which is not serializable and a bean which must be serializable but must have access to this service class:</p>
  
  <pre><code>class SomeBean implements Serializable
  {
      private StuffFactory factory;
  
      @Autowired
      public SomeBean(StuffFactory factory)
      {
          this.factory = factory;
      }
  
      public getOther()
      {
          return this.factory.getSomeOtherStuff();
      }
  }
  </code></pre>
  
  <p>This obviously doesn't work because now the <code>SomeBean</code> class is no longer serializable. What is the correct way to solve this in Spring? When I make the <code>factory</code> field transient then I loose the injected factory instance on deserialization, or not? And when I make the <code>StuffFactory</code> also serializable then this class will no longer be a singleton because each <code>SomeBean</code> instance will have it's own factory after deserialization then.</p>