0% found this document useful (0 votes)
4 views

Module-2(IOC & Dependency Injection)

Inversion of Control (IoC) is a software engineering principle that shifts control of program flow from custom code to a framework, often utilizing Dependency Injection (DI) to manage application components. There are two main types of IoC containers: BeanFactory and ApplicationContext, with the latter being part of the Spring framework and responsible for managing the lifecycle of beans. Dependency Injection connects objects through an assembler, and the Spring framework provides various implementations of the ApplicationContext interface for different application types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Module-2(IOC & Dependency Injection)

Inversion of Control (IoC) is a software engineering principle that shifts control of program flow from custom code to a framework, often utilizing Dependency Injection (DI) to manage application components. There are two main types of IoC containers: BeanFactory and ApplicationContext, with the latter being part of the Spring framework and responsible for managing the lifecycle of beans. Dependency Injection connects objects through an assembler, and the Spring framework provides various implementations of the ApplicationContext interface for different application types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNDERSTANDING

IOC(INVERSION OF CONTROL)
What Is Inversion of Control?
Inversion of Control is a principle in software engineering which transfers
the control of objects or portions of a program to a container or framework.
We most often use it in the context of object-oriented programming.

In contrast with traditional programming, in which our custom code makes


calls to a library, IOC enables a framework to take control of the flow of a
program and make calls to our custom code

The Container uses Dependency Injection(DI) to manage the components


that make up the application. It gets the information about the objects
from a configuration file(XML) or Java Code or Java Annotations and Java
POJO class. These objects are called Beans. Since the Controlling of Java
objects and their lifecycle is not done by the developers, hence the name
Inversion Of Control.
There are 2 types of IoC containers:
BeanFactory

BeanFactory represents a basic IoC container which is a parent interface


of ApplicationContext. BeanFactory uses Beans and their
dependencies metadata to create and configure them at run-time.
BeanFactory loads the bean definitions and dependency amongst the
beans based on a configuration file(XML) or the beans can be directly
returned when required using Java Configuration.

ApplicationContext

ApplicationContext belongs to the Spring framework. Spring IoC


container is responsible for instantiating, wiring, configuring, and
managing the entire life cycle of beans or
objects. BeanFactory and ApplicationContext represent the Spring IoC
Containers. ApplicationContext is the sub-interface of BeanFactory. It is
used when we are creating an enterprise-level application or web
application
What Is Dependency Injection?
Dependency injection is a pattern we can use to implement IoC, where the
control being inverted is setting an object’s dependencies.
Connecting objects with other objects, or “injecting” objects into other
objects, is done by an assembler rather than by the objects themselves.
The Spring IoC Container
An IoC container is a common characteristic of frameworks that
implement IoC.
In the Spring framework, the interface ApplicationContext represents the
IoC container. The Spring container is responsible for instantiating,
configuring and assembling objects known as beans, as well as managing
their life cycles.
The Spring framework provides several implementations of
the ApplicationContext interface: AnnotationConfigApplicationContext, Cl
assPathXmlApplicationContext and FileSystemXmlApplicationContext for
standalone applications, and WebApplicationContext for web applications.
In order to assemble beans, the container uses configuration metadata,
which can be in the form of XML configuration or annotations.
EXAMPLE OF
DEPENDENCY INJECTION
For Dependency Example we are going to use
Maven Project, instead of Java Project. Maven lets you get your
package dependencies easily

Maven forces you to have a


standard directory structure

Maven is used to manage the


build, testing, and deployment
processes. It can separate the
Why Maven Project ?
unit tests and integration tests
so you only run them when
necessary and cut down on build
time.
Maven pom.xml file. POM is
an acronym for Project Object
Model. The pom.xml
file contains information of
project and configuration
information for the maven to
build the project such as
dependencies, build
directory, source directory
To design an example we need to add some jars in POM.xml
For jars we have to use Spring Maven Repository.

Some of the common jars

Spring Web
Spring Web MVC
Spring Core
Spring Data Core
Spring
Spring AOP
Context
Spring
Spring Bean
Transaction
Spring JDBC
Examples refer to Sts
Practical examples will be show in class.

You might also like