Home » Spring BeanFactory

Spring BeanFactory

  • by
Spring BeanFactory

1. Overview

In this article, we will learn about the Spring BeanFactory.

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. Spring BeanFactory implementations

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. Spring BeanFactory 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 BeanFactory 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 ClassPathXmlApplicationContext is an ApplicationContext that loads the definitions from the provided XML file and automatically refreshes the context.

public class BeanfactoryApplication {
	public static void main(String[] args) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("config.xml");
                // of course, an ApplicationContext is just a BeanFactory
		SampleBean sampleBean = beanFactory.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. BeanFactory vs ApplicationContext features

FeatureBeanFactoryApplicationContext
Bean instantiation/wiringYesYes
Automatic BeanPostProcessor registrationNoYes
Automatic BeanFactoryPostProcessor registrationNoYes
Convenient MessageSource access (for i18n)NoYes
ApplicationEvent publicationNoYes
  1. The BeanFactory does not register these BeanFactoryPostProcessor and BeanPostProcessor interfaces automatically.
  2. BeanFactory performs bean instantiation and auto wiring/injection of bean dependencies.
  3. Lightweight compared to ApplicationContext.
  4. The BeanFactory does not support annotation-based dependency injection.

4. Conclusion

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