Spring IOC Container Overview


In this article, we will discuss what is Spring IOC container, How it works, how to create Spring IOC container, how to retrieve beans from Spring IOC container with examples.

What We'll Learn?

  • What Is the Spring Container?
  • What is Configuration Metadata?
  • How to Create a Spring Container?
  • How to Retrieve Bean from Spring Container?
  • Spring IOC Container XML Config Example
  • Spring IOC Container Java Config Example

What is the Spring Container?

The Spring container is responsible for instantiating, configuring, and assembling the Spring beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express the objects that compose your application and the rich inter-dependencies between those objects.
The responsibilities of IOC container are:
  • Instantiating the bean
  • Wiring the beans together
  • Configuring the beans
  • Managing the bean’s entire life-cycle
The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. Spring framework provides two distinct types of containers.
  1. BeanFactory container
  2. ApplicationContext container
BeanFactory is the root interface of Spring IOC container. ApplicationContext is the child interface of BeanFactory interface that provides Spring AOP features, i18n etc.
One main difference between BeanFactory and ApplicationContext is that BeanFactory only instantiates bean when we call getBean() method while ApplicationContext instantiates singleton bean when the container is started, It doesn't wait for getBean() method to be called.

Check out a difference between BeanFactory vs ApplicationContext in-detail at BeanFactory vs ApplicationContext in Spring?
The following diagram shows a high-level view of how Spring works. Your application classes are combined with configuration metadata so that, after the ApplicationContext is created and initialized, you have a fully configured and executable system or application.

What is Configuration Metadata?

From the above diagram, the Spring IoC container consumes a form of configuration metadata. This configuration metadata represents how you, as an application developer, tell the Spring container to instantiate, configure, and assemble the objects in your application.
Three ways we can supply Configuration Metadata to Spring IoC container
  1. XML-based configuration
  2. Annotation-based configuration
  3. Java-based configuration

How to Create a Spring Container?

Spring provides many ApplicationContext interface implementations that we use are;
  1. AnnotationConfigApplicationContext: If we are using Spring in standalone Java applications and using annotations for Configuration, then we can use this to initialize the container and get the bean objects.
  2. ClassPathXmlApplicationContext: If we have spring bean configuration XML file in a standalone application, then we can use this class to load the file and get the container object.
  3. FileSystemXmlApplicationContext: This is similar to ClassPathXmlApplicationContext except that the XML configuration file can be loaded from anywhere in the file system.
AnnotationConfigWebApplicationContext and XmlWebApplicationContext for web applications.
Let's write a code to create Spring container:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Note that we are supplying configuration metadata via applicationContext.xml file(XML-based configuration).
AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
Note that we are supplying configuration metadata via AppConfig.class file.
The most used API that implements the BeanFactory is the XmlBeanFactory.
 XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("applicationContext.xml")); 
Next, how to Retrieve bean from spring container?

How to Retrieve Bean from Spring Container?

Both BeanFactory and ApplicationContext interface provides getBean() method to retrieve bean from spring container.
ApplicationContext getBean() Example:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
BeanFactory getBean() Example:
XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("beans.xml")); 
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld"); 

Spring IOC Container XML Config Example

Spring IOC Container Java Config Example


>> Spring IOC Container Java Config Example

Difference Between BeanFactory vs ApplicationContext in Spring?


>> Check out a difference between BeanFactory vs ApplicationContext in-detail at BeanFactory vs ApplicationContext in Spring?
Check out complete Spring core tutorial at Spring Core Tutorial - Learn Spring IOC, DI, Configurations

Comments

  1. Very nicely explained Spring IOC container.

    ReplyDelete
  2. Clearly and useful !

    ReplyDelete
  3. Can you please explain the difference between bean factory and application context in detail.

    ReplyDelete
  4. Ur contents r very helpful ...
    Please make some videos on microservices and angular lastest for beginners

    ReplyDelete

Post a Comment

Leave Comment