Title: Spring Framework Java
1Spring Framework Tutorials
2Using MessageSourceto get text from properties
file
3Introduction to Spring
4Spring is a light weight and open source
framework created by Rod Johnson in 2003.
5Spring framework makes the development of
Java/JavaEE application easy.
AOP
Dependency Injection
Dtababase Connectivity
Transaction Management
6Spring can be thought of as a framework of
frameworks because it provides support to various
frameworks such as Struts, Hibernate, EJB, JSF
etc.
7Spring enables you to build applications from
plain old Java objects (POJOs).
8Spring framework is said to be a non-invasive mean
s it doesnt force a programmer to extend or
implement their class from any predefined class
or interface given by Spring API.
9 Spring applications are loosely coupled because
of dependency injection and Inversion of
Control(IOC).
Address instance
Third Party
Loose Coupling
Tight Coupling
10(No Transcript)
11Spring Modules
12Core Container
- 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.
13Data Access / Integration-
- 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.
14Web
- These modules provide support to create web
application.
Test
- This module supports the testing of Spring
components with JUnit or TestNG.
15(No Transcript)
16Dependency Injection IOC
17Dependency 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 class Y, if class X
uses class Y 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(Unit Testing). - Dependency injection is a style of object
configuration in which an objects 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.
class X Y y
18Dependency Injection and Inversion of Control
Dependency
Injection
19Dependency Injection and Inversion of Control
- 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 objects 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.
20Dependency 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
21Dependency 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.
With IOC (setter Injection)
Without IOC
With IOC (constructor Injection)
class Employee Address address Employee()
addressnew Address(XYZ Street, Pune,
MH) ..
class Employee Address address public voi
d setAddress(Address address) this.addressaddr
ess
class Employee Address address Employee(A
ddress address) this.addressaddress
22(No Transcript)
23BeanFactory AplicationContext
24IOC
- 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.
BeanFactory
IOC containers
ApplicationContext
25BeanFactory
- 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.XmlBeanFacto
ry - Example-
- Resource resourcenew ClassPathResource(Beans.xml
") - BeanFactory factorynew XmlBeanFactory(resource)
- The Resource interface has many implementaions.
Two mainly used are1)org.springframework.core.i
o.FileSystemResource Loads the resource from
underlying file system. - Example-
- BeanFactory bfObj new XmlBeanFactory(new
FileSystemResource ("c/beansconfig.xml")) - 2)org.springframework.core.io.ClassPathResourc
eLoads the resource from classpath.
26ApplicationContext
- The ApplicationContext container is Springs
advanced container. - It is defined by org.springframework.context.Appli
cationContext 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.ClassPathXmlAp
plicationContext - Example-
- ApplicationContext contextnew
ClassPathXmlApplicationContext("Beans.xml")
27(No Transcript)
28Spring Autowiring
29Autowiring in Spring
- 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 ltbeangt or by using
_at_Autowired annotation.
30Without Autowiring
. ltbean id"address" class"com.tutorials.demo.
Address"gt ltproperty name"street" value"Park
Street"gtlt/propertygt ltproperty name"city"
value"Pune"gtlt/propertygt ltproperty name"state"
value"Maharashtra"gtlt/propertygt lt/beangt ltbean
id"emp" class"com.tutorials.demo.Employee"gt ltp
roperty name"id" value"111"gtlt/propertygt ltproper
ty name"name" value"Ram"gtlt/propertygt ltproperty
name"address" ref"address"gtlt/propertygt lt/beangt
.
public class Employee private int id
private String name private Address
address .
31With Autowiring
. ltbean id"address" class"com.tutorials.demo.
Address"gt ltproperty name"street" value"Park
Street"gtlt/propertygt ltproperty name"city"
value"Pune"gtlt/propertygt ltproperty name"state"
value"Maharashtra"gtlt/propertygt lt/beangt ltbean
id"emp" class"com.tutorials.demo.Employee
autowirebyName"gt ltproperty name"id"
value"111"gtlt/propertygt ltproperty name"name"
value"Ram"gtlt/propertygt lt/beangt .
public class Employee private int id
private String name private Address
address .
32Autowiring modes
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.
33(No Transcript)
34Bean Scopes
35Autowiring in Spring
- 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 ltbeangt or by using
_at_Autowired annotation.
36(No Transcript)