Home » DefaultListableBeanFactory

DefaultListableBeanFactory

  • by
DefaultListableBeanFactory

1. Overview

In this article, we will discuss the DefaultListableBeanFactory with example.

2. Spring BeanFactory

The BeanFactory is the actual Spring IOC container that instantiates, configures, and manages the Spring beans. These beans typically collaborate with one another and thus have dependencies between themselves.

You had to define these dependencies in the configuration data used by the BeanFactory such as XML configuration. To know more about Bean instantiation, refer to this article to know more

The BeanFactory provides an advanced configuration mechanism capable of managing beans (objects) of any nature and stores the beans likely in any kind of storage facility. 

2.1. DefaultListableBeanFactory implementation

The org.springframework.beans.factory.BeanFactory is the root interface for the Spring IOC container for which there are multiple implementations.

The most commonly used simple BeanFactory implementation org.springframework.beans.factory.xml.XmlBeanFactory deprecated as of Spring 3.1 in favor of DefaultListableBeanFactory and XmlBeanDefinitionReader.

2.2. DefaultListableBeanFactory dependencies

You must include the following dependencies to your Spring application to use the BeanFactory:

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-beans</artifactId>
	<version>5.3.17</version>
</dependency>

2.3. Spring DefaultListableBeanFactory Instantiation

You can instantiate the BeanFactory explicitly:

The FileSystemResource return the absolute resource path and look for the resource in the file system such as C:/.../Core/IOC/beanfactory/src/main/resources/package/config. xml.

public class BeanfactoryApplication {
	public static void main(String[] args) {
		DefaultListableBeanFactory beanDefinitionRegistry = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
		reader.loadBeanDefinitions(new FileSystemResource("//Core//IOC//beanfactory//src//main//resources//config.xml"));
		SampleBean sampleBean = beanDefinitionRegistry.getBean(SampleBean.class);
		System.out.println(sampleBean.getValue() + "");
	}
}

The ClassPathResource looks for the resource on the classpath files inside /WEB-INF/classes.

public class BeanfactoryApplication {
	public static void main(String[] args) {
		DefaultListableBeanFactory beanDefinitionRegistry = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
		reader.loadBeanDefinitions(new ClassPathResource("config.xml"));
		SampleBean sampleBean = beanDefinitionRegistry.getBean(SampleBean.class);
		System.out.println(sampleBean.getValue() + "");
	}
}

Note that we have used the getBean method of the BeanFactory implementation to access the beans. The BeanFactory uses lazy initialization whereas ApplicationContext uses eager initialization. You can refer to this article to see the differences between ApplicationContext vs BeanFactory.

The BeanFactory creates the beans when you invoke or use the beans such as getBeans() method. You can refer to this article to understand more about BeanFactory lazy loading.

3. DefaultListableBeanFactory Bean Definitions

A BeanFactory configuration comprises, at its most basic level, definitions of one or more beans that the BeanFactory must manage.

<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-2.5.xsd">
    <bean id="sampleBean" class="com.tedblob.beanfactory.models.SampleBean">
        <constructor-arg value="1000" />
    </bean>
</beans>

These bean definitions provide information on how to create a specific bean for the BeanFactory. However, you can also allow to register existing bean objects that are created outside the factory (by custom code).

DefaultListableBeanFactory supports this through the registerSingleton method, as defined by the org.springframework.beans.factory.config.ConfigurableBeanFactory interface.

For example, the following code registers the CustomBean object using the registerSingleton method.

public class BeanfactoryApplication {
	public static void main(String[] args) {
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
		reader.loadBeanDefinitions(new ClassPathResource("config.xml"));
		beanFactory.registerSingleton("customBean", new CustomBean("custom bean"));
		CustomBean customBean = beanFactory.getBean(CustomBean.class);
		System.out.println(customBean.getValue() + "");
                //Destroy a singleton bean from context
                beanFactory.destroySingleton(customBean);
           }
}

You can destroy the singleton bean by calling the destroySingleton method on the BeanFactory.

4. Conclusion

To sum up, we have learned the Spring DefaultListableBeanFactory interface. You can find code samples of this article in our GitHub repository.