Spring
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();