How to programatically add an @Bean definition to Spring context?

java, spring

Solution

You should autowire the bean factory, and use a `@PostConstruct` to register your bean. That way, you guarantee that all dependencies has been injected (the bean factory is injected by the container, no setup is needed).

@Service
public class MyPortRegistrar {

    @Autowired
    private ConfigurableBeanFactory beanFactory;

    @Autowired
    private SpringBus bus;

    @PostConstruct
    public void createPort() {
        Port port = new WebservicePort(bus);
        beanFactory.registerSingleton("myDynamicPortName", port);
    }
}

Problem

Normally I'm adding my objects to spring context using `@Bean` definition: ``` @Autowired private SpringBus bus; //register a singleton @Bean public WebservicePort getPort() { //new port() //initialize //configure //return port; } ``` But now I need deeper control of the process, especially I want to create the bean name dynamically under which the bean is registered. I tried: ``` @Service public class MyPortRegistrar implements BeanDefinitionRegistryPostProcessor { @Autowired private SpringBus bus; @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println(bus); //prints null //create and configure port with the SpringBus Port port = new WebservicePort(bus); // -> throws NullPointerException beanFactory.autowireBean(port); beanFactory.initializeBean(port, "myDynamicPortName"); } } ``` But this throws an NPE as the autowired dependecies are not yet initialized here! So, how can I add those beans programatically?

Original source