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?