EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial IoC Containers
Secondary Sidebar
Spring Tutorial
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

IoC Containers

By Priya PedamkarPriya Pedamkar

IoC Containers

Introduction to IoC Containers

The following article provides an outline for IoC Containers. One of the most important parts of the Spring Core Framework is the Spring IoC Container. Spring IoC Container is mainly responsible for implementing Inversion of Control (IoC) i.e., to achieve loose coupling between the classes by creating the objects & injecting them dynamically during the run time.

Spring IoC Containers help in instantiating the class & creating the object, configuring the object, assembling and managing the dependencies between the objects. The information required for creating, configuring & assembling the objects is obtained by reading the XML Configuration file or scanning the Spring Java Annotations used in the project.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Architecture of IoC Container

IoC Containers

From the above architecture diagram, we can interpret that the Spring Container consumes Configuration Metadata which is nothing but the XML file Configuration or Java annotations.

This Configuration Metadata contains information which defines:

  1. What are the Objects to be instantiated?
  2. What are the Objects to be configured & how to configure?
  3. How to assemble the Objects?

These objects will be a part of your Project’s Business Objects or DAO Objects. So, IoC Container instantiates, configures, assembles the objects at the time of starting up the application itself as well as manage the object life cycle from creation till destruction.

Types of IoC Containers

Spring Framework provides two types of IoC Containers.

  • Bean Factory
  • Application Context

1. Bean Factory

The Bean Factory, the root interface of the IoC container provides basic support for achieving loose coupling between the classes using Dependency Injection. It is defined under org.springframework.beans.factory. BeanFactory Interface. As the name suggests, Bean Factory is a place where the beans are manufactured & injected into the classes.

You may be wondering, what are Beans? Beans are nothing, but the Java Objects that are initialized by the Spring in it. So, any POJO classes or DAO classes act as a Spring Bean if it is configured to be instantiated by IoC Containers from the meta-information available in the XML configuration file or as a Java-based annotation in the project.

Scopes of Beans:

  • Singleton: This is the default scope. As the name suggests, only one instance of the bean will be created for each container.
  • Prototype: For each request, a new bean will be created.
  • Request: It will be used in Web Application. For each HTTP request from the client, a new bean will be created.
  • Session: For each HTTP Session, a new bean will be created.

How to Create a Bean Factory?

Let us see a real-time example of creating Bean Factory using XML based configurations.

IOC containers

Employee POJO class.

IOC containers

BeanFactory class for reading the XML Configuration using XmlBeanFactory.

IOC containers

BeanFactory.xml file for configuring the metadata for creating the bean objects by Spring Bean Factory.

Output:

Muneer Ahmed J

Methods of Bean Factory

There are six primary methods for Bean Factory.

  • Boolean containsBean (String): This method compares the name of the bean instance available with the name passed as an argument. Returns true if matched else returns false.
  • Object getBean (String): This method returns the available bean instance with the name passed as an argument. If the bean instance is not found, it throws NoSuchBeanDefinition Exception.
  • Object getBean (String, Class): This method returns the available bean instance with the name passed as an argument & it will be cast to the Class passed as the second argument. If the bean instance is not found, it throws NoSuchBeanDefinition Exception & if the bean cannot be cast to the provided class, it throws BeanNotOfRequiredType Exception.
  • Class getType (String name): This method will return the Class of the bean passed as an argument.
  • Boolean singleton (String): This method will check if the bean name passed as an argument is in Singleton scope or not. Return true if Singleton else returns false.
  • String[] getAliases (String): This method returns a single or array of aliases for the passed bean name, if available.

2. Application Context

Application Context is the same as Bean Factory as it is built upon BeanFactory Interface only, but it provides a few more extra functionalities such as:

  • Simple Integration with Spring Framework’s AOP.
  • Message resource handling in Internationalization (i18N).
  • Event propagation.
  • Most importantly WebApplicationContext for Web Applications.

It is defined under org.springframework.context.ApplicationContext Interface.

As you can see, Application Context is built over Bean Factory and it also provides a few more additional features, it is mostly recommended over Bean Factory.

How to Create Application Context?

Let us see a real-time example of creating Application Context using XML based configurations.

Creating Application Context

Employee POJO class.

employee pojo class

BeanFactory class for reading the XML Configuration using Application Context.

XML configuration

BeanFactory.xml file for configuring the metadata for creating the bean objects by Spring Bean Factory.

Output:

Muneer Ahmed J

Types of Application Context

There are three different types of Application Contexts.

  • FileSystemXmlApplicationContext
  • ClassPathXmlApplicationContext
  • WebXmlApplicationContext

Advantages of IoC Containers

Given below are the advantages mentioned:

  • Spring IoC Containers helps in instantiating, configuring & assembling the Objects provided in the XML Configuration files or Java Annotations.
  • Spring IoC Container helps in managing the Spring Bean Life Cycle i.e., right from instantiation till destruction.
  • Spring IoC Containers provide support for Inversion of Control (IoC).
  • It provides a factory of beans for dynamically binding them at the time of starting/booting up the application.
  • It helps in achieving loose coupling.
  • It provides easy support for creating Web Application using the WebApplication Context.
  • It minimizes the amount of code by avoiding boilerplate coding.
  • It provides support for Eager instantiation & lazy loading of Services.
  • It makes Unit Testing easy by allowing the developers to inject the Mock Objects.

Conclusion

From this article, we have done a deep dive into the architecture, importance, and role of IoC containers. We have also gone through the different types of IoC Containers and its advantages with the real-time example on XML based configurations.

Recommended Articles

This is a guide to IoC Containers. Here we discuss the introduction, architecture of IoC containers, along with the types and advantages. You can also go through our other suggested articles to learn more –

  1. Web Application Security
  2. Types of Web Hosting
  3. What is ASP.Net Web Services?
  4. What is Selenium Web Driver?
Popular Course in this category
Spring Framework Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  38+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Boot Training Program (2 Courses, 3 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more