SlideShare a Scribd company logo
SESSION - 3 & 4
Byโ†’ Anuj Kumar Singh
IOC and Dependency Injection
1. Dependency Injection is the main functionality provided by Spring
IOC (Inversion of Control).
2. The Spring-Core module is responsible for injecting dependencies
through either Constructor or Setter methods in POJO class.
3. Dependency Injection in Spring also ensures loose-coupling
between the classes.
Need for Dependency Injection:
1. Spring IOC resolves dependencies with Dependency Injection,
which makes the code easier to test and reuse.
2. Loose coupling between classes can be possible by defining
interfaces for common functionality and the injector will instantiate
the objects of required implementation.
Example
Tight Coupling - (without IOC)
class Employee{
Address address;
Employee()
{address=new Address(โ€œxyz
streetโ€, โ€Lucknowโ€, โ€UPโ€);
}}
class Address{
Address(String street, String city,
String state){
โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ..
...............................
}}
One class object is created in another class in tight coupling. Leads to
a. Stop server b. Recompile c. Create jar and war file
d. Redeploy e. Restart the server.
1. with IOC (Setter Injection)
class Employee{
Address address;
public void setAddress(Address
address)
{
this.address=address;
} }
class Address
{
Address()
{
โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ.
โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ.
}}
Loose Coupling
2. with IOC (Constructor Injection)
class Employee
{
Address address;
Employee(Address address)
{
this.address=address;
} }
class Address
{
Address()
{
โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ.
โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ.
}}
No need to stop the existing component, in real time it is better.
POJO(Plain Old Java Object)
1. POJO is a ordinary Java Object not bounded by any restriction,
they are loosely coupled.
2. POJO help to increase the readability and reusability of a program.
3. Helps to achieve three types of Dependencies
1. Dependencies in the form of Primitives
a. Setter Dependency
b. Constructor Dependency
2. Dependency in the form of Object
3. Dependency in the form of Collections
Rules for Creating POJOโ€˜s
1. Class must be public
2. Variables must be private
3. Must having public default Constructor
4. Can have arg-constructor
5. Every property should have public getter and setter method
POJO Example
public class Employee
{
private String name,email;
public String getName()
{return name;
}
public void setName(String name)
{this.name=name;}
public String getEmail()
{return email;}
public void setEmail(String email)
{this.email=email;}
}
Containers in Spring
1. Core Container (slower)
a. It is called Lazy Container.
b. It create object on demand one by one.
c. It will create the object when the bean file is loaded.
ClassPathResource res=new ClassPathResource(โ€œSpring.xmlโ€);
XmlBeanFactory fact=new XmlBeanFactory(res);
fact.getBean(โ€œabcโ€);
2. J2EE Container (faster)
a. It is called Eager Container.
b. It create all object at once.
c. It cerate the object as soon as object of ApplicationContext is made.
ApplicationContext con=new
ClassPathXmlApplicationContext(โ€œSpring.xmlโ€);
XmlBeanFactory VS ApplicationContext Container
1. XmlBeanFactory
2. ApplicationContext
1. The objects that form the backbone of your application and that are
managed by the Spring IoC container are called beans.
2. A bean is an object that is instantiated, assembled, and otherwise
managed by a Spring IoC container.
3. These beans are created with the configuration metadata that you
supply to the container. For example, in the form of XML <bean/>
definitions.
4. Bean definition contains the information called configuration
metadata, which is needed for the container to know the following โˆ’
a. How to create a bean
b. Bean's lifecycle details
c. Bean's dependencies
Bean File (.xml)
Properties & Description in Bean File
1. class : This attribute is mandatory and specifies the bean class to
be used to create the bean.
2. name: This attribute specifies the bean identifier uniquely. In XML
based configuration metadata, you use the id and/or name
attributes to specify the bean identifier(s).
3. scope: This attribute specifies the scope of the objects created from
a particular bean definition.
4. constructor-arg: This is used to inject the dependencies.
5. properties: This is used to inject the dependencies.
6. autowiring mode: This is used to inject the dependencies.
7. lazy-initialization mode: A lazy-initialized bean tells the IoC
container to create a bean instance when it is first requested, rather
than at the startup.
8. initialization method: A callback to be called just after all necessary
properties on the bean have been set by the container.
9. destruction method: A callback to be used when the container
containing the bean is destroyed.
Bean Scope
When defining a <bean> you have the option of declaring a scope for that
bean. For example, to force Spring to produce a new bean instance each
time, you should declare the bean's scope attribute to be prototype.
Similarly, if you want Spring to return the same bean instance each time
one is needed, you should declare the bean's scope attribute to
be singleton(default).
Scope & Description
1. Singleton: This scopes the bean definition to a single instance per
Spring IoC container (default).
2. Prototype: This scopes a single bean definition to have any number
of object instances.
Bean Lifecycle
1. When a bean is instantiated, it may be required to perform some
initialization to get it into a usable state. Similarly, when the bean is
no longer required and is removed from the container, some
cleanup may be required.
2. The time of bean Instantiation and its destruction, bean life cycle
callback methods, which are required at the time of bean
initialization and its destruction.
3. To define setup and teardown for a bean, we simply declare the
<bean> with init method and/or destroy-method parameters. The
init-method attribute specifies a method that is to be called on the
bean immediately upon instantiation and destroy method specifies
a method that is called just before a bean is removed from the
container.
How to Start Using Spring
1. create the class
2. create the xml file to provide the values
3. create the test class
4. Load the spring jar files
5. Run the test class
Setter Injection ๏ƒ 
1. CLASS FILE - POJO (PLAIN OLD JAVA OBJECT)
2. TEST FILE - DAO (DATA ACCESS OBJECT)
3. BEAN FILE - XML
4. Load the jar files required for spring framework
There are mainly three jar files required to run this application.
1. org.springframework.core-3.0.1.RELEASE-A
2. com.springsource.org.apache.commons.logging-1.1.1
3. org.springframework.beans-3.0.1.RELEASE-A
5. Run the TEST FILE
Constructor Injection ๏ƒ 
1. POJO CLASS
2. Test Class
3. XML File
Advantages of POJOโ€™s
1. Lightweight - Spring framework is lightweight because of its POJO
Enable you to write powerful, scalable applications using POJO.
2. Easy to develop J2EE application - The Dependency Injection
feature of Spring Framework and it support to various frameworks
makes the easy development of JavaEE application.
3. Easy to test - The Dependency Injection makes easier to test the
application. The EJB or Struts application require server to run the
application but Spring framework doesn't require server.
4. Loose Coupling - The Spring applications are loosely coupled
because of dependency injection and handles injecting dependent
components without a component knowing where they came from
(IoC).
8. Lifecycle โ€“ Spring Framework is responsible for managing POJOโ€™s
lifecycle: init(), destroy().
Spring framework  IOC and Dependency Injection

More Related Content

What's hot

JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
elliando dias
ย 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
ย 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Trey Howard
ย 
Spring User Guide
Spring User GuideSpring User Guide
Spring User Guide
Muthuselvam RS
ย 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ASG
ย 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Hรนng Nguyแป…n Huy
ย 
ReactJS
ReactJSReactJS
ReactJS
Hiten Pratap Singh
ย 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
ย 
Spring Security
Spring SecuritySpring Security
Spring Security
Knoldus Inc.
ย 
Spring core module
Spring core moduleSpring core module
Spring core module
Raj Tomar
ย 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Arjun Thakur
ย 
Introduction aฬ€ spring boot
Introduction aฬ€ spring bootIntroduction aฬ€ spring boot
Introduction aฬ€ spring boot
Antoine Rey
ย 
Exploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & BeyondExploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & Beyond
Kaushal Dhruw
ย 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Dineesha Suraweera
ย 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
Maulik Shah
ย 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
Dzmitry Naskou
ย 
Ch12 Spring ่ตทๆญฅ่ตฐ
Ch12 Spring ่ตทๆญฅ่ตฐCh12 Spring ่ตทๆญฅ่ตฐ
Ch12 Spring ่ตทๆญฅ่ตฐ
Justin Lin
ย 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
shaunthomas999
ย 
Spring beans
Spring beansSpring beans
Spring beans
Roman Dovgan
ย 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Knoldus Inc.
ย 

What's hot (20)

JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
ย 
Spring Boot
Spring BootSpring Boot
Spring Boot
ย 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
ย 
Spring User Guide
Spring User GuideSpring User Guide
Spring User Guide
ย 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ย 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ย 
ReactJS
ReactJSReactJS
ReactJS
ย 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
ย 
Spring Security
Spring SecuritySpring Security
Spring Security
ย 
Spring core module
Spring core moduleSpring core module
Spring core module
ย 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
ย 
Introduction aฬ€ spring boot
Introduction aฬ€ spring bootIntroduction aฬ€ spring boot
Introduction aฬ€ spring boot
ย 
Exploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & BeyondExploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & Beyond
ย 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ย 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
ย 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
ย 
Ch12 Spring ่ตทๆญฅ่ตฐ
Ch12 Spring ่ตทๆญฅ่ตฐCh12 Spring ่ตทๆญฅ่ตฐ
Ch12 Spring ่ตทๆญฅ่ตฐ
ย 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
ย 
Spring beans
Spring beansSpring beans
Spring beans
ย 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
ย 

Similar to Spring framework IOC and Dependency Injection

Spring framework
Spring frameworkSpring framework
Spring framework
Ajit Koti
ย 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
Anuj Singh Rajput
ย 
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf
DeoDuaNaoHet
ย 
Spring IOC advantages and developing spring application sample
Spring IOC advantages and developing spring application sample Spring IOC advantages and developing spring application sample
Spring IOC advantages and developing spring application sample
Sunil kumar Mohanty
ย 
Spring training
Spring trainingSpring training
Spring training
shah_d_p
ย 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
ThirupathiReddy Vajjala
ย 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
Dhaval Shah
ย 
Spring
SpringSpring
Spring
Kamalmeet Singh
ย 
Spring bean mod02
Spring bean mod02Spring bean mod02
Spring bean mod02
Guo Albert
ย 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
Dhiraj Champawat
ย 
Spring
SpringSpring
Spring
s4al_com
ย 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
s4al_com
ย 
Different Types of Containers in Spring
Different Types of Containers in Spring Different Types of Containers in Spring
Different Types of Containers in Spring
Sunil kumar Mohanty
ย 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
Fu Cheng
ย 
Spock
SpockSpock
Spock
Naiyer Asif
ย 
SpringIntroductionpresentationoverintroduction.ppt
SpringIntroductionpresentationoverintroduction.pptSpringIntroductionpresentationoverintroduction.ppt
SpringIntroductionpresentationoverintroduction.ppt
imjdabhinawpandey
ย 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
Deepu S Nath
ย 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAO
AnushaNaidu
ย 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
ealio
ย 
S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010
S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010
S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010
Arun Gupta
ย 

Similar to Spring framework IOC and Dependency Injection (20)

Spring framework
Spring frameworkSpring framework
Spring framework
ย 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
ย 
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf
ย 
Spring IOC advantages and developing spring application sample
Spring IOC advantages and developing spring application sample Spring IOC advantages and developing spring application sample
Spring IOC advantages and developing spring application sample
ย 
Spring training
Spring trainingSpring training
Spring training
ย 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
ย 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
ย 
Spring
SpringSpring
Spring
ย 
Spring bean mod02
Spring bean mod02Spring bean mod02
Spring bean mod02
ย 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
ย 
Spring
SpringSpring
Spring
ย 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
ย 
Different Types of Containers in Spring
Different Types of Containers in Spring Different Types of Containers in Spring
Different Types of Containers in Spring
ย 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
ย 
Spock
SpockSpock
Spock
ย 
SpringIntroductionpresentationoverintroduction.ppt
SpringIntroductionpresentationoverintroduction.pptSpringIntroductionpresentationoverintroduction.ppt
SpringIntroductionpresentationoverintroduction.ppt
ย 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
ย 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAO
ย 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
ย 
S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010
S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010
S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010
ย 

More from Anuj Singh Rajput

Web technology
Web technologyWeb technology
Web technology
Anuj Singh Rajput
ย 
Java script
Java scriptJava script
Java script
Anuj Singh Rajput
ย 
Html (hypertext markup language)
Html (hypertext markup language)Html (hypertext markup language)
Html (hypertext markup language)
Anuj Singh Rajput
ย 
Css
CssCss
Bootstrap
BootstrapBootstrap
Bootstrap
Anuj Singh Rajput
ย 
Jsp session 13
Jsp   session 13Jsp   session 13
Jsp session 13
Anuj Singh Rajput
ย 
Jsp session 12
Jsp   session 12Jsp   session 12
Jsp session 12
Anuj Singh Rajput
ย 
Jsp session 11
Jsp   session 11Jsp   session 11
Jsp session 11
Anuj Singh Rajput
ย 
Jsp session 10
Jsp   session 10Jsp   session 10
Jsp session 10
Anuj Singh Rajput
ย 
Jsp session 9
Jsp   session 9Jsp   session 9
Jsp session 9
Anuj Singh Rajput
ย 
Jsp session 8
Jsp   session 8Jsp   session 8
Jsp session 8
Anuj Singh Rajput
ย 
Jsp session 7
Jsp   session 7Jsp   session 7
Jsp session 7
Anuj Singh Rajput
ย 
Jsp session 6
Jsp   session 6Jsp   session 6
Jsp session 6
Anuj Singh Rajput
ย 
Jsp session 5
Jsp   session 5Jsp   session 5
Jsp session 5
Anuj Singh Rajput
ย 
Jsp session 4
Jsp   session 4Jsp   session 4
Jsp session 4
Anuj Singh Rajput
ย 
Jsp session 3
Jsp   session 3Jsp   session 3
Jsp session 3
Anuj Singh Rajput
ย 
Jsp session 2
Jsp   session 2Jsp   session 2
Jsp session 2
Anuj Singh Rajput
ย 
Jsp session 1
Jsp   session 1Jsp   session 1
Jsp session 1
Anuj Singh Rajput
ย 
Servlet session 14
Servlet   session 14Servlet   session 14
Servlet session 14
Anuj Singh Rajput
ย 
Servlet session 13
Servlet   session 13Servlet   session 13
Servlet session 13
Anuj Singh Rajput
ย 

More from Anuj Singh Rajput (20)

Web technology
Web technologyWeb technology
Web technology
ย 
Java script
Java scriptJava script
Java script
ย 
Html (hypertext markup language)
Html (hypertext markup language)Html (hypertext markup language)
Html (hypertext markup language)
ย 
Css
CssCss
Css
ย 
Bootstrap
BootstrapBootstrap
Bootstrap
ย 
Jsp session 13
Jsp   session 13Jsp   session 13
Jsp session 13
ย 
Jsp session 12
Jsp   session 12Jsp   session 12
Jsp session 12
ย 
Jsp session 11
Jsp   session 11Jsp   session 11
Jsp session 11
ย 
Jsp session 10
Jsp   session 10Jsp   session 10
Jsp session 10
ย 
Jsp session 9
Jsp   session 9Jsp   session 9
Jsp session 9
ย 
Jsp session 8
Jsp   session 8Jsp   session 8
Jsp session 8
ย 
Jsp session 7
Jsp   session 7Jsp   session 7
Jsp session 7
ย 
Jsp session 6
Jsp   session 6Jsp   session 6
Jsp session 6
ย 
Jsp session 5
Jsp   session 5Jsp   session 5
Jsp session 5
ย 
Jsp session 4
Jsp   session 4Jsp   session 4
Jsp session 4
ย 
Jsp session 3
Jsp   session 3Jsp   session 3
Jsp session 3
ย 
Jsp session 2
Jsp   session 2Jsp   session 2
Jsp session 2
ย 
Jsp session 1
Jsp   session 1Jsp   session 1
Jsp session 1
ย 
Servlet session 14
Servlet   session 14Servlet   session 14
Servlet session 14
ย 
Servlet session 13
Servlet   session 13Servlet   session 13
Servlet session 13
ย 

Recently uploaded

Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
ย 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
ย 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
MJDuyan
ย 
How to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in useHow to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in use
Celine George
ย 
Observational Learning
Observational Learning Observational Learning
Observational Learning
sanamushtaq922
ย 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
ย 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
ย 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
ย 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
ย 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
ย 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
Celine George
ย 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
ย 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
ย 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
ย 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
ย 
BPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end examBPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end exam
sonukumargpnirsadhan
ย 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
ย 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
ย 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
ย 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
RandolphRadicy
ย 

Recently uploaded (20)

Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
ย 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
ย 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
ย 
How to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in useHow to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in use
ย 
Observational Learning
Observational Learning Observational Learning
Observational Learning
ย 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
ย 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
ย 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
ย 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
ย 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
ย 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
ย 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
ย 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
ย 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
ย 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
ย 
BPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end examBPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end exam
ย 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
ย 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
ย 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
ย 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
ย 

Spring framework IOC and Dependency Injection

  • 1. SESSION - 3 & 4 Byโ†’ Anuj Kumar Singh
  • 2. IOC and Dependency Injection 1. Dependency Injection is the main functionality provided by Spring IOC (Inversion of Control). 2. The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods in POJO class. 3. Dependency Injection in Spring also ensures loose-coupling between the classes. Need for Dependency Injection: 1. Spring IOC resolves dependencies with Dependency Injection, which makes the code easier to test and reuse. 2. Loose coupling between classes can be possible by defining interfaces for common functionality and the injector will instantiate the objects of required implementation.
  • 3.
  • 4. Example Tight Coupling - (without IOC) class Employee{ Address address; Employee() {address=new Address(โ€œxyz streetโ€, โ€Lucknowโ€, โ€UPโ€); }} class Address{ Address(String street, String city, String state){ โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ.. ............................... }} One class object is created in another class in tight coupling. Leads to a. Stop server b. Recompile c. Create jar and war file d. Redeploy e. Restart the server.
  • 5. 1. with IOC (Setter Injection) class Employee{ Address address; public void setAddress(Address address) { this.address=address; } } class Address { Address() { โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ. โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ. }} Loose Coupling
  • 6. 2. with IOC (Constructor Injection) class Employee { Address address; Employee(Address address) { this.address=address; } } class Address { Address() { โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ. โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ. }} No need to stop the existing component, in real time it is better.
  • 7. POJO(Plain Old Java Object) 1. POJO is a ordinary Java Object not bounded by any restriction, they are loosely coupled. 2. POJO help to increase the readability and reusability of a program. 3. Helps to achieve three types of Dependencies 1. Dependencies in the form of Primitives a. Setter Dependency b. Constructor Dependency 2. Dependency in the form of Object 3. Dependency in the form of Collections
  • 8. Rules for Creating POJOโ€˜s 1. Class must be public 2. Variables must be private 3. Must having public default Constructor 4. Can have arg-constructor 5. Every property should have public getter and setter method
  • 9. POJO Example public class Employee { private String name,email; public String getName() {return name; } public void setName(String name) {this.name=name;} public String getEmail() {return email;} public void setEmail(String email) {this.email=email;} }
  • 10. Containers in Spring 1. Core Container (slower) a. It is called Lazy Container. b. It create object on demand one by one. c. It will create the object when the bean file is loaded. ClassPathResource res=new ClassPathResource(โ€œSpring.xmlโ€); XmlBeanFactory fact=new XmlBeanFactory(res); fact.getBean(โ€œabcโ€); 2. J2EE Container (faster) a. It is called Eager Container. b. It create all object at once. c. It cerate the object as soon as object of ApplicationContext is made. ApplicationContext con=new ClassPathXmlApplicationContext(โ€œSpring.xmlโ€);
  • 11. XmlBeanFactory VS ApplicationContext Container 1. XmlBeanFactory
  • 13. 1. The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. 2. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. 3. These beans are created with the configuration metadata that you supply to the container. For example, in the form of XML <bean/> definitions. 4. Bean definition contains the information called configuration metadata, which is needed for the container to know the following โˆ’ a. How to create a bean b. Bean's lifecycle details c. Bean's dependencies Bean File (.xml)
  • 14. Properties & Description in Bean File 1. class : This attribute is mandatory and specifies the bean class to be used to create the bean. 2. name: This attribute specifies the bean identifier uniquely. In XML based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). 3. scope: This attribute specifies the scope of the objects created from a particular bean definition. 4. constructor-arg: This is used to inject the dependencies. 5. properties: This is used to inject the dependencies. 6. autowiring mode: This is used to inject the dependencies.
  • 15. 7. lazy-initialization mode: A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at the startup. 8. initialization method: A callback to be called just after all necessary properties on the bean have been set by the container. 9. destruction method: A callback to be used when the container containing the bean is destroyed.
  • 16. Bean Scope When defining a <bean> you have the option of declaring a scope for that bean. For example, to force Spring to produce a new bean instance each time, you should declare the bean's scope attribute to be prototype. Similarly, if you want Spring to return the same bean instance each time one is needed, you should declare the bean's scope attribute to be singleton(default). Scope & Description 1. Singleton: This scopes the bean definition to a single instance per Spring IoC container (default). 2. Prototype: This scopes a single bean definition to have any number of object instances.
  • 17. Bean Lifecycle 1. When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required. 2. The time of bean Instantiation and its destruction, bean life cycle callback methods, which are required at the time of bean initialization and its destruction. 3. To define setup and teardown for a bean, we simply declare the <bean> with init method and/or destroy-method parameters. The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation and destroy method specifies a method that is called just before a bean is removed from the container.
  • 18. How to Start Using Spring 1. create the class 2. create the xml file to provide the values 3. create the test class 4. Load the spring jar files 5. Run the test class
  • 19. Setter Injection ๏ƒ  1. CLASS FILE - POJO (PLAIN OLD JAVA OBJECT)
  • 20. 2. TEST FILE - DAO (DATA ACCESS OBJECT)
  • 21. 3. BEAN FILE - XML
  • 22. 4. Load the jar files required for spring framework There are mainly three jar files required to run this application. 1. org.springframework.core-3.0.1.RELEASE-A 2. com.springsource.org.apache.commons.logging-1.1.1 3. org.springframework.beans-3.0.1.RELEASE-A 5. Run the TEST FILE
  • 26. Advantages of POJOโ€™s 1. Lightweight - Spring framework is lightweight because of its POJO Enable you to write powerful, scalable applications using POJO. 2. Easy to develop J2EE application - The Dependency Injection feature of Spring Framework and it support to various frameworks makes the easy development of JavaEE application. 3. Easy to test - The Dependency Injection makes easier to test the application. The EJB or Struts application require server to run the application but Spring framework doesn't require server. 4. Loose Coupling - The Spring applications are loosely coupled because of dependency injection and handles injecting dependent components without a component knowing where they came from (IoC).
  • 27. 8. Lifecycle โ€“ Spring Framework is responsible for managing POJOโ€™s lifecycle: init(), destroy().