SlideShare ist ein Scribd-Unternehmen logo
1 von 28
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

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API07.pallav
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
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 ControlArjun Thakur
 
Formation Spring Avancé gratuite par Ippon 2014
Formation Spring Avancé gratuite par Ippon 2014Formation Spring Avancé gratuite par Ippon 2014
Formation Spring Avancé gratuite par Ippon 2014Ippon
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVCIndicThreads
 
Spring core module
Spring core moduleSpring core module
Spring core moduleRaj Tomar
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introductionRasheed Waraich
 

Was ist angesagt? (20)

Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
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
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
Formation Spring Avancé gratuite par Ippon 2014
Formation Spring Avancé gratuite par Ippon 2014Formation Spring Avancé gratuite par Ippon 2014
Formation Spring Avancé gratuite par Ippon 2014
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
 
Completable future
Completable futureCompletable future
Completable future
 
Spring framework core
Spring framework coreSpring framework core
Spring framework core
 
Spring jdbc dao
Spring jdbc daoSpring jdbc dao
Spring jdbc dao
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 
Spring core module
Spring core moduleSpring core module
Spring core module
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 

Ähnlich wie Spring framework IOC and Dependency Injection

Spring framework
Spring frameworkSpring framework
Spring frameworkAjit Koti
 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework IntroductionAnuj Singh Rajput
 
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdfDeoDuaNaoHet
 
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 trainingshah_d_p
 
Spring bean mod02
Spring bean mod02Spring bean mod02
Spring bean mod02Guo Albert
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questionsDhiraj Champawat
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204s4al_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 JavaScriptFu Cheng
 
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 JuneDeepu S Nath
 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAOAnushaNaidu
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204ealio
 
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 2010Arun Gupta
 
More topics on Java
More topics on JavaMore topics on Java
More topics on JavaAhmed Misbah
 

Ähnlich wie 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
 
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 topics on Java
More topics on JavaMore topics on Java
More topics on Java
 

Mehr von 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
 

Kürzlich hochgeladen

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Kürzlich hochgeladen (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

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().