Spring – IOC Container

The most important component of the Spring Framework is the Spring Core Container which creates the objects, wires them together, manages the configuration and overall object life cycle from creation of the object till its destruction. We can also say that The IoC container is responsible to instantiate, configure and assemble the objects. The Spring container uses Dependency Injection (DI) to manage the components that make up an application. The below diagram depicts that the application classes are combined with the configuration metadata which creates and initializes the ApplicationContext and provides a fully customized application, ready for use

Spring IOC Container

Spring Containers

Spring provides two types of containers.

  • Spring BeanFactory container
  • Spring ApplicationContext container

Spring Bean Factory Container

Spring Bean Factory Container uses the org. springframework. beans. factory. BeanFactory interface and provides the support for Dependency Injection (DI) by registering different beans and their dependencies. The XmlBeanFactory is the implementation class for the BeanFactory interface. The constructor of XmlBeanFactory class receives the Resource object so we need to pass the resource object to create the object of BeanFactory.

Resource resource=new ClassPathResource(“applicationContext.xml”);  

BeanFactory factory=new XmlBeanFactory(resource); 

Spring ApplicationContext Container

The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface. Use the below given syntax to instantiate the ClassPathXmlApplicationContext class to use the ApplicationContext as given below:

ApplicationContext context =   new ClassPathXmlApplicationContext(“applicationContext.xml”);  

spring- applicationContext container

The constructor of ClassPathXmlApplicationContext class receives string, so we can pass the name of the xml file to create the instance of ApplicationContext.

Below given are the possible ways for implementing the Inversion of Control (Spring IoC Container):

  1. With the usage of Factory Pattern
  2. With the usage of Service Locator Pattern
  3. With the Inclusion of Dependency Injection through
  • The Constructor Injection
  • The Setter Injection
  • The Interface Injection

Spring Architecture

Spring Framework is one of the most commonly used Java EE open source framework, initially written by Rod Johnson and was released under the Apache 2.0 license in June 2003. The Spring Framework provides a comprehensive and infrastructural support providing focus on building application-level business logic and reduces the ties on specific deployment environments.

The tutorial is designed on the Spring Version Spring 5 for the student and Java developers or anyone who wants to learn spring framework for implementing the learned skills to enhance their career path.

What is Spring Framework ?

Spring framework is a modular architecture consisting of several modules (grouped as Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, Messaging, and Test) which allows developers to build enterprise web applications with the ease. Developers can select the modules required for building the components of web applications without having to bring in all the modules and thus makes it lightweight and flexible as developers are using only the required set of modules and not the complete bundle . The below given is the Spring Architecture Diagram highlighting the main modules it provides. Let’s discuss about the architecture components to understand how they can be used in the application development.

Spring Architecture

Core Container in Spring

The Spring Core Container consists of spring-core, spring-beans, spring-context, spring-context-support, and spring-expression (expression language) modules

  • The spring-core and spring-beans modules provides the fundamental parts of the framework, including the IoC (Inversion of Control) and Dependency Injection (DI) features.
  • The spring-beans module provides BeanFactory, which inherits the implementation of the factory pattern.
  • The spring-context module provides the mechanism to access objects in a framework-style manner, similar to a JNDI registry with the base foundation of spring-core and spring-beans modules. The Context module inherits spring-beans module to provide support for internationalization (resource bundles), event propagation, resource loading, and the transparent creation of contexts (Servlet Container) and also supports Java EE features such as EJB, JMX, and basic remoting. The ApplicationContext interface is the focal point of the Context module.
  • The spring-expression (SpEL) module provides a powerful expression language for querying and manipulating an object graph at runtime.

Data Access / Integration in Spring

The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and Transaction modules. Let’s discuss about these components to understand how they can be used in the application development.

  • The spring-jdbc (JDBC) module provides a JDBC-abstraction layer that removes the need for tedious JDBC related coding and parsing of vendor specific error codes.
  • The spring-orm (ORM)module provides integration layers for popular object-relational mapping APIs, including Hibernate, JPA, JPA, Top Link, JDO and iBatis.
  • The spring-oxm (OXM) module provides an abstraction layer that supports Object/XML mapping implementations for JAXB, Castor, XML Beans, JiBX and XStream
  • The spring-jms (Java Messaging Service -JMS) module contains features for producing and consuming messages.
  • The spring-tx (Transaction) module supports programmatic and declarative transaction management for classes that implement special interfaces and for all your POJOs

Web Layer in Spring

The Web layer consists of the Web (spring-web), Web-MVC(spring-webmvc), Web-Socket (spring-websocket) and Web Portlet (Web-Portlet) modules. Let’s discuss about these components to understand how they can be used in the application development.

  • The Web module provides basic web-oriented integration features such as HTTP Client for remoting support, multipart file-upload functionality and the initialization of the IoC container using servlet listeners and a web-oriented application context.
  • The Web-MVC module contains Spring’s Model-View-Controller (MVC) and REST Services implementation for web applications.
  • The Web-Socket module provides support for WebSocket-based, two-way communication between the client and the server in web applications.
  • The Web-Portlet module provides the MVC implementation to be used in a portlet environment and mirrors the functionality of Web-Servlet module

AOP , Aspects, Instrumentation in Spring

  • The spring-aop module provides an aspect-oriented programming implementation allows developers to define method interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated.
  • The spring-aspects (Aspects) module provides integration with AspectJ, which is again a powerful and mature AOP framework
  • The spring-instrument (Instrumentation) module provides class instrumentation support and class loader implementations to be used in certain application servers. The spring-instrument-tomcat module contains Spring’s instrumentation agent for Tomcat


The spring-messaging (Messaging) module provides key abstractions such as MessageMessageChannelMessageHandler and provides support for STOMP as the Web Socket sub-protocol to use in applications. It also provides the set of annotations for mapping messages to methods, similar to the Spring MVC annotation based programming model

Test module in Spring

  • The spring-test (Test) module supports the Unit testing and Integration Testing of Spring components with JUnit or