You are on page 1of 8

Spring in simple

What is Spring?
The Spring Framework is an open source application framework created to simplify the
development of enterprise Java software. The framework achieves this goal by providing
developers with a component model and a set of simplified and consistent A!"s that
effectively insulate developers from the comple#ity and error$prone boilerplate code re%uired
to create comple# applications.
The framework has evolved into roughly &' modules that can be grouped into ( basic
functional areas.
Why use Spring?
)uality*From the overall design of the modules+ packages+ class structures and A!s to the
implementation and test coverage of the source code+ the Spring Framework is a great
e#ample of high$%uality open source software.
,odularity*As we mentioned earlier+ the framework has evolved into roughly &' modules+
giving developers the freedom to choose which parts of the framework to use in their
applications without the need to include the entire framework.
romotes best practices*Spring"s plain old Java ob-ect ./J/0$based programming model
promotes decoupled component models+ unit testing+ and other best practices.
,odest learning curve*1ue to the consistency and simplicity of the A!s+ Spring isn"t hard to
learn. As we make our way through the framework+ you"ll see that common patterns emerge.
lus+ hundreds of resources online and in print are at your disposal+ including message
boards where the core developers often participate.
opularity*As evidenced by myriad publications+ websites+ and -ob postings+ the Spring
Framework is almost ubi%uitous.
What is !nversion of 2ontrol?
!nversion of control is based on the phenomenon that says whenever dependency between
two classes .say class A and class 30 now if A needs to access the methods of 3 then
following our traditional approach+ we go ahead and create an ob-ect of 3 in A. 3ut as the fact
of matter 3 is there to serve A but not A serving 3 creating ob-ects for 3. So in !/2 the class
3 will try to in-ect its ob-ect into A rather than A creating ob-ect of 3. so the control is reversed
during the run time now spring container takes care which class ob-ect to in-ect into other
class. So here class 3 in-ect its dependency in class A using constructor or setter.
What is 1ependency !n-ection?
1ependency in-ection is a software design pattern in which one or more dependencies .or
services0 are in-ected+ or passed by reference+ into a dependent ob-ect .or client0 and are
made part of the client4s state. The pattern separates the creation of a client4s dependencies
from its own behavior+ which allows program designs to be loosely coupled and to follow the
dependency inversion and single responsibility principles. !t directly contrasts the service
locator pattern+ which allows clients to know about the system they use to find dependencies.
What are the types of 1ependency !n-ection?
2onstruction !n-ection*we have our /J/ and a basic configuration for the message bean+
we can introduce our first dependency in-ection e#ample. Through the Spring beans 5,6 file
you can configure your bean to initiali7e with an argument for the constructor+ and then assign
the arguments. Spring essentially 8in-ects9 the argument into your bean. This is referred to as
constructor in-ection.
package spring.test
public class 2onst!n-ec1emo:
public String string;<
public Test2lass test2lass<
public 2onst!n-ec1emo.String string;+ Test2lass test2lass0:
this.string;=string;<
this.test2lass=test2lass<
>
>
this is sample constructor. 3ean.#ml file
?3ean name=9construction!n-ectTest9 class=9spring.test.2onst!n-ec1emo9@
?constructor$arg type=9String9 value=9Aello Spring9B@
?constructor$arg type=9Testt2lass9 ref=9test2lassCeference9B@
?B3ean@
?3ean name=9test2lassCeference9 class=9spring.test.Test2lass@
D.
D.
2onstruction arguments can be in-ected different ways
?constructor$arg name=9string;9 value =9Aello Spring9B@
?constructor$arg type=9String9 value=9Aello Spring9B@
?constructor$arg inde#=9inde#'9 value=9Aello Spring9B@
?constructor$arg type=9Testt2lass9 ref=9test2lassCeference9B@

Setter !n-ection*Setter in-ection uses the setE methods in a class file to garner property
names that are configurable in the spring 5,6 config.
package spring.test
public class 2onst!n-ec1emo:
public String string;<
public Test2lass test2lass<
public void setString;.String string;0:
this.string;=string;<
>
public void setString;.Test2lass test2lass0:
this.test2lass=test2lass<
>
>
this is sample po-o.
3ean.#ml file
?3ean name=9construction!n-ectTest9 class=9spring.test.2onst!n-ec1emo9@
?property name=Fstring;F value=FSpring is fun.F B@
?property name=Ftest2lassF ref=Ftest2lassCeference.F B@
?B3ean@
?3ean name=9test2lassCeference9 class=9spring.test.Test2lass@
D.
D.
?B3ean@
What are the 3ean scopes?
Singleton*scope is the default scope for beans declared in Spring. These singletons are
different than Java classes that implement the singleton design pattern. 1eclaring a singleton
bean in Spring ensures that only one instance e#ists on a per$container basis+ whereas an
instance of a class that implements the singleton design pattern will be the only one available
on a per$classloader basis. As you re%uest singleton beans from the container+ Spring will
create instances and cache them if they haven"t been created otherwise+ Spring will return
already$e#isting instances from its cache. Therefore+ singleton beans in Spring often don"t
maintain state because they"re usually shared among multiple threads .such as in servlet
environments0.
rototype$scoped beans are created every time they"re re%uested via Application2onte#t"s
get3ean.0 method+ or each time they"re in-ected into other beans. 6et"s consider the second
case. The following code in-ects a bean with prototype scope into another bean with default
singleton scope.
?bean id=Fconstruction!n-ectTestF class=Fspring.test.2onstruction!n-ectTestF
scope=FprototypeFB@
Ce%uest+ Session and Global Session Scope*The last three scopes*re%uest+ session and
global session*are useful only in the conte#t of web applications. !t doesn"t matter what web
framework you"re using< they function identically across all of them.
Ce%uest$scoped beans are created each time an ATT re%uest makes its way into a servlet
resource that is in-ected with one. Similar to re%uest$scoped variables in servlets+ these beans
are safe to change and work with because the servlet specification dictates one thread per
ATT re%uest.
Session$scoped beans are confined to the scope of standard session$scoped variables.
These+ like re%uest$scoped beans+ are safe to modify and work with because although their
access isn"t restricted to the same+ single thread+ it"s restricted to one thread at a time+ which
is tied to the current session of the client making the re%uests.
Global sessionHscoped beans are applicable only in the conte#t of portlet applications. 6ike
session$scoped beans+ they e#ist throughout an entire session+ but they"re shared among all
the portlets in a complete portlet web application+ whereas session scoped beans are created
and managed for each individual portlet.
There are a few prere%uisites to employing beans with these scopes. First+ if you"re using a
framework other than Spring Web ,I2+ you have to register a Ce%uest$ 2onte#t6istener in
the servlet deployment descriptor .web.#ml0J
Figure ;.K rototype$scoped beans are instantiated
?web$app@
...
?listener@
?listener$class@org.springframework.web.conte#t.re%uest.Ce%uest2onte#t6istener?Blistener$
class@
?Blistener@
...
?Bweb$app@
What is Autowiring?
LAutowired annotation can be applied to constructors or fields and allows you to wire
relationships by type+ without having to e#plicitly set them up in the 5,6 configuration. After
that+ we"ll e#plore the stereotype annotations that come with the frameworkJ L2omponent+
LCepository+ LService+ and L2ontroller. With LAutowired and the stereotype annotations+
you can enable component scanning for dependencies using annotations and minimal 5,6
configuration. We use autowiring and component scanning
public class AccountService :
LAutowired
private Account1ao account1ao<
...
>
What is the 3ean 6ife 2ycle?
2ontainer 6ife 2ycle.
3ean life cycleJ A Spring 3ean represents a /J/ component performing some useful
operation. All Spring 3eans reside within a Spring 2ontainer also known as !/2 2ontainer.
The Spring Framework is transparent and thereby hides most of the comple# infrastructure
and the communication that happens between the Spring 2ontainer and the Spring 3eans.
This section lists the se%uence of activities that will take place between the time of 3ean
!nstantiation and hand over of the 3ean reference to the 2lient Application.
The 3ean 2ontainer finds the definition of the Spring 3ean in the 2onfiguration file. The 3ean
2ontainer creates an instance of the 3ean using Java Ceflection A!.
!f any properties are mentioned+ then they are also applied. !f the property itself is a 3ean+
then it is resolved and set.
!f the 3ean class implements the 3eanMameAware interface+ then the set3eanMame.0
method will be called by passing the name of the 3ean.
!f the 3ean class implements the 3ean2lass6oaderAware interface+ then the method
set3ean2lass6oader.0 method will be called by passing an instance of the 2lass6oader
ob-ect that loaded this bean.
!f the 3ean class implements the 3eanFactoryAware interface+ then the method
set3eanFactory.0 will be called by passing an instance of 3eanFactory ob-ect.
!f there are any 3eanostrocessors ob-ect associated with the 3eanFactory that loaded the
3ean+ then the method postrocess3efore!nitiali7ation.0 will be called even before the
properties for the 3ean are set.
!f the 3ean class implements the !nitiali7ing3ean interface+ then the method
afterropertiesSet.0 will be called once all the 3ean properties defined in the 2onfiguration file
are set.
!f the 3ean definition in the 2onfiguration file contains a 4init$method4 attribute+ then the value
for the attribute will be resolved to a method name in the 3ean class and that method will be
called.
The postrocessAfter!nitiali7ation.0 method will be called if there are any 3ean ost
rocessors attached for the 3ean Factory ob-ect.
!f the 3ean class implements the 1isposable3ean interface+ then the method destroy.0 will be
called when the Application no longer needs the bean reference.
!f the 3ean definition in the 2onfiguration file contains a 4destroy$method4 attribute+ then the
corresponding method definition in the 3ean class will be called.
3ean 6ife 2ycle N#ample
3eanFactory 2ontainer This is the simplest container providing basic support for 1! and
defined by the org.springframework.beans.factory.3eanFactory interface. The 3eanFactory
and related interfaces+ such as 3eanFactoryAware+ !nitiali7ing3ean+ 1isposable3ean+ are still
present in Spring for the purposes of backward compatibility with the large number of third$
party frameworks that integrate with Spring.
Application2onte#t 2ontainer This container adds more enterprise$specific functionality such
as the ability to resolve te#tual messages from a properties file and the ability to publish
application events to interested event listeners. This container is defined by the
org.springframework.conte#t.Application2onte#t interface.
Spring Application2onte#t 2ontainer The Application 2onte#t is spring4s more advanced
container. Similar to 3eanFactory it can load bean definitions+ wire beans together and
dispense beans upon re%uest. Additionally it adds more enterprise$specific functionality such
as the ability to resolve te#tual messages from a properties file and the ability to publish
application events to interested event listeners. This container is defined by the
org.springframework.conte#t.Application2onte#t interface.
The Application2onte#t includes all functionality of the 3eanFactory+ it is generally
recommended over the 3eanFactory. 3eanFactory can still be used for lightweight
applications like mobile devices or applet based applications. The most commonly used
Application2onte#t implementations areJ
FileSystem5mlApplication2onte#tJ This container loads the definitions of the beans from an
5,6 file. Aere you need to provide the full path of the 5,6 bean configuration file to the
constructor.
2lassath5mlApplication2onte#t This container loads the definitions of the beans from an
5,6 file. Aere you do not need to provide the full path of the 5,6 file but you need to set
26ASSATA properly because this container will look bean configuration 5,6 file in
26ASSATA.
Web5mlApplication2onte#tJ This container loads the 5,6 file with definitions of all beans
from within a web application.
3ean 1efinition
What is A/?
Aspect oriented programming .A/0framework. Aspect /riented rogramming entails
breaking down program logic into distinct parts called so$called concerns. The functions that
span multiple points of an application are called crosscutting concerns and these cross$
cutting concerns are conceptually separate from the application4s business logic. There are
various common good e#amples of aspects like logging+ auditing+ declarative transactions+
security+ and caching etc.
What is A/ Terminology?
Aspect a module has set A/ providing crosscutting re%uirements is called Aspect for an
e#ample like logging module will be called Aspect for logging. An Application can have n
number of Aspects.
Jointpoing this represents in your application where A/ Aspect can be plugged in+ it"s
called Joinpoint.
Advice Actual action can be taken before or after e#ecution of a method is called advice.
ointcut This is a set of one or more -oint points where an advice is e#ecuted is called
pointcut.
!ntroduction An !ntroduction allows you to add new methods or attributes for an e#isting class.
Target ob-ect The ob-ect being advised by one or more aspects. The ob-ect always kind of
FT ob-ect.
Weaving !ts the process of linking aspect with other application types or ob-ects to create an
Advice ob-ect. !t can be done compile time or load time or at runtime.
2ompile time Aspect are made when the target class is compiled.
2lass load time Aspect are made when the target class are loaded into JI,.
Cuntime Aspect are made something during e#ecution of the application. Typically+ an A/
container will dynamically generate pro#y ob-ect that will delegate to the target ob-ect.
Types of Advice
3efore Cun Advice 3efore the method e#ecution.
After Cun Advice After the method e#ecution.
After$Ceturn Cun Advice after method e#ecution when method e#ecution is successfully.
After$throwing Cun Advice after method e#ecution only if method e#ist by throwing an
e#ception.
Around Cun Advice before and After the Advised method is e#ecuted.
Spring J132 Framework.
J132Template class The JdbcTemplate class e#ecutes S)6 %ueries+ update statements and
stored procedure calls+ performs iteration over CesultSets and e#traction of returned
parameter values. !t also catches J132 e#ceptions and translates them to the generic+ more
informative+ e#ception hierarchy defined in the org.springframework.dao package.

You might also like