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

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html

  1. The ApplicationContext (internally, it uses BeanFactory) is in the center of IoC in Spring.
  2. In the Spring framework, the interface ApplicationContext represents the IoC container.
  3. The Spring container is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their life cycles.
  4. The Spring framework provides several implementations of the ApplicationContext interface: AnnotationConfigApplicationContext, ClassPathXmlApplicationContext and FileSystemXmlApplicationContext for standalone applications, and WebApplicationContext for web applications.
  5. To assemble beans, the spring bean factory uses configuration metadata, which can be in the form of XML configuration or annotations.
  6. 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:

  1. A means for resolving text messages, including support for internationalization.
  2. A generic way to load file resources.
  3. Events to beans that are registered as listeners.

The three commonly used implementations of ApplicationContext are:

  1. 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”);
    
  2. 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”);
    
  3. XmlWebApplicationContext: loads context definition from an XML file contained within a web application.

Implementation

https://github.com/explorer436/programming-playground/tree/main/java-playground/spring-ioc-container-poc

External references

  1. https://docs.spring.io/spring-framework/reference/core/beans/java/instantiating-container.html
  2. https://blog.devgenius.io/why-do-spring-and-idea-not-recommend-autowired-annotation-7273691a3079
  3. https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring

KNOWLEDGE GAP - LEARN MORE, IMPLEMENT THIS

  1. https://medium.com/@ilyailin7777/dependency-injection-keep-it-easy-3d0152b691e1
  2. https://howtodoinjava.com/spring-core/how-to-create-beans-using-spring-factorybean/

Links to this note