KEMBAR78
Spring FrameWork Tutorials Java Language | PPTX
Mahika Tutorials
Spring Framework
Tutorials
Mahika Tutorials
Using MessageSource
to get text from properties file
INTRODUCTIONTO
SPRING
Mahika Tutorials
Mahika Tutorials
 Spring is a light weight and open source framework created by Rod
Johnson in 2003.
Mahika Tutorials
Spring Application
Spring jars
 Spring framework makes the development of Java/JavaEE application
easy.
Mahika Tutorials
Dtababase Connectivity
Transaction Management
Dependency Injection
AOP
 Spring can be thought of as a framework of frameworks because it provides
support to various frameworks such as Struts, Hibernate, EJB, JSF etc.
Mahika Tutorials
Spring
JSF
EJB
Hibernate
Struts
 Spring enables you to build applications from “plain old Java objects”
(POJOs).
Mahika Tutorials
 Spring framework is said to be a non-invasive means it doesn’t force a
programmer to extend or implement their class from any predefined
class or interface given by Spring API.
Mahika Tutorials
 Spring applications are loosely coupled because of dependency
injection and Inversion of Control(IOC).
Mahika Tutorials
Employee
Address address;
Employee(){
address=newAddress(“XYZ Street ,”Pune”);
}
Employee
Address address;
Third Party
Address
instance
Tight Coupling Loose Coupling
Spring Modules
Mahika Tutorials
Core Container
Mahika Tutorials
 Core and Beans:These modules provide IOC and Dependency Injection features.
 Context:This module supports internationalization (I18N), EJB, JMS, Basic Remoting.
 SpEL: It is an extension to the EL defined in JSP. It provides support to setting and getting property values,
method invocation, accessing collections, named variables, logical and arithmetic operators, retrieval of
objects by name etc.
Data Access / Integration-
Mahika Tutorials
 These modules basically provide support to interact with the database.
 The JDBC module removes the need to do tedious JDBC coding.
 ORM module is used to tie up with ORM tools such as hibernate.
Web
Mahika Tutorials
 These modules provide support to create web application.
Test
 This module supports the testing of Spring components with JUnit orTestNG.
Mahika Tutorials
Mahika Tutorials
Dependency Injection &IOC
Dependency Injection
 Dependency Injection (DI) is a software design pattern that deals with how components get hold of their
dependencies.
 A class X has a dependency to classY, if class X uses classY as a variable.
 Java components / classes should be as independent as possible of other Java classes.
 This increases the possibility to reuse these classes and to test them independently of other classes(UnitTesting).
 Dependency injection is a style of object configuration in which an object’s fields are set by an external entity, in other
words objects are configured by an external entity.
 Dependency injection is an alternative to having the object configure itself.
Mahika Tutorials
class X
{
Y y;
……………
}
Dependency Injection and Inversion of Control
Mahika Tutorials
A
C
B
Dependency
A
C
B
Injection
Inversion of Control is a principle by which the control of objects is transferred to a
container or framework.
Dependency injection is a pattern through which IoC is implemented, where the control
being inverted is the setting of object’s dependencies.
The act of connecting objects with other objects, or “injecting” objects into other objects,
is done by container rather than by the objects themselves.
A class should not configure itself but should be configured from outside.
Mahika Tutorials
Dependency Injection and Inversion of Control
Dependency Injection and Inversion of Control
 IOC makes the code loosely coupled. In such case, there is no need to
modify the code if our logic is moved to new environment.
 IOC makes the application easy to test.
 In Spring framework, IOC container is responsible to inject the
dependency. We provide metadata to the IOC container either by XML
file or annotation
Mahika Tutorials
Dependency Injection
Spring framework provides two ways to inject dependency
By Constructor (Constructor Injection)-In the case of constructor-based dependency
injection, the container will invoke a constructor with arguments each representing a
dependency we want to set.
By Setter method (Setter Injection)-For setter-based DI, the container will call setter
methods of our class.
Mahika Tutorials
class Employee{
Address address;
Employee(){
address=newAddress(“XYZ Street”, ”Pune”, ”MH”);
}
…………………..
}
With IOC (setter Injection)Without IOC
class Employee{
Address address;
public void setAddress(Address address){
this.address=address;
}
………
}
With IOC (constructor Injection)
class Employee{
Address address;
Employee(Address address){
this.address=address;
………
}
}
Mahika Tutorials
Mahika Tutorials
BeanFactory & AplicationContext
IOC
 In Spring framework, IOC container is responsible to inject the dependencies.We provide metadata
to the IOC container either by XML file or annotation.
 The IoC container is responsible to instantiate, configure and assemble the objects.
Mahika Tutorials
BeanFactory
ApplicationContext
IOC
containers
BeanFactory
Mahika Tutorials
 Spring BeanFactory Container is the simplest container which provides basic support for DI.
 It is defined by org.springframework.beans.factory.BeanFactory interface.
 There are many implementations of BeanFactory interface . The most commonly used
BeanFactory implementation is –
org.springframework.beans.factory.xml.XmlBeanFactory
 Example-
Resource resource=new ClassPathResource(“Beans.xml");
BeanFactory factory=new XmlBeanFactory(resource);
 The Resource interface has many implementaions. Two mainly used are:
1)org.springframework.core.io.FileSystemResource :Loads the resource from underlying file
system.
Example-
BeanFactory bfObj = new XmlBeanFactory(new FileSystemResource ("c:/beansconfig.xml"));
2)org.springframework.core.io.ClassPathResource:Loads the resource from classpath.
ApplicationContext
The ApplicationContext container is Spring’s advanced container.
It is defined by org.springframework.context.ApplicationContext interface.
The ApplicationContext interface is built on top of the BeanFactory interface.
 It adds some extra functionality than BeanFactory such as simple integration with
Spring's AOP, message resource handling (for I18N), event propagation etc.
There are many implementations of ApplicationContext interface .The most
commonly used ApplicationContext implementation is –
org.springframework.context.support.ClassPathXmlApplicationContext
 Example-
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
Mahika Tutorials
Mahika Tutorials
Mahika Tutorials
Spring Autowiring
Autowiring in Spring
Mahika Tutorials
 Autowiring means injecting the object dependency implicitly.
 It internally uses setter or constructor injection.
 It works with reference only.
 Autowiring can't be used to inject primitive and string values.
 By default autowiring is disabled in spring framework.
 Autowiring can be performed by either using “autowire” attribute in <bean> or by using @Autowired annotation.
Without Autowiring
Mahika Tutorials
public class Employee {
private int id;
private String name;
private Address address;
…….
}
…….
<bean id="address" class="com.tutorials.demo.Address">
<property name="street" value="Park Street"></property>
<property name="city" value="Pune"></property>
<property name="state" value="Maharashtra"></property>
</bean>
<bean id="emp" class="com.tutorials.demo.Employee">
<property name="id" value="111"></property>
<property name="name" value="Ram"></property>
<property name="address" ref="address"></property>
</bean>
……….
With Autowiring
Mahika Tutorials
public class Employee {
private int id;
private String name;
private Address address;
…….
}
…….
<bean id="address" class="com.tutorials.demo.Address">
<property name="street" value="Park Street"></property>
<property name="city" value="Pune"></property>
<property name="state" value="Maharashtra"></property>
</bean>
<bean id="emp" class="com.tutorials.demo.Employee“ autowire=“byName">
<property name="id" value="111"></property>
<property name="name" value="Ram"></property>
</bean>
……….
Autowiring modes
Mahika Tutorials
No. Mode Description
1) no It is the default autowiring mode. It means no autowiring bydefault.
2) byName The byName mode injects the object dependency according to name of the bean. In such
case, property name and bean name must be same. It internally calls setter method.
3) byType The byType mode injects the object dependency according to type. So property name and
bean name can be different. It internally calls setter method.
4) constructor The constructor mode injects the dependency by calling the constructor of the class. It calls
the constructor having large number of parameters.
5) autodetect It is deprecated since Spring 3.
Mahika Tutorials
Mahika Tutorials
Bean Scopes
Autowiring in Spring
Mahika Tutorials
 Autowiring means injecting the object dependency implicitly.
 It internally uses setter or constructor injection.
 It works with reference only.
 Autowiring can't be used to inject primitive and string values.
 By default autowiring is disabled in spring framework.
 Autowiring can be performed by either using “autowire” attribute in <bean> or by using @Autowired annotation.
Mahika Tutorials

Spring FrameWork Tutorials Java Language

  • 1.
  • 2.
    Mahika Tutorials Using MessageSource toget text from properties file
  • 3.
  • 4.
     Spring isa light weight and open source framework created by Rod Johnson in 2003. Mahika Tutorials Spring Application Spring jars
  • 5.
     Spring frameworkmakes the development of Java/JavaEE application easy. Mahika Tutorials Dtababase Connectivity Transaction Management Dependency Injection AOP
  • 6.
     Spring canbe thought of as a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, EJB, JSF etc. Mahika Tutorials Spring JSF EJB Hibernate Struts
  • 7.
     Spring enablesyou to build applications from “plain old Java objects” (POJOs). Mahika Tutorials
  • 8.
     Spring frameworkis said to be a non-invasive means it doesn’t force a programmer to extend or implement their class from any predefined class or interface given by Spring API. Mahika Tutorials
  • 9.
     Spring applicationsare loosely coupled because of dependency injection and Inversion of Control(IOC). Mahika Tutorials Employee Address address; Employee(){ address=newAddress(“XYZ Street ,”Pune”); } Employee Address address; Third Party Address instance Tight Coupling Loose Coupling
  • 10.
  • 11.
    Core Container Mahika Tutorials Core and Beans:These modules provide IOC and Dependency Injection features.  Context:This module supports internationalization (I18N), EJB, JMS, Basic Remoting.  SpEL: It is an extension to the EL defined in JSP. It provides support to setting and getting property values, method invocation, accessing collections, named variables, logical and arithmetic operators, retrieval of objects by name etc.
  • 12.
    Data Access /Integration- Mahika Tutorials  These modules basically provide support to interact with the database.  The JDBC module removes the need to do tedious JDBC coding.  ORM module is used to tie up with ORM tools such as hibernate.
  • 13.
    Web Mahika Tutorials  Thesemodules provide support to create web application. Test  This module supports the testing of Spring components with JUnit orTestNG.
  • 14.
  • 15.
  • 16.
    Dependency Injection  DependencyInjection (DI) is a software design pattern that deals with how components get hold of their dependencies.  A class X has a dependency to classY, if class X uses classY as a variable.  Java components / classes should be as independent as possible of other Java classes.  This increases the possibility to reuse these classes and to test them independently of other classes(UnitTesting).  Dependency injection is a style of object configuration in which an object’s fields are set by an external entity, in other words objects are configured by an external entity.  Dependency injection is an alternative to having the object configure itself. Mahika Tutorials class X { Y y; …………… }
  • 17.
    Dependency Injection andInversion of Control Mahika Tutorials A C B Dependency A C B Injection
  • 18.
    Inversion of Controlis a principle by which the control of objects is transferred to a container or framework. Dependency injection is a pattern through which IoC is implemented, where the control being inverted is the setting of object’s dependencies. The act of connecting objects with other objects, or “injecting” objects into other objects, is done by container rather than by the objects themselves. A class should not configure itself but should be configured from outside. Mahika Tutorials Dependency Injection and Inversion of Control
  • 19.
    Dependency Injection andInversion of Control  IOC makes the code loosely coupled. In such case, there is no need to modify the code if our logic is moved to new environment.  IOC makes the application easy to test.  In Spring framework, IOC container is responsible to inject the dependency. We provide metadata to the IOC container either by XML file or annotation Mahika Tutorials
  • 20.
    Dependency Injection Spring frameworkprovides two ways to inject dependency By Constructor (Constructor Injection)-In the case of constructor-based dependency injection, the container will invoke a constructor with arguments each representing a dependency we want to set. By Setter method (Setter Injection)-For setter-based DI, the container will call setter methods of our class. Mahika Tutorials class Employee{ Address address; Employee(){ address=newAddress(“XYZ Street”, ”Pune”, ”MH”); } ………………….. } With IOC (setter Injection)Without IOC class Employee{ Address address; public void setAddress(Address address){ this.address=address; } ……… } With IOC (constructor Injection) class Employee{ Address address; Employee(Address address){ this.address=address; ……… } }
  • 21.
  • 22.
  • 23.
    IOC  In Springframework, IOC container is responsible to inject the dependencies.We provide metadata to the IOC container either by XML file or annotation.  The IoC container is responsible to instantiate, configure and assemble the objects. Mahika Tutorials BeanFactory ApplicationContext IOC containers
  • 24.
    BeanFactory Mahika Tutorials  SpringBeanFactory Container is the simplest container which provides basic support for DI.  It is defined by org.springframework.beans.factory.BeanFactory interface.  There are many implementations of BeanFactory interface . The most commonly used BeanFactory implementation is – org.springframework.beans.factory.xml.XmlBeanFactory  Example- Resource resource=new ClassPathResource(“Beans.xml"); BeanFactory factory=new XmlBeanFactory(resource);  The Resource interface has many implementaions. Two mainly used are: 1)org.springframework.core.io.FileSystemResource :Loads the resource from underlying file system. Example- BeanFactory bfObj = new XmlBeanFactory(new FileSystemResource ("c:/beansconfig.xml")); 2)org.springframework.core.io.ClassPathResource:Loads the resource from classpath.
  • 25.
    ApplicationContext The ApplicationContext containeris Spring’s advanced container. It is defined by org.springframework.context.ApplicationContext interface. The ApplicationContext interface is built on top of the BeanFactory interface.  It adds some extra functionality than BeanFactory such as simple integration with Spring's AOP, message resource handling (for I18N), event propagation etc. There are many implementations of ApplicationContext interface .The most commonly used ApplicationContext implementation is – org.springframework.context.support.ClassPathXmlApplicationContext  Example- ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml"); Mahika Tutorials
  • 26.
  • 27.
  • 28.
    Autowiring in Spring MahikaTutorials  Autowiring means injecting the object dependency implicitly.  It internally uses setter or constructor injection.  It works with reference only.  Autowiring can't be used to inject primitive and string values.  By default autowiring is disabled in spring framework.  Autowiring can be performed by either using “autowire” attribute in <bean> or by using @Autowired annotation.
  • 29.
    Without Autowiring Mahika Tutorials publicclass Employee { private int id; private String name; private Address address; ……. } ……. <bean id="address" class="com.tutorials.demo.Address"> <property name="street" value="Park Street"></property> <property name="city" value="Pune"></property> <property name="state" value="Maharashtra"></property> </bean> <bean id="emp" class="com.tutorials.demo.Employee"> <property name="id" value="111"></property> <property name="name" value="Ram"></property> <property name="address" ref="address"></property> </bean> ……….
  • 30.
    With Autowiring Mahika Tutorials publicclass Employee { private int id; private String name; private Address address; ……. } ……. <bean id="address" class="com.tutorials.demo.Address"> <property name="street" value="Park Street"></property> <property name="city" value="Pune"></property> <property name="state" value="Maharashtra"></property> </bean> <bean id="emp" class="com.tutorials.demo.Employee“ autowire=“byName"> <property name="id" value="111"></property> <property name="name" value="Ram"></property> </bean> ……….
  • 31.
    Autowiring modes Mahika Tutorials No.Mode Description 1) no It is the default autowiring mode. It means no autowiring bydefault. 2) byName The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method. 3) byType The byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method. 4) constructor The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters. 5) autodetect It is deprecated since Spring 3.
  • 32.
  • 33.
  • 34.
    Autowiring in Spring MahikaTutorials  Autowiring means injecting the object dependency implicitly.  It internally uses setter or constructor injection.  It works with reference only.  Autowiring can't be used to inject primitive and string values.  By default autowiring is disabled in spring framework.  Autowiring can be performed by either using “autowire” attribute in <bean> or by using @Autowired annotation.
  • 35.