-
Notifications
You must be signed in to change notification settings - Fork 1
BeanProvider
The BeanProvider interface provides a hook for implementing classes to provide bean definitions which will be registered with the InfinitumContext. BeanProvider exposes a single method, getBeans, which simply returns a list of bean definitions that should be registered by the framework.
The example below shows how BeanProvider can be implemented to programmatically register beans at framework initialization. This particular example creates two beans, fooBean and barBean.
@Component
public class MyBeanProvider implements BeanProvider {
@Override
public List<AbstractBeanDefinition> getBeans(BeanDefinitionBuilder beanDefinitionBuilder) {
List<AbstractBeanDefinition> beans = new ArrayList<AbstractBeanDefinition>();
beans.add(beanDefinitionBuilder
.setName("fooBean")
.setType(FooBean.class)
.build());
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("propertyA", true);
properties.put("propertyB", 42);
beans.add(beanDefinitionBuilder
.setName("barBean")
.setType(BarBean.class)
.setProperties(properties)
.build());
return beans;
}
}If you are not using component scanning, you can register this BeanProvider in the infinitum.cfg.xml:
<bean id="myBeanProvider"
class="com.foo.bar.MyBeanProvider" />Both BeanFactoryPostProcessors and BeanProviders can be used to programmatically register beans with the BeanFactory during framework initialization. BeanFactoryPostProcessor directly exposes the BeanFactory, while BeanProvider can be implemented to return a list of beans which the framework will register.
Both can be used to register beans, but there is a key distinction in how these two hooks work. As its name implies, a BeanFactoryPostProcessor is executed after the BeanFactory has been initialized and beans have been registered. What's less obvious is that BeanFactoryPostProcessors are executed after BeanPostProcessors, meaning any beans registered through a BeanFactoryPostProcessor will not undergo bean post processing. This has an important implication in that autowired dependency injections are identified during this step, so beans registered through a BeanFactoryPostProcessor are not eligible for autowiring.
BeanProviders, on the other hand, are used to programmatically define beans for registration which will undergo post processing. BeanProvider beans are registered before post processors are executed.