Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Spring | AOP

Spring
AOP
pg. 1
Spring | AOP
Table of Contents
Introduction ...........................................................................................................................................................................3
AOP Concepts ........................................................................................................................................................................3
Reuse Aspect Pointcut Definitions ..................................................................................................................................... 10

pg. 2
Spring | AOP

Introduction
• AOP is often referred to as a tool for implementing crosscutting concerns.
• The term crosscutting concerns refers to logic in an application that cannot
be decomposed from the rest of the application and may result in code
duplication and tight coupling.
• By using AOP for modularizing individual pieces of logic, known as concerns,
you can apply them to many parts of an application without duplicating the
code or creating hard dependencies.
Ex: Logging and security are typical examples of crosscutting concerns
that are present in many applications.
• Consider an application that logs the start and end of every method for
debugging purposes. You will probably refactor the logging code into a
special class, but you still have to call methods on that class twice per
method in your application in order to perform the logging.
• Using AOP, you can simply specify that you want the methods on your
logging class to be invoked before and after each method call in your
application
AOP Concepts
AOP comes with its own specific set of concepts and terms, and it’s
important to understand what they mean. The following are the core concepts of
AOP.
JoinPoints:
A joinpoint is a well-defined point during the execution of your
application. Typical examples of joinpoints include a call to a method, the
method invocation itself, class initialization, and object instantiation.
Joinpoints are a core concept of AOP and define the points in your
application at which you can insert additional logic using AOP.
In Spring, it supports only one joinpoint type: method invocation. At
first glance, this might seem like a severe limitation if you are familiar with
pg. 3
Spring | AOP

other AOP implementations such as AspectJ, which supports many more


joinpoints, but in fact this makes Spring more accessible.
The method invocation joinpoint is by far the most useful joinpoint
available, and using it, you can achieve many of the tasks that make AOP
useful in day-to-day programming.

JoinPoint

Advice:
The code that is executed at a particular joinpoint is the advice, defined
by a method in your class. There are many types of advice, such as before,
which executes before the joinpoint, and after, which executes after it.

1
2

Figure 1-1. Sample Aspect component

1. “execution(* com.navnredy.*.PersonAccessor.getPersonDetails(..))” –
is a point cut
pg. 4
Spring | AOP

2. “@Before” – is an Advice
3. @Aspect – is an Aspect

Spring supports the following advices


1. @Before
Before advice is one of the most useful advice types available in
Spring. This advice can modify the arguments passed to a method
and can prevent the method from executing by raising an exception.
Usage:
• Security Checking
• Validations
• Logging

2. @After
An after advice is executed after a join point finishes and is
represented by a method annotated with @After, whenever it
returns a result or throws an exception.

3. @AfterReturning
An after advice is executed regardless of whether a join point returns
normally or throws an exception. But an After-returning advice is
executed after the method invocation at the joinpoint has finished
executing and has returned a value.
Using this advice we
can't change the return value,
can’t change the execution path,
can’t prevent the method from executing,
can read arguments,
can throw exception.

pg. 5
Spring | AOP

4. @AfterThrowing
Throws advice is similar to after-returning advice in that it executes
after the joinpoint, which is always a method invocation, but throws
advice executes only if the method throws an exception.
Using this advice we
can’t ignore the exception
can change the exception type
Usage:
• Centralized error logging mechanism

5. @Around
Around advice functions like a combination of before and after
advice, with one big difference: you can modify the return value. Not
only that, but you can prevent the method from executing. This
means that by using around advice, you can essentially replace the
entire implementation of a method with new code.
Usage:
• Remote proxy support
• Transaction support
• Profiling
Pointcuts:
A pointcut is a collection of joinpoints that you use to define when
advice should be executed. By creating pointcuts, you gain fine-grained
control over how you apply advice to the components in your application.
As mentioned previously, a typical joinpoint is a method invocation, or the
collection of all method invocations in a particular class. Often you can
compose pointcuts in complex relationships to further constrain when
advice is executed.

Supported Pointcut Designators:


pg. 6
Spring | AOP

1. execution - for matching method execution join points

execution(* com.navnredy.PersonAccessor.*(..))
- means all methods within PersonAccessor which resides in
com.navnredy package, methods can accept any number of
parameters and can have any return type.

execution(* *.PersonAccessor.*(..))
- means all methods within PersonAccessor irrespective of any
package, can have any number of parameters and can have any
return type.

execution(pubilc * *.PersonAccessor.*(..))
- means only public methods within PersonAccessor irrespective
of any package, can have any number of parameters and can have
any return type.

execution(public String *.PersonAccessor.*(..))


- means only public methods and return type must be String
within PersonAccessor irrespective of any package, can have any
number of parameters.

execution(public String *.PersonAccessor.*(double, ..))


- means only public methods and return type must be String
within PersonAccessor irrespective of any package, must have double
parameter as first and can have any number of parameters after that.

2. Within - matches all join points within certain types

within(com.navnredy.*)
- matches all the method execution join points within
com.navnredy package(sub package won't considered).
pg. 7
Spring | AOP

within(com.navnredy..*)
- matches all the method execution join points within
com.navnredy package and it's sub packages.

within(com.navnredy.PersonAccessor)
- matches all the method execution join points within
com.navnredy.PersonAccessor class.

within(PersonAccessor)
- matches if the target class is located in the same package as this
aspect, the package name can be omitted

within(PersonAccessor+)
- matches the method execution join points within all classes that
implement the PersonAccessor interface by adding a plus(+) symbol

3. this
4. target
5. args
6. @target

@target(com.navnredy.SecurityChecker)
- any join point (method execution only in Spring AOP) where the
target object has an @SecurityChecker

7. @args
8. @within

@within(com.navnredy.SecurityCheck)
- any join point (method execution only in Spring AOP) where the
declared type of the target object has an @SecurityCheck annotation
pg. 8
Spring | AOP

9. @annotation

@annotation(com.navnredy.SecurityCheck)
- any join point (method execution only in Spring AOP) where the
executing method has an @SecurityCheck annotation

We can combine the pointcut expression using the operators && (and),
|| (or), and ! (not)
within(PersonAccessor+) || within(EmployeeAccessor+)

Aspects:
An aspect is the combination of advice and pointcuts encapsulated in a
class. This combination results in a definition of the logic that should be
included in the application and where it should execute.
In Spring AOP, an aspect is represented by an instance of a class that
implements the Advisor interface. Spring provides convenience Advisor
implementations that you can reuse in your applications, thus removing the
need for you to create custom Advisor implementations. There are two
subinterfaces of Advisor: PointcutAdvisor and IntroductionAdvisor.Or
Simply we can use @Aspect annotation to decalre any class as aspect.

Figure 1-2. A sample Aspect


pg. 9
Spring | AOP

Weaving:
This is the process of inserting aspects into the application code at the
appropriate point. For compile-time AOP solutions, this weaving is generally
done at build time. Likewise, for runtime AOP solutions, the weaving
process is executed dynamically at runtime. AspectJ supports another
weaving mechanism called loadtime weaving (LTW), in which it intercepts
the underlying JVM class loader and provides weaving to the bytecode when
it is being loaded by the class loader.
Target:
An object whose execution flow is modified by an AOP process is
referred to as the target object. Often you see the target object referred to
as the advised object.

Reuse Aspect Pointcut Definitions


When writing aspects, we can directly embed a pointcut expression in an
advice annotation. If want to use the same pointcut expression in multiple advices
without embedding it multiple times then we can use the @Pointcut annotation
to define a pointcut independently to be reused in multiple advices.

Figure 1-3. A Reusable Pointcuts

This Pointcut is will trigger every join point execution in all the packages
and its sub packages irrespective of return type and number of parameters

pg. 10
Spring | AOP

Figure 1-4. A sample Aspect using reusable Pointcuts

pg. 11

You might also like