Wednesday 19 May 2010

Spring: Inject My Bean

A few weeks ago I was wondering how you get Spring to inject an existing bean that I'd instantiated myself.

I'm trying to integrate our JUnit concurrent test runner with Spring's SpringJUnit4ClassRunner, and while plumbing the depths of the relevant spring code, I came across this:

public void inject(ApplicationContext context, String beanName, Object bean, int autowiringMode) {
   AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
   factory.autowireBeanProperties(bean, autowiringMode, false);
   factory.initializeBean(bean, "bean name");

inject(bean, "myBean", appContext, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);


No comments:

Post a Comment