Scroll to top

Spring AOP(Aspect Oriented Programming)


The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. In OOP, classes are organised to perform specific kind of work. There could be a kind of work which might be done by several classes, in that sense ASPECT will be used.
AOP in Spring Framework

  • Provide declarative enterprise services,
  • Allow users to implement custom aspects
  • Terminologies used in AOP(Not Spring Specific):


A modularization of a concern that cuts across multiple classes.

Join point:

Point during the execution of a program. Method Execution(Spring Specific)


Action taken by an aspect at a particular join point


a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name).


Declaring additional methods or fields on behalf of a type.

Target Object:

Object being advised by one or more aspects.

AOP proxy:

an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.


linking aspects with other application types or objects to create an advised object.


Type of Advice

Before advice:

Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

After returning advice:

Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.

After throwing advice:

Advice to be executed if a method exits by throwing an exception.

After(finally) advice:

Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).

Around advice:

Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

AOP Proxies in Spring

By Default Spring AOP uses standard J2SE dynamic proxies for AOP proxies. It can also use the CGLIB proxies(Required for CLASS proxying).
What is proxy: <TO DO>

Use of Proxies:

AOP Style in Spring

There are two approach(style) available in Spring for AOP implementation.

@AspectJ annotation-style approach
Spring XML configuration-style approach.

@AspectJ Approach
How to Enable: There are two way we can enable @AspectJ annotation style.

Include below configuration in your spring configuration file.

Including below definition in application context.
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />

JARs Required

Note : version 1.5.1 or later

Declaring an Aspect

After enabling the @AspectJ support in web application. Any class with @Aspect annotation will be detected by spring(Automatically / XML configuration) and used to configure Spring AOP.

Detection of Aspect by Spring Framework can be done in two way:

Register aspect classes as regular beans in your Spring XML configuration.
Ex: <TO DO>
Autodetect them through classpath scanning. For autodetection to work properly @Component annotation needs to be added with @Aspect.
Ex: <TO DO>
Declaring a Pointcut

Pointcuts determine join points of interest, and thus enable us to control when advice executes. Spring AOP only supports method execution join points for Spring beans.
So in case of Spring, pointcuts are nothing but matching the execution of methods on Spring Bean.
A pointcut declaration has two parts: a signature comprising a name and any parameters, and a pointcut expression that determines exactly which method executions we are interested in.
Ex: \
Combining Pointcuts

Pointcuts can be combined using && or ||.
Declaring Advice

Advice is associated with a pointcut expression, and runs before, after, or around method executions matched by the pointcut.

Before Advice: @Before annotation can be used to declare advice in an Aspect.

After Returning Advice: After returning advice runs when a matched method execution returns normally. It is declared using the @AfterReturning annotation.

After Throwing Advice: After throwing advice runs when a matched method execution exits by throwing an exception. It is declared using the @AfterThrowing annotation.

After (finally) advice: After (finally) advice runs however a matched method execution exits. It is declared using the @After annotation. After advice must be prepared to handle both normal and exception return conditions.
Around advice: Around advice runs “around” a matched method execution.


  1. AOP has no link with IOC so AOP can be used individually.
  2. Spring AOP currently supports only method execution join points.
  3. Spring AOP is proxy based.
Author avatar


  1. fotograf ślubny lublin

    We are a group of volunteers and opening a
    new scheme in our community. Your website offered us
    with valuable info to work on. You’ve done a formidable job and our entire community will be thankful to you.

  2. sss

    nobody knows bro…

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.