Spring IOC Container
An IoC container is a common characteristic of frameworks that implement IoC. See IOC Containers
IoC in Spring Framework
In the spring framework, inversion of control refers to the framework’s ability to create, destroy, and manage the lifecycle of beans.
Spring ApplicationContext
- The
ApplicationContext
(internally, it usesBeanFactory
) is in the center of IoC in Spring. - In the Spring framework, the interface
ApplicationContext
represents the IoC container. - The Spring container is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their life cycles.
- The Spring framework provides several implementations of the
ApplicationContext
interface:AnnotationConfigApplicationContext
,ClassPathXmlApplicationContext
andFileSystemXmlApplicationContext
for standalone applications, andWebApplicationContext
for web applications. - To assemble beans, the spring bean factory uses configuration metadata, which can be in the form of XML configuration or annotations.
- When we create an instance of
AnnotationConfigApplicationContext
and provide it with one or more configuration classes, it scans these classes for the@Bean
annotations and other relevant annotations. It then initializes and manages the beans defined in these classes, setting up their dependencies and managing their lifecycle.
Spring Bean Factory
Difference between BeanFactory and ApplicationContext?
A BeanFactory
is like a factory class that contains a collection of beans. The BeanFactory
holds bean definitions within itself and then instantiates a bean whenever asked for by clients.
BeanFactory
is able to create associations between collaborating beans as they are instantiated. BeanFactory
also takes part in the life cycle of the beans, making calls to custom initialization and destruction methods.

At a high level, an application context is the same as a bean factory. Both load bean definitions, wire beans together, and dispense beans upon request. But the application context also provides:
- A means for resolving text messages, including support for internationalization.
- A generic way to load file resources.
- Events to beans that are registered as listeners.
The three commonly used implementations of ApplicationContext are:
-
ClassPathXmlApplicationContext
: loads context definition from an XML file located in the classpath, treating context definitions as classpath resources. The application context is loaded from the application’s classpath by using the code.ApplicationContext context = new ClassPathXmlApplicationContext(“bean.xml”);
-
FileSystemXmlApplicationContext
: loads context definition from an XML file in the filesystem. The application context is loaded from the file system by using the code.ApplicationContext context = new FileSystemXmlApplicationContext(“bean.xml”);
-
XmlWebApplicationContext
: loads context definition from an XML file contained within a web application.
Implementation
External references
- https://docs.spring.io/spring-framework/reference/core/beans/java/instantiating-container.html
- https://blog.devgenius.io/why-do-spring-and-idea-not-recommend-autowired-annotation-7273691a3079
- https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring