Application Context Interface

IoC (Inversion of Control) Container

IoC stands for Inversion of Control, which means the control of object creation and management is reversed. Normally, when we write code, we manually create and manage classes ourselves. However, with an IoC container, Spring takes care of this management. It gives our code more freedom because Spring manages the required objects or classes (known as “beans”) and injects them as needed.


Application Context

The Application Context is the main container in Spring, which acts as the IoC container. It’s the core infrastructure of Spring that creates, initializes, configures, and manages beans. When we run a Spring application, the Application Context is the framework that registers and manages our beans.

  • The interfaces BeanFactory and ApplicationContext represent the Spring IoC container. Here, BeanFactory is the root interface for accessing the Spring container. It provides basic functionalities for managing beans.
  • On the other hand, the ApplicationContext is a sub-interface of the BeanFactory. Therefore, it offers all the functionalities of BeanFactory.


Bean

In Spring, a bean is simply an object that is managed by the IoC container. Beans are actually specific class objects that Spring stores in the container and provides to us as needed. You can define beans using a configuration file or by using annotations.


How Does “Get Bean” Work?

The Spring Application Context provides a method to get a specific bean. We use the getBean() method to retrieve it.


Example Code:

Here’s a small example showing how Spring Application Context and beans work:

@SpringBootApplication:

  • @SpringBootApplication: This is a key annotation that sets up a Spring Boot application, handling most of the configurations automatically.
  • @Import({BeanConfig.class}): This annotation imports the BeanConfig class, which contains our bean configurations, so we can use them in this main application class.

Main Method:

  • public static void main(String[] args): This is the main method, where the program starts.
  • SpringApplication.run(...): This line starts the Spring Boot application and returns an ApplicationContext.
  • ApplicationContext Context: This ApplicationContext is the Spring container, which manages all our beans (objects).

Using the getbean() Method:


  • context.getBean("student1"): Here, we are retrieving a bean named student1 as a Student object. Using getBean() method, we can access student1 and student2 beans and display their information.
  • System.out.println(...): This prints the name of each student (student1and student2) and their department names.

Summary:

In this example, we learn how to use beans with the Spring container (ApplicationContext). Key concepts are:

  • ApplicationContext: The Spring container that manages all the beans in the application.
  • BeanConfig: Our bean configuration file.
  • getBean(): A method to retrieve specific bean objects from the container.





Comments