Difference between Spring ApplicationContext and BeanFactory
This tutorial explains the difference between ApplicationContext and BeanFactory interfaces. On high level, BeanFactory is the root interface used for accessing Spring bean container and ApplicationContext is the central interface used to provide configuration for an application. ApplicationContext interface is the subinterface to BeanFactory and both interfaces provide a way to get a bean from Spring IoC container. So what is the difference ?
Difference between Spring ApplicationContext and BeanFactory
Note, the packages org.springframework.beans and org.springframework.context provides the basis for Spring Framework’s IoC container.
The BeanFactory is the actual representation of Spring IoC container and provides an mechanism to manage beans/ business objects/ POJOs. The ApplicationContext extends BeanFactory and it provides support for other functionalities like easier integration with AOP, message resource handling for internationalization, event propagation, and application layer specific contexts such as WebApplicationContext.
We can say ApplicationContext is a superset of the BeanFactory and below matrix lists features that are supported by these interfaces and the difference.
Table 1
Feature | BeanFactory | ApplicationContext |
---|---|---|
Bean instantiation/wiring | Yes | Yes |
Automatic BeanPostProcessor registration | No | Yes |
Automatic BeanFactoryPostProcessor registration | No | Yes |
Convenient MessageSource access (for i18n) | No | Yes |
ApplicationEvent publication | No | Yes |
The below code example shows how to get bean using both interfaces.
Spring Bean/ POJO
Let’s write simple bean HelloWorld.java. Spring bean is the object that form the backbone of application.
package com.sneppets.common; /** * Spring Bean - The object that form the backbone of application and that are managed by the Spring IoC container are called beans * * @author sneppets * */ public class HelloWorld { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void printHello(){ System.out.println("Hello! " + name); } }
Maven Dependencies
Let’s add the required maven dependencies in the pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.3.RELEASE</version> <scope>provided</scope> </dependency>
Configuring Spring Bean with XML
Let’s configure now the HelloWorld bean in XML (spring-module.xml)
<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-4.3.xsd"> <bean id="helloBean" class="com.sneppets.common.HelloWorld"> <property name="name" value="Krish" /> </bean> <alias name="helloBean" alias="helloBeanAlias"/> </beans>
ApplicationContext Vs BeanFactory to get Beans
The below code sample shows how to get bean from Spring IoC container using both interfaces.
package com.sneppets.common; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; /** * Hello world! * */ public class App { public static void main(String[] args) { System.out.println("ApplicationContext"); //create and configure beans ApplicationContext context = (ApplicationContext) new ClassPathXmlApplicationContext( "spring-module.xml"); //retrieve configured instance using ApplicationContext HelloWorld helloObj = (HelloWorld) context.getBean("helloBean"); //use configured instance helloObj.printHello(); System.out.println("BeanFactory"); Resource res = new ClassPathResource("spring-module.xml"); //BeanFactory with ClassPathResource - get bean using BeanFactory BeanFactory factory = new XmlBeanFactory(res); HelloWorld hObj = (HelloWorld) factory.getBean("helloBean"); hObj.printHello(); } }
Output
ApplicationContext Hello! Krish BeanFactory Hello! Krish
So based on the differences listed in the Table 1, you need to choose which interface to use. But in the above example only the implementation about first point (Bean instantiation/wiring) is shown.
Further Reading
- Where the Docker images are stored locally?
- Check if Element exists in a Collection using Lambda Expressions
- Python ImportError: No module named flask
- Terraform tutorial