A short introduction to Spring AOP

helpflightInternet and Web Development

Nov 10, 2013 (3 years and 9 months ago)

104 views

A short introduction to
Spring AOP
INF329
Introduction to AOP

AOP is an abbreviation for aspect-
oriented programming

Aspect-oriented programming is a new
paradigm in programming, seperating
functionality into aspects, not objects

Aspect-oriented programming is fully
integratable with object-oriented
programming
Why AOP?

Most functionality is easily divided into objects, but
some is not

Transactions

Caching

Logging

Etc

Common for these things is that it is things that you
need, but it has nothing to do with the business logic
itself, and therefore decreases the readability of the
business logic code

This is called ”cross-cutting concerns”in the AOP
lingo
What happens?

The cross-cutting concerns are
factored out into aspectsand
applied to the code in question

For instance: add logging to the object
in question

This removes the ”noise”from non-
business logic code

Also, all logging code is in one place,
thus removing redundancy
Examples of use

Logging

Caching

Security

Testing

Transactions
AOP terms and definitions

Aspect-oriented programming has
its own lingo, and it’s not always
straight-forward:

Aspect: The functionality of a cross-
cutting concern

Joinpoint: A place in the execution
where an aspect can be plugged in. In
Spring AOP, this means a method call
or an exception being thrown
AOP terms and definitions
[cont.]

Advice: The actual implementation of
an aspect

Pointcut: Ties together advice and
joinpoints by specifying where the
advice should be applied
How aspects are applied

In Spring AOP, aspects are proxy-based

This means that the framework uses the
proxy pattern to create a proxy of the
original object (called the target) and add
the advice to that

This is completely invisible from the business
logic code

Another popular AOP implementation,
AspectJ, applies the aspects at compile
time. This requires a special compiler
Advicetypes

Therearefivekindsofadvicesupportedby
Spring AOP:

Beforeadvice: Addfunctionalitythatexecutes
beforea methodis run

Afteradvice: Addfunctionalitythatexecutes
after
a
methodis run

Throwsadvice: Addfunctionalitythatis run if
an exceptionis thrown

Aroundadvice: Addfunctionalitythasis
executedbeforeand aftera methodis run

Introductionadvice: Alters a class’behaviour.
Wewillnot cover thisin thispresentation.
Howto implementAOP in
Spring

With ”regular”Spring AOP, onlyobjects
createdby thecontainer itselfcanbe advised

In Spring 2.0 and over, alsoAspectJaspects
aresupported, enablingtheprogrammer to
adviseall objects

Adviceis writtenas normal Java classes
implementingoneor more interfaces

No needfor a specialcompiler

Pointcutsaredefinedin Springs XML
configurationfile

No needto learna newlanguage
Implementinga simple
beforeadvice

Theadviceitselfis writtenlike this:

An objectofthisclassis instanciated
in Spring’sconfigfile like this:

Note: Theclassmust implementthe
interfaceMethodBeforeAdvice
Implementinga simple
beforeadvice[cont.]

Theproxyfor thecourseobjectis writtenlike this:

Thiswillcausethemessage”enteringmethod”to be
printedouteverytime a methodoftheinf100bean
objectis called

Note: Wedon’tusepointcutshere. Theadviceis
appliedto everymethodoftheclass.
Afteradvice

Afteradviceis implementedin a
verysimilarmanner, by letting the
classimplementtheinterface
AfterReturningAdvice:
Aroundadvice

Aroundadviceis used whenyouwantto
addfunctionalitybeforeand afterthe
methodhas executed

Theproxiedmethodmust be called
explicitlywithinvocation.proceed()

Is implementedby letting theclass
implementtheinterfaceMethodInterceptor
Throwsadvice

Is executedifand onlyifthemethodthat
is advisedthrowsan exception

Note: Alwaysrethrowsthethrown
exception, unlessa newexceptionis
thrown

Is implementedby letting theclass
implementtheinterfaceMethodInterceptor
Applyingpointcutswith
advisors

To be ableto usepointcutsand define
whichmethodsshouldusewhich
advice, weuseadvisors:

To usetheadvisor, setthe
interceptorNamesvaluein theproxy
beanto pointto theadvisor, not the
advice
Wrap-up

Thiswasall wehadtime for today

Thisis onlya briefintroductionto Spring AOP and
AOP in general: It is muchmore!

To findoutmore:

Spring frameworkhomepage:

http://www.springframework.org

Manual entryaboutAOP:

http://www.springframework.org/docs/reference/aop.html

TheAspectJproject:

http://www.eclipse.org/aspectj/

TheAOP Alliance:
•http://aopa
l
l
iance.sourceforge.net/