0% found this document useful (0 votes)
5 views

Spring

Uploaded by

Suraj Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Spring

Uploaded by

Suraj Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Spring

Phase II
What is Spring?
• It is a framework of frameworks.
• Spring is a Dependency Injection framework to make java application
loosely coupled.
• Spring framework makes the easy development of JavaEE application.
• It was developed by Rod Johnson in 2003.
Framework
• Framework is like a skeleton, a structure around which the
components of our application is created.

• Frameworks are built around the design pattern & they consist of
structural components which can be modified by the developers and
contains multiple pre defined codes they can use it as the base to
build something to their own.
Dependency Injection
• Its is a design pattern.
• We build objects and these objects are dependent on other objects.
• In order to develop applications in java we use various classes.
Example: Dependency Injection
class Abc class Xyz
{ {
Xyz ob; public void doWork()
public void doWork() {
{ }
} }
}

Inversion of Control(IoC)
• In this class Xyz is able to create its own object and will able to inject
this object in Abc class with the help of spring framework.
• The whole technique is called the Inversion of control means that the
object creation control is now is not in class control rather we gave it
to spring.
Normally, a class depends on
another class to do some work, for
example:
• public class ClientA {
• ServiceB service;

• public void doSomething() {
• String info = service.getInfo();
• }
•}
Here, class ClientA uses
class ServiceB which is written as
below, for example:
• public class ServiceB {
• public String getInfo() {
• return "ServiceB’s Info";
• }
•}
Drawbacks
• The code is inflexible - it’s hard to maintain and extend as when a
class permanently depends on another class, change to the
depending class my require change to the dependent class. And it’s
impossible to change the depending class later without updating and
re-compiling the code.
• The code is hard for unit testing because when you want to test only
the functionalities of a class, you have to test other depending classes
as well.
• The code is hard for reuse because the classes are tightly coupled.
First, interfaces are used to define
the types of the classes.
• public interface Client {
• void doSomething();
•}

• public interface Service {
• String getInfo();
•}
Then the ServiceB class becomes an
implementation of Service as below:
• public class ServiceB implements Service {

• @Override
• public String getInfo() {
• return "ServiceB's Info";
• }
•}
Then it’s possible to have different implementations
of Service, for example ServiceC and ServiceD:
• public class ServiceC implements Service {
• @Override
• public String getInfo() {
• return "ServiceC's Info";
• }
• }
• public class ServiceD implements Service {
• @Override
• public String getInfo() {
• return "ServiceD's Info";
• }
• }
And the class ClientA is now implementing
the Client interface and it uses
the Service interface instead of a concrete class -
the actual Service’s implementation is “injected”
• public class ClientA implements Client {
• Service service;
• public ClientA(Service service) {
• this.service = service;
• }
• @Override
• public void doSomething() {
• String info = service.getInfo();
• }
•}
the dependency injection container or
framework is now responsible for creating
that instance and inject it to the
class ClientA via its constructor.
• Service service = new ServiceB();
• Client client = new ClientA(service);
• client.doSomething();

• Here, an implementation of Service is ServiceB is created and passed


to ClientA, which is not aware of the actual implementation it is
using. ClientA only knows that the injected object is of type Service.
Spring & JEE
• When we develop the applications based on J2EE we have different
types of layers:
• UI Layer: To handle request
• Business/Services Layer: To handle business logic
• Data Access Layer: to access database
Strut/JSF
Spring MVC
• ProductController: capable of
accepting request only.(or send
Security
request to next layer) Inject
Transaction • ProductService: Use the business
Management
ProductDao dao=new logic.
ProductDao Inject
Spring JDBC • ProductDao: contacts the
Spring ORM
database
Spring Modules/Spring Framework
• Spring Core & Beans: It includes the fundamental part of the
framework & provides IoC and dependency injection.
• 1. Core
• 2. Beans
• 3. Context: Event propagation, Resource loading, transparent creation
of context & also provides the features of J2EE.
• 4. spEL: Spring expression language: Query manipulate object/object
graph at run time.(Extension of expression language in JSP)
Next level of modules
• AOP: Aspect oriented Programming
• It allows us to define method interceptors(suppose we want to do
anything before or after methods) & point cuts so that we can
decouple our code.
• Aspect: By separating application business logic from system services.
• Instrumentation: for server applications.
• Messaging: we can map our messages with the methods by using
some annotations.
Data Access/Integration layer
• All related to database. (it’s a API)
• JDBC: Spring JDBC provides extra abstraction layer that removes the
need of tedious old JDBC code.
• ORM: it is an integration layer through this we can integrate another
ORM tool in our project, like Hybernate.
• JMS: Java messaging Services, provide producing and consuming
messages
• OXM: Object XML mapping like Xstream.
Web Module
• Use in creating web projects, provides web oriented integration
features, like file uploading on server, also contains http client. (MVC
also come under this)
• Web
• Servlet
• Portlet
• WebSocket
Test Module
• With the help of this we can perform unit testing and integration
testing. Like JUnit, testNG.
• It also provides lots of mock objects through which we can test our
code by keeping it in isolation.
Disadvantages of framework
• Restrictions: Frameworks fundamental behavior cannot be change,
we have to work with in the framework itself, if have to create
something outside then we have do it manually.
• Custom Features: When we use custom features it is very likely that
we have to use according to the framework standards which may
differ with the original concepts that we are trying to deploy.
• Code is public: since this framework is readily available to everyone it
is also offered to people bad intentions. Anyone can study the
loopholes of one specific framework and discover flows and apply
against us.
Spring IoC Container
• Object creation and hold these objects in memory.
• Inject one object in another object.
• It maintains the life cycle of the object, from the object creation to
the object destroy.
Dependency Injection
IoC Container:
manage both
class Student class Address
{ {
int id; String street;
String name; String city;
Address address; String state;
} String country;
}
Dependency Injection can be done
in 2 ways
• Using Setter Injection
• Using Constructor Injection
Setter Injection
class Student class Address
{ {
id.name.address street.city.state.country
setId(id){} setStreet(street)
setName(name){} setState(state)
setAddress(address){} setCountry(country)
} }
• At the time of object creation Ioc container will call the setter method
and set all the content of address that is street, city, state and
country.
• same in the case of student.
Constructor Injection
class Student class Address
{ {
Student(id,name,address) String street.city.state.country;
{ Address(street,city,state,country)
{
}
} }
}
• In this case at the time of creating object IoC container will
automatically use constructor method
Configuration File
• Spring do have its own configuration file like hibernate configuration
file.
• It’s a xml file.
• To inform IoC container that how many beans or classes we have, its
name and on which they depend.
• Define: where we declare beans and its dependency.
Data Types (Dependencies)
• Types of dependency it can inject (IoC can handle)
• 1. Primitive data types
• Byte, short, char, int, float, double, long, Boolean
• 2. Collection Type
• List, Set, Map and Properties
• 3. Reference Type(User defined data type)
• Other class object

You might also like