Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically adding Beans to Spring application Context

I am trying to add a simple String to my Spring Application Context, and then autowire this to a different existing bean (A) within the application context. I know this is not the usual way to go, yet I need to add many beans programmatically, which would otherwise make my xml configuration huge.

public class MyPostProcessor implements BeanFactoryPostProcessor, Ordered {

  @Override
  public int getOrder() {
    return 0;
  }

  @Override
  public void postProcessBeanFactory(
        ConfigurableListableBeanFactory beanFactory) throws BeansException {
    beanFactory.registerSingleton("myString", "this is the String");
    A a = beanFactory.getBean(A.class);
    beanFactory.autowireBean(a);
  }
}    

public class A {

    @Autowired 
    public transient String message;

}

When running this, the property message of the instance of A is null. What am I missing?

EDIT: this is my application context:

@Configuration
class TestConfig {

  @Bean 
  public A a() {
    return new A();
  }

  @Bean
  public MyPostProcessor postProcessor() {
    return new MyPostProcessor();
  }

}

And this is my test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
public class MyTest {

  @Autowired 
  private transient A a;

  @Test
  public void test() throws Exception {
    System.err.println("Running");
    System.err.println("This is the autowired String: " + a.message);
    Thread.sleep(1000);
  }

}

Thanks

like image 333
user152468 Avatar asked Nov 09 '22 15:11

user152468


1 Answers

You should not instantiate beans from BeanFactoryPostprocessors. From BeanFactoryPostProcessor JavaDoc:

A BeanFactoryPostProcessor may interact with and modify bean definitions, but never bean instances. Doing so may cause premature bean instantiation, violating the container and causing unintended side-effects.

In your case, the A bean is instantiated before BeanPostProcessors and therefore not autowired.

Remove the lines:

A a = beanFactory.getBean(A.class);
beanFactory.autowireBean(a);

And will work.

like image 69
Jose Luis Martin Avatar answered Nov 14 '22 22:11

Jose Luis Martin