SlideShare une entreprise Scribd logo
1  sur  32
SPRING
What is a framework?
 Framework is a collection of classes of predefined code to which
can use in your program to solve some problems.
 Advantages of java framework:
 Efficiency
 Security
 Expense
 Support
 Disadvantages of java framework:
 Restriction-we cannot modify the API
 Code is public-The framework is readily available to
everyone.
 Custom built feature
Difference between java framework and SPRING
 Spring framework-will increase the efficiency of the application and will
reduce overall application development time. So we can easily develop our
project within the given time framework.
What are advantages of SPRING framework?
 Powerful open source
 Light weight application framework.
 Reduces overall complexities.
 It is framework of frameworks.
 We can integrate hibernate code with spring code
SPRING was first released on February
2003 by Rod Johnson
Why spring is so popular?
 It is the distinct division between java beans models, controllers
and view.(Spring is basically an MVC architecture)
 Spring MVC is very flexible as it make use of interfaces.
*no restriction come over here.
*we can use our implementation as per our requirement.
 Spring is a:
Application framework.
Light weighted.
Layered architecture.
Loose coupling(less dependency of one module to another module)
Easy integration with ORM technologies(we can integrate hibernate)
What is the difference between spring and hibernate?
 Spring and Hibernate are two different things, Spring has several components
like Dependency Injection, Spring MVC, Spring ORM, Spring Rest API.
 So Spring ORM and Hibernate are kind of similar, they both deal with the
object relation model, which means they deal with connection java objects to
data base entities.
 Loose coupling-The dependent objects are written outside your java code so
if you need to change the dependent object you just need to change it in one
location(xml configuration file).
 Hibernate- It provides with ORM where in you are able to perform database
transaction is the form of objects mapped to corresponding table, using
configuration xml files.
 Spring, on the other hand is a framework that helps you follow the MVC
architecture in an effective and efficient way.
SPRING ARCHITECTURE
 The Spring framework is a layered architecture which consists of several
modules.
 All modules are built on the top of its core container.
 These modules provide everything that a developer may need for use in
the enterprise application development.
 He is always free to choose what features he needs and eliminate the
modules which are of no use.
 It's modular architecture enables integration with other frameworks.
Spring Architecture continues….
 The Core Module: Provides the Dependency Injection (DI) feature which is the
basic concept of the Spring framework. This module contains the BeanFactory, an
implementation of Factory Pattern which creates the bean as per the configurations
provided by the developer in an XML file. Core Container
Core: provides the fundamental parts of Spring framework.
Bean: It’s an object. Provides Bean Factory.
 BeanFactory, an implementation of Factory Pattern which creates the bean
as per the configurations provided by the developer in an XML file
Context: It's a medium to access any objects defined and configured.
SpEL-Spring Expression Language:it provides a powerful expression
language.
Data access
JDBC:provides a jdbc abstraction layer.
ORM:provides integration layers for popular object-relational
mapping API’s.
OXM: Object XML Mapping. Provides an abstraction layer.
JMS(Java Message Service):contains features for producing and
consuming messages.
Transactions:support programmatic and declarative transaction
management.
Web Module: Spring comes with MVC framework which eases
the task of developing web applications. It also integrates well with the
most popular MVC frameworks like Struts, JSF, etc.
Web Socket:provide support for web socket based a two way
communication between client and server.
Servlet:it is the core part of enterprise edition.Contains Spring
MVC implementation for web application.
Web:provide basic web-oriented integration features.
Portlet: provides MVC implementation. It is a stand alone
application.
Miscellaneous
AOP: provides an Aspect-Oriented Programming
implementation. It aims at declarative transaction management
which is easier to maintain.
Aspects: provides integration with aspects.
Instrumentation:provides class instrumentation support and
class loader implementations.
Messaging:provide support for messaging.
Test:support Support the testing of spring components(unit
testing)
Spring IoC Containers
 The Spring container is at the core of the Spring Framework.
 The container will create the objects, wire them together, configure them,
and manage their complete life cycle from creation till destruction.
 The Spring container uses DI(Dependency Injection) to manage the
components that make up an application. (Dependency injection-Just direct object
its dependent object and container will bind them at runtime)
 Dependency Injection (DI) is a design pattern that removes the
dependency from the programming code so that it can be easy to
manage and test the application. Dependency Injection makes our
programming code loosely coupled
 Spring provides the following two distinct types of containers.
Spring BeanFactory Container
This is the simplest container and is defined by
the org.springframework.beans.factory.BeanFactory interface.
Spring ApplicationContext Container
This container adds more enterprise-specific functionality.
This container is defined by
the org.springframework.context.ApplicationContext interface.
 The ApplicationContext container includes all functionality of
the BeanFactorycontainer, so it is generally recommended
over BeanFactory.
 BeanFactory can still be used for lightweight applications like mobile
devices or applet-based applications where data volume and speed is
significant.
BEAN SCOPE &METHOD INJECTION
 Spring Bean Definition: The objects that form the backbone of your
application and that are managed by the Spring IoC container are called beans.
 A bean is an object that is instantiated, assembled, and otherwise managed by a
Spring IoC container.
 When defining a <bean> you have the option of declaring a scope for that bean.
The Spring Framework supports the following five scopes
Singleton-Only one singleton instance will be created(default)
Prototype-Creates any number of instance from a single bean configuration.
Below three are connected with web based application Only valid in the
context of a web-aware Spring ApplicationContext
Request- This scopes a bean definition to an HTTP request.
Session-This scopes a bean definition to an HTTP session.
Global Session-This scopes a bean definition to a global HTTP session.
<bean name=“student” class=“Student” scope=“prototype”/>
Singleton Scope
 Default scope-if we don’t define anything in scope spring construct
automatically to Singleton Scope
 With this what does it mean that, even though you call an instance of the
bean using spring framework for one or more times all the time only one
instance will return.
Single student
instance
ctx.getBean(“student”)
Spring
Container
Prototype Scope
Spring
Container
Multiple
beans
For ‘n’ number of request using ctx.getBean(“student”) you get
‘n’ number of instance to the application that means every call you
get new instance of bean.
Method Injection-Method Replace
class MobileStore
{
public String buyMobile()
{
return “Bought a Mobile
Phone”;
}
}
class MobileStoreReplacer implements
MethodReplacer
{
public Object reimplement(Object
obj,Method method,Object[]args)
throws Throwable
{
return “Bought an iPhone”;
}
}
<bean id=“mobileStore” class “MobileStore”>
<replace-method name=“buyMobile”
replacer=“mobileStoreReplacer”/></bean>
<bean id=“mobileStoreReplacer” class
“MobileStoreReplacer”/>
On run time when ever you get a bean of mobile Store through
spring framework and call buyMobile.Spring understands it
need to replace buyMobile to MobileStoreReplacer.
Spring - Bean Definition Inheritance
 A bean definition can contain a lot of configuration information, including
constructor arguments, property values and so on.
 Spring Bean definition inheritance has nothing to do with Java class
inheritance but the inheritance concept is same.
 Following is the configuration file Beans.xml where we defined "helloWorld"
bean which has two properties message1 and message2. Next "helloIndia"
bean has been defined as a child of "helloWorld" bean by using parent
attribute. The child bean inherits message2 property as is, and overrides
message1 property and introduces one more property message3.
Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld">
<property name = "message1" value = "Hello World!"/>
<property name = "message2" value = "Hello Second World!"/>
</bean>
<bean id ="helloIndia" class = "com.tutorialspoint.HelloIndia"
parent = "helloWorld">
<property name = "message1" value = "Hello India!"/>
<property name = "message3" value = "Namaste India!"/>
</bean> </beans>
HelloWorld.java
public class HelloWorld
{ private String message1;
private String message2;
public void setMessage1(String message)
{
this.message1 = message;
}
public void setMessage2(String message)
{
this.message2 = message;
}
public void getMessage1()
{ System.out.println("World Message1 : " + message1);
}
public void getMessage2()
{
System.out.println("World Message2 : " + message2);} }
HelloIndia.java
public class HelloIndia
{
private String message1;
private String message2;
private String message3;
public void setMessage1(String message)
{
this.message1 = message;
}
public void setMessage2(String message)
{
this.message2 = message;
}
public void setMessage3(String message)
{ this.message3 = message; } public void getMessage1(){ System.out.println
("India Message1 : " + message1);
} public void getMessage2(){ System.out.println("India Message2 : " + message2);
} public void getMessage3(){ System.out.println("India Message3 : " + message3); } }
MainApp.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp
{ public static void main(String[] args)
{ ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.getMessage1();
objA.getMessage2();
HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
objB.getMessage1();
objB.getMessage2();
objB.getMessage3(); } }
output
World Message1 : Hello World!
World Message2 : Hello Second World!
India Message1 : Hello India!
India Message2 : Hello Second World!
India Message3 : Namaste India!
THANK YOU

Contenu connexe

Tendances

Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
Ram132
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
Guo Albert
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
John Slick
 
24 collections framework interview questions
24 collections framework interview questions24 collections framework interview questions
24 collections framework interview questions
Arun Vasanth
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Hibernate Interview Questions
Hibernate Interview QuestionsHibernate Interview Questions
Hibernate Interview Questions
Syed Shahul
 

Tendances (20)

Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Hibernate
HibernateHibernate
Hibernate
 
Hibernate architecture
Hibernate architectureHibernate architecture
Hibernate architecture
 
Introduction to Hibernate
Introduction to HibernateIntroduction to Hibernate
Introduction to Hibernate
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Hibernate Basic Concepts - Presentation
Hibernate Basic Concepts - PresentationHibernate Basic Concepts - Presentation
Hibernate Basic Concepts - Presentation
 
Hibernate in Nutshell
Hibernate in NutshellHibernate in Nutshell
Hibernate in Nutshell
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
Introduction To Hibernate
Introduction To HibernateIntroduction To Hibernate
Introduction To Hibernate
 
24 collections framework interview questions
24 collections framework interview questions24 collections framework interview questions
24 collections framework interview questions
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Hibernate Interview Questions
Hibernate Interview QuestionsHibernate Interview Questions
Hibernate Interview Questions
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
Hibernate An Introduction
Hibernate An IntroductionHibernate An Introduction
Hibernate An Introduction
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
 

Similaire à Spring (1)

Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptx
NourhanTarek23
 
Spring Fa Qs
Spring Fa QsSpring Fa Qs
Spring Fa Qs
jbashask
 

Similaire à Spring (1) (20)

Spring framework-tutorial
Spring framework-tutorialSpring framework-tutorial
Spring framework-tutorial
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
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
 
Spring 2
Spring 2Spring 2
Spring 2
 
Spring notes
Spring notesSpring notes
Spring notes
 
Spring User Guide
Spring User GuideSpring User Guide
Spring User Guide
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptx
 
spring framework ppt by Rohit malav
spring framework ppt by Rohit malavspring framework ppt by Rohit malav
spring framework ppt by Rohit malav
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
 
Spring framework Introduction
Spring framework  IntroductionSpring framework  Introduction
Spring framework Introduction
 
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 Fa Qs
Spring Fa QsSpring Fa Qs
Spring Fa Qs
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
Spring Framework Tutorial | VirtualNuggets
Spring Framework Tutorial | VirtualNuggetsSpring Framework Tutorial | VirtualNuggets
Spring Framework Tutorial | VirtualNuggets
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Java spring ppt
Java spring pptJava spring ppt
Java spring ppt
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2
 

Dernier

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Dernier (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
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
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Spring (1)

  • 2. What is a framework?  Framework is a collection of classes of predefined code to which can use in your program to solve some problems.  Advantages of java framework:  Efficiency  Security  Expense  Support
  • 3.  Disadvantages of java framework:  Restriction-we cannot modify the API  Code is public-The framework is readily available to everyone.  Custom built feature
  • 4. Difference between java framework and SPRING  Spring framework-will increase the efficiency of the application and will reduce overall application development time. So we can easily develop our project within the given time framework. What are advantages of SPRING framework?  Powerful open source  Light weight application framework.  Reduces overall complexities.  It is framework of frameworks.  We can integrate hibernate code with spring code
  • 5. SPRING was first released on February 2003 by Rod Johnson Why spring is so popular?  It is the distinct division between java beans models, controllers and view.(Spring is basically an MVC architecture)  Spring MVC is very flexible as it make use of interfaces. *no restriction come over here. *we can use our implementation as per our requirement.
  • 6.  Spring is a: Application framework. Light weighted. Layered architecture. Loose coupling(less dependency of one module to another module) Easy integration with ORM technologies(we can integrate hibernate)
  • 7. What is the difference between spring and hibernate?  Spring and Hibernate are two different things, Spring has several components like Dependency Injection, Spring MVC, Spring ORM, Spring Rest API.  So Spring ORM and Hibernate are kind of similar, they both deal with the object relation model, which means they deal with connection java objects to data base entities.  Loose coupling-The dependent objects are written outside your java code so if you need to change the dependent object you just need to change it in one location(xml configuration file).  Hibernate- It provides with ORM where in you are able to perform database transaction is the form of objects mapped to corresponding table, using configuration xml files.  Spring, on the other hand is a framework that helps you follow the MVC architecture in an effective and efficient way.
  • 9.  The Spring framework is a layered architecture which consists of several modules.  All modules are built on the top of its core container.  These modules provide everything that a developer may need for use in the enterprise application development.  He is always free to choose what features he needs and eliminate the modules which are of no use.  It's modular architecture enables integration with other frameworks.
  • 10. Spring Architecture continues….  The Core Module: Provides the Dependency Injection (DI) feature which is the basic concept of the Spring framework. This module contains the BeanFactory, an implementation of Factory Pattern which creates the bean as per the configurations provided by the developer in an XML file. Core Container Core: provides the fundamental parts of Spring framework. Bean: It’s an object. Provides Bean Factory.  BeanFactory, an implementation of Factory Pattern which creates the bean as per the configurations provided by the developer in an XML file Context: It's a medium to access any objects defined and configured. SpEL-Spring Expression Language:it provides a powerful expression language.
  • 11. Data access JDBC:provides a jdbc abstraction layer. ORM:provides integration layers for popular object-relational mapping API’s. OXM: Object XML Mapping. Provides an abstraction layer. JMS(Java Message Service):contains features for producing and consuming messages. Transactions:support programmatic and declarative transaction management.
  • 12. Web Module: Spring comes with MVC framework which eases the task of developing web applications. It also integrates well with the most popular MVC frameworks like Struts, JSF, etc. Web Socket:provide support for web socket based a two way communication between client and server. Servlet:it is the core part of enterprise edition.Contains Spring MVC implementation for web application. Web:provide basic web-oriented integration features. Portlet: provides MVC implementation. It is a stand alone application.
  • 13. Miscellaneous AOP: provides an Aspect-Oriented Programming implementation. It aims at declarative transaction management which is easier to maintain. Aspects: provides integration with aspects. Instrumentation:provides class instrumentation support and class loader implementations. Messaging:provide support for messaging. Test:support Support the testing of spring components(unit testing)
  • 14. Spring IoC Containers  The Spring container is at the core of the Spring Framework.  The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction.  The Spring container uses DI(Dependency Injection) to manage the components that make up an application. (Dependency injection-Just direct object its dependent object and container will bind them at runtime)  Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the application. Dependency Injection makes our programming code loosely coupled
  • 15.  Spring provides the following two distinct types of containers. Spring BeanFactory Container This is the simplest container and is defined by the org.springframework.beans.factory.BeanFactory interface. Spring ApplicationContext Container This container adds more enterprise-specific functionality. This container is defined by the org.springframework.context.ApplicationContext interface.
  • 16.  The ApplicationContext container includes all functionality of the BeanFactorycontainer, so it is generally recommended over BeanFactory.  BeanFactory can still be used for lightweight applications like mobile devices or applet-based applications where data volume and speed is significant.
  • 17. BEAN SCOPE &METHOD INJECTION  Spring Bean Definition: The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.  A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.  When defining a <bean> you have the option of declaring a scope for that bean.
  • 18. The Spring Framework supports the following five scopes Singleton-Only one singleton instance will be created(default) Prototype-Creates any number of instance from a single bean configuration. Below three are connected with web based application Only valid in the context of a web-aware Spring ApplicationContext Request- This scopes a bean definition to an HTTP request. Session-This scopes a bean definition to an HTTP session. Global Session-This scopes a bean definition to a global HTTP session. <bean name=“student” class=“Student” scope=“prototype”/>
  • 19. Singleton Scope  Default scope-if we don’t define anything in scope spring construct automatically to Singleton Scope  With this what does it mean that, even though you call an instance of the bean using spring framework for one or more times all the time only one instance will return. Single student instance ctx.getBean(“student”) Spring Container
  • 20. Prototype Scope Spring Container Multiple beans For ‘n’ number of request using ctx.getBean(“student”) you get ‘n’ number of instance to the application that means every call you get new instance of bean.
  • 21. Method Injection-Method Replace class MobileStore { public String buyMobile() { return “Bought a Mobile Phone”; } } class MobileStoreReplacer implements MethodReplacer { public Object reimplement(Object obj,Method method,Object[]args) throws Throwable { return “Bought an iPhone”; } } <bean id=“mobileStore” class “MobileStore”> <replace-method name=“buyMobile” replacer=“mobileStoreReplacer”/></bean> <bean id=“mobileStoreReplacer” class “MobileStoreReplacer”/>
  • 22. On run time when ever you get a bean of mobile Store through spring framework and call buyMobile.Spring understands it need to replace buyMobile to MobileStoreReplacer.
  • 23.
  • 24.
  • 25.
  • 26. Spring - Bean Definition Inheritance  A bean definition can contain a lot of configuration information, including constructor arguments, property values and so on.  Spring Bean definition inheritance has nothing to do with Java class inheritance but the inheritance concept is same.  Following is the configuration file Beans.xml where we defined "helloWorld" bean which has two properties message1 and message2. Next "helloIndia" bean has been defined as a child of "helloWorld" bean by using parent attribute. The child bean inherits message2 property as is, and overrides message1 property and introduces one more property message3.
  • 27. Beans.xml <?xml version = "1.0" encoding = "UTF-8"?> <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld"> <property name = "message1" value = "Hello World!"/> <property name = "message2" value = "Hello Second World!"/> </bean> <bean id ="helloIndia" class = "com.tutorialspoint.HelloIndia" parent = "helloWorld"> <property name = "message1" value = "Hello India!"/> <property name = "message3" value = "Namaste India!"/> </bean> </beans>
  • 28. HelloWorld.java public class HelloWorld { private String message1; private String message2; public void setMessage1(String message) { this.message1 = message; } public void setMessage2(String message) { this.message2 = message; } public void getMessage1() { System.out.println("World Message1 : " + message1); } public void getMessage2() { System.out.println("World Message2 : " + message2);} }
  • 29. HelloIndia.java public class HelloIndia { private String message1; private String message2; private String message3; public void setMessage1(String message) { this.message1 = message; } public void setMessage2(String message) { this.message2 = message; } public void setMessage3(String message) { this.message3 = message; } public void getMessage1(){ System.out.println ("India Message1 : " + message1); } public void getMessage2(){ System.out.println("India Message2 : " + message2); } public void getMessage3(){ System.out.println("India Message3 : " + message3); } }
  • 30. MainApp.java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld objA = (HelloWorld) context.getBean("helloWorld"); objA.getMessage1(); objA.getMessage2(); HelloIndia objB = (HelloIndia) context.getBean("helloIndia"); objB.getMessage1(); objB.getMessage2(); objB.getMessage3(); } }
  • 31. output World Message1 : Hello World! World Message2 : Hello Second World! India Message1 : Hello India! India Message2 : Hello Second World! India Message3 : Namaste India!