What Is Spring IOC Container

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 interdependencies between those objects.
The responsibilities of the 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 the Spring IOC container. ApplicationContext is the child interface of the BeanFactory interface that provides Spring AOP features, i18n, etc.
One main difference between BeanFactory and ApplicationContext in 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.

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