Instantiate beans with Spring without @Autowired

Are you in a situation where @Autowired will not work? for example, in a static class or a class that is istantiated by another framework (hibernate) and so on. The latter case is my case: I had to autowire a bean in a Sequence Generator (to create ID for my classes), but since the class was instantiated by Hibernate, Spring could not access and autowire it, even if it was declared as a bean.

However, there is a chance to get what you want. In my example we will try to autowire “myBean” object. Please ensure that your bean is declared somewhere in your applicationContext.xml 😉

  1. First of all, the class that can’t be Spring-ified must implement ApplicationContextAware interface :
public class SequenceTableIdGenerator implements IdentifierGenerator, 
    ApplicationContextAware { 

private static ApplicationContext applicationContext;

private MyBean myBean; 

...

}
  1. you must override the method setApplicationContext that comes with the above interface :
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
}
  1. Finally, you can autowire your beans in a constructor or in any other method:
if (myBean == null) {
    myBean = (MyBean) applicationContext.getBean("myBean");
}

That’s it! This method is not standard but it’s very useful. Every time I needed it I had to go searching the web. Now this useful piece of info is my website 🙂

Related Posts: