Spring Bean Creation: BeanDefinition vs FactoryBean Explained
The article compares Spring's BeanDefinition and FactoryBean mechanisms, detailing their roles, how they are defined via XML, annotations or interfaces, the use of BeanDefinition.setInstanceSupplier() for flexible instantiation, and provides concrete code examples for each approach.
In the Spring framework, BeanDefinition and FactoryBean are two core mechanisms for creating and managing beans.
BeanDefinition creates Bean
BeanDefinition is an object that describes a bean's properties such as class name, scope, lazy initialization, and dependencies. It serves as a blueprint for the Spring container to instantiate, configure, and assemble a bean.
Purpose : describe bean attributes and configuration; act as the container's creation blueprint.
Creation method : typically defined through XML files or annotations; the container parses these definitions at startup and creates corresponding BeanDefinition objects.
setInstanceSupplier() : a method called on a BeanDefinition to set a custom Supplier that provides the bean instance, allowing programmatic control over instantiation logic. This is useful when the creation process needs to select constructors or arguments based on runtime conditions.
FactoryBean creates Bean
FactoryBean is an interface that can be implemented to create beans with complex instantiation logic. It offers a way to customize bean creation beyond what standard configuration provides.
Purpose : customize bean instantiation, handle complex dependencies, initialization logic, post‑processing, and can act as a proxy to support AOP.
Creation method : implement the FactoryBean interface and override getObject() and getObjectType(). Register the implementation as a regular bean in the Spring container.
Use Cases and Scenarios
BeanDefinition.setInstanceSupplier() is suitable when flexible control over the bean creation process is required, such as choosing different constructors or parameters based on specific conditions.
FactoryBean is appropriate for creating complex bean instances or beans that need additional processing, for example creating proxy objects for AOP, reading configuration files, or establishing network connections during getObject().
Code Example: BeanDefinition with setInstanceSupplier()
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(Test.class);
TestSupplier testSupplier = new TestSupplier();
definition.setInstanceSupplier(testSupplier::getuser);
ctx.registerBeanDefinition("b", definition);
ctx.refresh();
Book b = ctx.getBean("b", Test.class);
System.out.println("b = " + b);Code Example: FactoryBean Implementation
public class MyFactoryBean implements FactoryBean<MyBean> {
@Override
public MyBean getObject() throws Exception {
// custom instantiation logic
return new MyBean("custom");
}
@Override
public Class<?> getObjectType() {
return MyBean.class;
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer1970
Formerly called 'Code to 35'. Add our main WeChat ID to access a wealth of shared resources (algorithms, interview prep, tech stacks: Java, Python, Go, big data). We mainly share serious development techniques, focusing on output-driven input. Occasionally we post life snippets and gossip. Our aim is to attract precise traffic and test advertising opportunities.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
