In the Spring framework, components that form the backbone of your application and are managed by the Spring IoC container are referred to as beans. Essentially, a bean is an object that is instantiated, configured, and managed by the Spring container. Beyond this container management, beans behave just like any other object in your application. The definitions of beans and their interdependencies are described through configuration metadata.
The BeanFactory serves as the actual implementation of the Spring IoC container, responsible for holding and managing the beans mentioned above.
In Spring, BeanFactory is the core interface of the IoC container. Its responsibilities include instantiating, locating, configuring objects within the application, and establishing dependencies among these objects.
Spring offers two primary types of containers:
- BeanFactory: The most basic Spring container, providing fundamental support for Dependency Injection (DI).
- ApplicationContext: A more comprehensive container that includes all features of BeanFactory plus additional enterprise-specific functionalities.
Spring provides several implementations of BeanFactory, with DefaultListableBeanFactory being the core component for bean loading and registration, serving as Spring's default implementation for these tasks.
DefaultListableBeanFactory Class Hierarchy
The class hierarchy for DefaultListableBeanFactory includes several key interfaces and classes:
- AliasRegistry: Defines basic operations for aliases (add, remove, update)
- SimpleAliasRegistry: Implements AliasRegistry using a map for alias caching
- SingletonBeanRegistry: Deefines registration and retrieval of singleton beans
- BeanFactory: Defines methods for obtaining beans and their properties
- DefaultSingletonBeanRegistry: Implements SingletonBeanRegistry functions
- HierarchicalBeanFactory: Extends BeanFactory with parentFactory support
- BeanDefinitionRegistry: Defines CRUD operations for BeanDefinition
- FactoryBeanRegistrySupport: Adds special handling for FactoryBean in DefaultSingletonBeanRegistry
- ConfigurableBeanFactory: Provides configuration methods for Factory
- ListableBeanFactory: Allows retrieval of bean configuration lists based on various criteria
- AbstractBeanFactory: Combines FactoryBeanRegistrySupport and ConfigurationBeanFactory
- AutowireCapableBeanFactory: Provides methods for creating beans, autowiring, initialization, and applying bean post processors
- AbstractAutowireCapableBeanFactory: Combines AbstractBeanFactory and implements AutowireCapableBeanFactory
- ConfigurableListableBeanFactory: BeanFactory configuration list with support for ignoring types and interfaces
Practical Example Using DefaultListableBeanFactory
Let's create a practical example demonstrating how to use DefaultListableBeanFactory:
Step 1: Create Maven Project with Spring Dependencies
First, set up a Maven project and include the necessary Spring dependencies in you're pom.xml.
Step 2: Define XML Configurasion
Create an XML configuration file named applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sampleService" class="com.example.SampleService"/>
</beans>
Step 3: Implement the Service Class
Create a SampleService class with properties and methods:
package com.example;
public class SampleService {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void displayMessage() {
System.out.println("Service message: " + message);
}
}
Step 4: Implement the Main Class
Create a main class to demonstrate bean factory usage:
package com.example;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class BeanFactoryDemo {
public static void main(String[] args) {
// 1. Load the XML configuration file
Resource configResource = new ClassPathResource("applicationContext.xml");
// 2. Create the bean factory
BeanFactory container = new DefaultListableBeanFactory();
// 3. Create a definition reader and load bean definitions
BeanDefinitionReader definitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) container);
definitionReader.loadBeanDefinitions(configResource);
// 4. Retrieve the bean from the container
SampleService service = (SampleService) container.getBean("sampleService");
// 5. Use the bean
service.setMessage("Hello from Spring BeanFactory!");
service.displayMessage();
}
}
Step 5: Run the Application
Executing the main method will produce the output:
Service message: Hello from Spring BeanFactory!
This output confirms that the bean was successfully retrieved from the container and used properly.
Beyond DefaultListableBeanFactory, Spring provides other container implementations such as ClassPathXmlApplicationContext, AnnotationConfigApplicationContext, AnnotationConfigWebApplicationContext, and FileSystemApplicationContext, each suited for different use cases.