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

Spring Framework-Lecture 4

Uploaded by

kshitija Urmale
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Spring Framework-Lecture 4

Uploaded by

kshitija Urmale
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

SPRING & SPRING BOOT

LECTURE - 4
Today’s Agenda

How Spring Helps In DI ?

Introduction To IOC & IOC


Container
Types Of IOC
Containers
Working Of Spring
Container
How Spring Helps In DI ?
 In all the previous examples we discussed , we ourself implemented DI
through code.

 However the Spring Framework also can perform DI for us automatically


and that’s probably the FIRST REASON for us to use Spring for our projects.
Important Question
 Question ?

 Why would we want Spring to create objects and manage dependencies


for us if we can do it ourself using new keyword ?
Important Question
 Answer

 There’s nothing wrong with we ourself creating the object, but wouldn’t it be nice to manage the
dependencies in a better way?

 Imagine an application with dozens or even hundreds of classes.

 Managing such a large number of objects will be extremely difficult. This is where Spring comes to
the rescue.

 Instead of constructing dependencies by itself, an object can retrieve its dependencies from Spring .

 All we need to do is to provide the container with appropriate configuration.


How Spring Performs DI ?
 Spring performs DI using a special software component called IOC
Container.

 But before discussing about IOC Container lets recall what is IOC ?
What Is IOC ?
 IOC stands for Inversion Of Control.

 It is a principle in software engineering which transfers the control of


objects or sections of a program to a container or framework.

 Lets take two examples to understand this.


Example 1
 Suppose you are a student.

 So what should be your main task ?

 Definitely learning/studying . Right ?

 Now , imagine you want to learn Spring


Framework.
Example 1
 Can you can start learning Spring by yourself ?

 Yes, why not !

 But before learning Spring you will require resources like books , notes,
videos, assignments and most importantly doubt solving.

 Now if you have to arrange these resources yourself then it will be very
difficult for you to focus on your core task i.e. learning Spring.
Example 1
 But suppose you join Spring Course at an institute .

 Now it will be institute’s responsibility to provide you someone who will


teach you Spring , provide you notes , solve all your doubts , provide
assignments and their solutions.

 And you are asked just to learn Spring , then it will be very easy.
Example 2
 Suppose you are a painter artist .

 So what should be your main task or expertise ?

 Definitely painting . Right ?


Example 2
 But before painting you will require tools like canvas , board, brush, colour
palette etc.

 Now if you have to arrange these tools yourself then it will be very difficult
for you to focus on your core job i.e. painting.

 But if someone arranges these tools for you and you are asked just
to do the painting then it will be very easy.
What Is IOC ?
 That SOMEONE is the FRAMEWORK and this principle is called as
INVERSION OF CONTROL.

 And to implement IOC and provide us Dependency Injection support , the


Spring Framework uses IOC Container.
Top Frameworks Using IOC
 Spring

 ASP.Net

 RoR

 Angular

 Laravel

 Google Guice
The Spring IOC
Container
 The Spring IoC container or Spring Container is the "Heart" or
"Core Component" of the Spring Framework.

 It is same like :-

 JVM which is used to execute the Core Java programs.

 Servlet Container which is used to execute the Servlets.


Responsibilities Of Spring
Container
 The Spring Container has many responsibilities but most important of
them are:
 Instantiate the Spring bean objects

 Configures the Spring bean objects

 Initialize the Spring bean objects

 Manage the Spring bean life-cycle

 Destroy the Spring bean objects

 Dependency Injection

 Resource Management ( db conn, api integration, security integration)


What Is A Spring
Bean ?
 In Spring, any Java object that is created and managed by the Spring
container is called as a Spring Bean.
Types Of Spring
Containers
 There are basically two types of IOC Containers in Spring:

 BeanFactory: It is an interface available in the package


org.springframework.beans.factory but is the old or outdated container.

 ApplicationContext: It is also an interface and it inherits from the


BeanFactory interface. It is available in the package
org.springframework.context and is the new and modern container.
Hierarchy Of Spring Containers
Working Of Spring
Container
What Is Configuration ?
 Configuration means settings/instructions provided to the Spring container .

 They help the container to define and manage various aspects of an application.

 There are lot of configurations in Spring like:

 Bean definitions
 Dependency Injection
 Bean Lifecycle
 Bean Autowiring
 Bean Post Processing
 Database Configurations
 Security Confgurations
 AOP Configurations
Ways Of Providing Configurations
 Configurations can be provided to Spring by :

 XML
 Java
 Annotation
 Properties file
 Environment Variables
 Command line arguments

and many more other ways.

You might also like