![]() |
VOOZH | about |
Spring BeanFactory is the simplest container in the Spring framework that manages the creation and lifecycle of beans using dependency injection. It loads bean definitions and their dependencies at runtime based on configuration metadata. It acts as the basic IoC container and is the parent of ApplicationContext.
Hierarchy of spring container and its implementation showing the relationship between BeanFactory , ApplicationContext and various XML and Annotation-based context classes.
Let us first go through some of the methods of Bean Factory before landing up on implementation which are shown below in tabular format:
Method | Description |
|---|---|
| containsBean(String name) | Does this bean factory contain a bean definition or externally registered singleton instance with the given name? |
| getAliases(String name) | Return the aliases for the given bean name, if any. |
| getBean(Class<T> requiredType) | Return the bean instance that uniquely matches the given object type, if any. |
| getBean(Class<T> requiredType, Object... args) | Return an instance, which may be shared or independent, of the specified bean. |
| getBean(String name) | Return an instance, which may be shared or independent, of the specified bean. |
| getBean(String name, Class<T> requiredType) | Return an instance, which may be shared or independent, of the specified bean. |
| getBean(String name, Object... args) | Return an instance, which may be shared or independent, of the specified bean. |
| getBeanProvider(Class<T> requiredType) | Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. |
| getBeanProvider(ResolvableType requiredType) | Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options. |
| getType(String name) | Determine the type of the bean with the given name. |
| getType(String name, boolean allowFactoryBeanInit) | Determine the type of the bean with the given name. |
| isPrototype(String name) | Is this bean a prototype? That is, will getBean(java.lang.String) always return independent instances? |
| isSingleton(String name) | Is this bean a shared singleton? That is, will getBean(java.lang.String) always return the same instance? |
| isTypeMatch(String name, Class<?> typeToMatch) | Check whether the bean with the given name matches the specified type. |
| isTypeMatch(String name, ResolvableType typeToMatch) | Check whether the bean with the given name matches the specified type. |
After creating all packages and classes, the project structure will look like below:
Follow these steps to configure and initialize the Spring BeanFactory in your application.
Defines the Student class as a POJO that will be managed as a Spring bean.
Student.java:
Configures the Student bean in the XML file so the BeanFactory knows how to create it.
XML Bean Configuration:
Loads the BeanFactory and retrieves the Student bean from the container.
Explanation: The console output shows that the Student bean was successfully created by the BeanFactory and its values (name = Tina, age = 21) were correctly retrieved and displayed.
Note: XmlBeanFactory class is deprecated.
Let's understand the above code with visuals:
👁 Diagram to understand the flowThe program flow is something like this:
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Definition | Basic container for managing beans | Advanced container built on top of BeanFactory |
| Interface Type | Root interface | Sub-interface of BeanFactory |
| Bean Initialization | Lazy loading (beans created when requested) | Eager loading (beans created at startup by default) |
| Performance | Faster startup, slower at runtime | Slower startup, better runtime performance |
| Dependency Injection | Supported | Supported |
| Bean Scope Support | Basic scopes | Supports all scopes + web scopes |
| Event Handling | Not supported | Supports event publishing & listening |
| Internationalization (i18n) | Not supported | Supported |
| Annotation Support | Limited | Full support (e.g., @Component, @Autowired) |
| AOP Integration | Limited | Full support |
| Resource Handling | Basic | Advanced resource handling |
| Use Case | Lightweight apps, memory-sensitive systems | Enterprise applications |
| Common Implementations | XmlBeanFactory (Deprecated) | ClassPathXmlApplicationContext, AnnotationConfigApplicationContext, WebApplicationContext |
| Recommended Usage | Rarely used now | Widely used in modern Spring apps |