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.
// Logging class is anotated with @Aspect
// and will contain advices.
// The class ImplementAspect
// contains method Aspectcall
// and the advices will be applied
// on that method for demo.
Scanner sc =
"my first aspect"
// **Add beanconfiguration file
// in your programme when executing.**
"enter an integer"
a = sc.nextInt();
" for the first time"
"This is an"
" extra method"
- 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
Let’s see what does a joinpoint do in our @Aspect class(Logger)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Introduction to Spring Framework
- Introduction to the Spring Data Framework
- Spring Boot | How to access database using Spring Data JPA
- Best Practices of Object Oriented Programming (OOP)
- Object Oriented Programming (OOPs) Concept in Java
- Spring Dependency Injection with Example
- Introduction to Spring Boot
- Spring vs. Struts in Java
- Spring MVC using Java based configuration
- How to create a REST API using Java Spring Boot
- Bean life cycle in Java Spring
- How to create a basic application in Java Spring Boot
- How to encrypt passwords in a Spring Boot project using Jasypt
- Singleton and Prototype Bean Scopes in Java Spring
- Spring Boot | How to consume string messages using Apache Kafka
- Spring Boot | How to publish JSON messages on Apache Kafka
- Spring Boot | How to publish String messages on Apache Kafka
- Containerizing Java applications | Creating a Spring Boot App using Dockerfile
- Spring Boot | How to consume JSON messages using Apache Kafka
- Introduction to Hibernate Framework
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.