title: Accessing Spring Beans inside AttributeConverter class
body: <p>I'm developing a Spring Data JPA application, and I've created an <code>AttributeConverter</code> class in order to save an <code>ArrayList</code> of objects as JSON in a database column. Inside this class I need to make use of a class I have defined as a Spring Bean.</p>
  
  <p>As the <code>AttributeConverter</code> class is managed by Hibernate, it seems to be instantiated before any Spring beans are created, and therefore DI does not seem to work (the Spring Bean in the <code>AttributeConverter</code> class is <code>null</code>, and I'm getting a <code>NullPointer</code> exception thrown). So at the moment I'm creating another instance of the said bean to be able to use it in the <code>AttributeConverter</code> class (which defeats the purpose of DI).</p>
  
  <p>I've also tried creating a Util class (annotated with <code>@Component</code>) which implements <code>ApplicationContextAware</code>, which provides a method giving the SpringBean (<code>cxt.getBean(BeanClass.class)</code>). But this also is instantiated <b>after</b> the <code>AttributeConverter</code>.</p>
  
  <p>Is there any idea of how this can be solved?</p>
  
  <p>Thank you.</p>