Aspect Oriented Programming and AOP in Spring Framework
Aspect oriented programming(AOP) as the name suggests uses aspects in programming. It can be defined as the breaking of code into different modules, also known as modularisation, where the aspect is the key unit of modularity. Aspects enable the implementation of crosscutting concerns such as- transaction, logging not central to business logic without cluttering the code core to its functionality. It does so by adding additional behaviour that is the advice to the existing code. For example- Security is a crosscutting concern, in many methods in an application security rules can be applied, therefore repeating the code at every method, define the functionality in a common class and control were to apply that functionality in the whole application.
Dominant Frameworks in AOP:
AOP includes programming methods and frameworks on which modularisation of code is supported and implemented. Let’s have a look at the three dominant frameworks in AOP:

- AspectJ: It is an extension for Java programming created at PARC research centre. It uses Java like syntax and included IDE integrations for displaying crosscutting structure. It has its own compiler and weaver, on using it enables the use of full AspectJ language.
- JBoss: It is an open source Java application server developed by JBoss, used for Java development.
- Spring: It uses XML based configuration for implementing AOP, also it uses annotations which are interpreted by using a library supplied by AspectJ for parsing and matching.
Currently, AspectJ libraries with Spring framework are dominant in the market, therefore let’s have an understanding of how Aspect-oriented programming works with Spring.
How Aspect-Oriented Programming works with Spring:
One may think that invoking a method will automatically implement cross-cutting concerns but that is not the case. Just invocation of the method does not invoke the advice(the job which is meant to be done). Spring uses proxy based mechanism i.e. it creates a proxy Object which will wrap around the original object and will take up the advice which is relevant to the method call. Proxy objects can be created either manually through proxy factory bean or through auto proxy configuration in the XML file and get destroyed when the execution completes. Proxy objects are used to enrich the Original behaviour of the real object.
Common terminologies in AOP:
- Aspect: The class which implements the JEE application cross-cutting concerns(transaction, logger etc) is known as the aspect. It can be normal class configured through XML configuration or through regular classes annotated with @Aspect.
- Weaving: The process of linking Aspects with an Advised Object. It can be done at load time, compile time or at runtime time. Spring AOP does weaving at runtime.
Let’s write our first aspect class but before that have a look at the jars required and the Bean configuration file for AOP.


packagecom.aspectimportorg.aspectj.lang.annotation.Aspect;importJava.lang.RuntimeException;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;// Logging class is anotated with @Aspect// and will contain advices.@AspectclassLogging {}// The class ImplementAspect// contains method Aspectcall// and the advices will be applied// on that method for demo.publicclassImplementAspect {publicstaticvoidmain(String args[]){Scanner sc =newScanner(System.in);System.out.println("my first aspect");// **Add beanconfiguration file// in your programme when executing.**ApplicationContext ctx=newClassPathXmlApplicationContext("beanconfigfile.XML");ImplementAspect call= (ImplementAspect)ctx.getbean("aspect");System.out.println("enter an integer");inta = sc.nextInt();if(a ==1) {thrownewRuntimeException(msg);}else{call.aspectCall();}call.myMethod();}publicvoidaspectCall(){System.out.println("Applying advices"+" for the first time");}publicvoidmyMethod(){System.out.println("This is an"+" extra method");}}chevron_rightfilter_none - Advice: The job which is meant to be done by an Aspect or it can be defined as the action taken by the Aspect at a particular point. There are five types of Advice namely: Before, After, Around, AfterThrowing and AfterReturning. Let’s have a brief discussion about all the five types.
Types of Advices:

- Before: Runs before the advised method is invoked. It is denoted by @Before annotation.
- After: Runs after the advised method completes regardless of the outcome, whether successful or not. It is denoted by @After annotation.
- AfterReturning: Runs after the advised method successfully completes ie without any runtime exceptions. It is denoted by @AfterReturning annotation.
- Around: This is the strongest advice among all the advice since it wraps around and runs before and after the advised method. This type of advice is used where we need frequent access to a method or database like- caching. It is denoted by @Around annotation.
- AfterThrowing: Runs after the advised method throws a Runtime Exception. It is denoted by @AfterThrowing annotation.
Let’s implement all the five pieces of advice in our Aspect class Logger
// Program to show types of Advices @Aspectclass Logging { // Implementing all the five pieces of advice // to execute AfterThrowing advice enter integer value as 1. // **Before** @Before("execution(public void com.aspect.ImplementAspect.aspectCall())") public void loggingAdvice1() { System.out.println("Before advice is executed"); } // **After** @After("execution(public void com.aspect.ImplementAspect.aspectCall())") public void loggingAdvice2() { System.out.println("Running After Advice."); } // **Around** @Around("execution(public void com.aspect.ImplementAspect.myMethod())") public void loggingAdvice3() { System.out.println("Before and After invoking method myMethod"); } // **AfterThrowing** @AfterThrowing("execution(" public void com.aspect.ImplementAspect.aspectCall()) ") public void loggingAdvice4() { System.out.println("Exception thrown in method"); } // **AfterRunning** @AfterReturning("execution(public void com.aspect.ImplementAspect.myMethod())") public void loggingAdvice5() { System.out.println("AfterReturning advice is run"); } } |
Let’s see what does a joinpoint do in our @Aspect class(Logger)
// Program to show JoinPoints @Aspectclass Logging { // Passing a JoinPoint Object // into parameters of the method // with the annotated advice // enables to print the information /// when the advice is executed // with the help of toString() method // present in it. @Before("execution(public void com.aspect.ImplementAspect.aspectCall())") public void loggingAdvice1(JoinPoint joinpoint) { System.out.println("Before advice is executed"); System.out.println(joinpoint.toString()); } } |
// Program to shgw PointCuts @Aspectclass Logging { // pointcut() is a dummy method // required to hold @Pointcut annotation // pointcut() can be used instead of writing line 1 // whenever required, as done in line 4. // This prevents a repetition of code. @Pointcut("execution(public void com.aspect.ImplementAspect.aspectCall())") // line 1 public void pointCut() { } // pointcut() is used to avoid repeatition of code @Before("pointcut()") public void loggingAdvice1() { System.out.println("Before advice is executed"); } } |
Recommended Posts:
- Introduction to Spring Framework
- Object Oriented Programming (OOPs) Concept in Java
- Spring Dependency Injection with Example
- Introduction to Hibernate Framework
- Lock framework vs Thread synchronization in Java
- Why Java is not a purely Object-Oriented Language?
- Java Programming Basics
- Socket Programming in Java
- Fast I/O in Java in Competitive Programming
- Comparison of Java with other programming languages
- Different Approaches to Concurrent Programming in Java
- Beginning Java programming with Hello World Example
- Introducing Threads in Socket Programming in Java
- The complete History of Java Programming Language
- Lambda Expressions | Concurrent Programming Approach 4
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



