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

Spring Intro

Uploaded by

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

Spring Intro

Uploaded by

thirtyseven93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

JAVA FULL STACK

DEVELOPMENT PROGRAM
Spring Introduction & Dependency Injection
OUTLINE

• Overview of Spring Framework

• Dependency Injection

• Inversion of Control Container


SPRING FRAMEWORK
• Spring Framework is a Java platform that provides comprehensive infrastructure support
for developing Java applications

• Spring handles the infrastructure so you can focus on your application

• Examples of how you, as an application developer, can use the Spring platform advantage:

• Make a Java method execute in a database transaction without having to deal with
transaction APIs

• Make a local Java method a remote procedure without having to deal with remote
APIs.

• Make a local Java method a message handler without having to deal with JMS APIs.
SPRING FRAMEWORK
• Some Advantages of Spring framework

• Prede ned Templates

• i.e. Spring provides us HibernateTemplate, which means that we don't need


to write the code for exception handling, creating connection, creating
statement, committing transaction, closing connection etc. All we need to do
is writing code for executing query.

• Loose Coupling — Dependency Injection

• Easy to test

• Modularity
fi
DEPENDENCY
• You work in an organization where you and your colleagues tend to travel
a lot. Your typical travel planning routine might look like the following:

• Decide the destination, and desired arrival date and time

• Call up the airline agency and convey the necessary information to


obtain a ight booking.

• Call up the cab agency, request for a cab to be able to catch a


particular ight

• Pickup the tickets, catch the cab and be on your way


fl
fl
DEPENDENCY
• Now, what if your company suddenly changed the preferred
agencies and their contact mechanisms?

• You would be subject to the following relearning scenarios:

• The new agencies, and their new contact mechanisms (say the
new agencies offer internet based services and the way to do
the bookings is over the internet instead of over the phone)

• The typical conversational sequence through which the


necessary bookings get done (Data instead of voice)
DEPENDENCY
• Now let’s say the protocol is a little bit different.

• There is an administration department in the company

• Whenever you needed to travel an administration department interactive


telephony system simply calls you up (which in turn is hooked up to the
agencies)

• Over the phone you simply state the destination, desired arrival date and
time by responding to a programmed set of questions

• The ight reservations are made for you, the cab gets scheduled for the
appropriate time, and the tickets get delivered to you
fl
DEPENDENCY
• When class A uses some functionality of class B, then its said that class
A has a dependency of class B.
TIGHTLY COUPLING
LOOSE COUPLING
DEPENDENCY INJECTION
• What is dependency Inject?

• Separating the usage from the creation of the object

• Why dependency injection? (or Decoupling)

• It improves the testability

• It's much easier to swap other pieces of code/modules/objects/


components when the pieces are not dependent on one another

• Modularity — One module doesn't break other modules in


unpredictable ways
EASY TO TEST
IOC CONTAINER
• IoC — Inversion of Control

• It is a process whereby objects de ne their dependencies (that is, the


other objects they work with) only through constructor arguments,
arguments to a factory method, or properties that are set on the object
instance after it is constructed or returned from a factory method.

• Dependency injection is a pattern through which to implement IoC,


where the control being inverted is the setting of object's dependencies.

• Spring IoC container injects those dependencies when it creates the bean.
fi
SPRING IOC CONTAINER
• The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s
IoC container.

• The BeanFactory interface provides an advanced con guration mechanism capable of managing any type of
object:

• Lazily load beans - load beans on demand.

• Only supports XML based con guration

• The ApplicationContext is a sub-interface of BeanFactory with additional features:

• Eagerly load beans - load all beans on startup.

• Supports XML and Annotation based con guration

• Provides easier integration with Enterprise level feature such as:

• Aspect Oriented Programming


fi
fi
fi
SPRING IOC CONTAINER
• The org.springframework.context.ApplicationContext interface represents
the Spring IoC container and is responsible for instantiating,
con guring, and assembling the beans
fi
CONFIGURATION METADATA
• As the preceding diagram shows, the Spring IoC container consumes a
form of con guration metadata.

• This con guration metadata represents how you, as an application


developer, tell the Spring container to instantiate, con gure, and
assemble the objects in your application.

• Con guration can be done in two ways:

• XML

• Annotation
fi
fi
fi
fi
XML CONFIGURATION

• The id attribute is a string that identi es the individual bean de nition.

• The class attribute de nes the type of the bean and uses the fully quali ed
classname.
fi
fi
fi
fi
SPRING IOC CONTAINER
• Dependency Inject in Spring can be done in three
ways:

• Constructor

• Setter

• Field
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
CONSTRUCTOR INJECTION
• Constructor argument resolution matching occurs by using the argument’s
type

• If no potential ambiguity exists in the constructor arguments of a bean


de nition, the order in which the constructor arguments are de ned in
a bean de nition is the order in which those arguments are supplied to
the appropriate constructor when the bean is being instantiated

• The container can use type matching with simple types if you explicitly
specify the type of the constructor argument by using the type or index
or name attribute
fi
fi
fi
CONSTRUCTOR INJECTION
SETTER INJECTION
• Setter-based DI is accomplished by the container calling setter methods
on your beans after invoking a no-argument constructor or a no-
argument static factory method to instantiate your bean
CONSTRUTOR VS SETTER
INJECTION
SPRING BEAN SCOPE
Scope Description

(Default) Scopes a single bean de nition to a single object


Singleton instance for each Spring IoC container.
Scopes a single bean de nition to any number of object
Prototype instances.
Scopes a single bean de nition to the lifecycle of a single
Request HTTP request. That is, each HTTP request has its own
instance of a bean created off the back of a single bean
Scopes a single bean de de nition
nition to the lifecycle of an HTTP
Session
Session
something which is connected to Portlet applications. When
Global-Session your application works in Portlet container it is built of
some amount of portlets
fi
fi
fi
fi
fi
THE SINGLETON SCOPE
• Only one shared instance of a singleton bean is managed, and all requests for
beans with an ID or IDs that match that bean de nition result in that one
speci c bean instance being returned by the Spring container.
fi
fi
THE PROTOTYPE SCOPE
• The non-singleton prototype scope of bean deployment results in the
creation of a new bean instance every time a request for that speci c
bean is made

fi
ANNOTATION BASED
CONFIGURATION
• How to do Annotation Based Con guration

• Enable component scanning in Spring con g le

• Add the @Component Annotation to your Java


classes

• Retrieve bean from Spring container


fi
fi
fi
ENABLE SPRING
COMPONENT SCANNING
@COMPONENT
ANNOTATION
@COMPONENT
ANNOTATION
ANNOTATION DI
• The @Autowired annotation

• This annotation has the following execution paths,


listed by precedence

• Match by Type

• Match by Quali er

• Match by Name
fi
ANNOTATION DI

• Let’s take look at an example


@BEAN AND
@CONFIGURATION
• Instead of using XML to do the con guration, we
can use purely Java Con guration with

• @Bean

• @Con guration

• @ComponentScan
fi
fi
fi
BEAN LIFE CYCLE
LIFE CYCLE CALLBACKS
• Spring bean factory controls the creation and
destruction of beans. To execute some custom
code, it provides the call back methods which can
be categorized broadly in two groups

• Post-initialization call back methods

• Pre-destruction call back methods


LIFE CYCLE CALLBACKS
LIFE CYCLE CALLBACKS
ANY QUESTION?

You might also like