Spring AOP?

Spring AOP?

AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.

1. Introduction

In this tutorial, we’ll introduce AOP (Aspect Oriented Programming) with Spring and start understanding how we can start using this powerful tool in practical scenarios.

It’s also possible to leverage AspectJ’s annotations when developing using Spring AOP but in this article, we’re focusing on the core Spring AOP XML-based configuration.

2. Overview

AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code without modification of the code itself.

Instead, we can declare this new code and these new behaviors separately.

Spring’s AOP framework helps us implement these cross-cutting concerns.

3. Maven Dependencies

Let’s start by adding Spring’s AOP library dependency in the pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE<version>
</parent>
  
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
</dependencies>

4. AOP Concepts and Terminology

Let’s briefly go over the concepts and terminology specific to AOP:

4.1. Business Object

A business object is a normal class which has a normal business logic. Let’s look at simple example of a business object where we just add two numbers:

public class SampleAdder {
    public int add(int a, int b) {
        return a + b;
    }
}

Note that this class is a normal class with business logic and without any Spring-related annotations.

4.2. Aspect

An aspect is a modularization of a concern that cuts across multiple classes. Unified logging can be an example of such cross-cutting concern.

Let’s see how we define a simple Aspect:

public class AdderAfterReturnAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    public void afterReturn(Object returnValue) throws Throwable {
        logger.info("value return was {}",  returnValue);
    }
}

In the above example, we’ve just defined a simple Java class which has a method called afterReturnthat takes one argument of type Object and it just logs in that value. Note that even our AdderAfterReturnAspect is a standard class, free of any Spring annotations.

In the next sections, we’ll see how we can wire this Aspect to our Business Object.

4.3. Joinpoint

Joinpoint is a point during the execution of a program, such as execution of a method or the handling of an exception.

In Spring AOP, a JoinPoint always represents a method execution.

4.4. Pointcut

A Pointcut is a predicate that helps match an Advice to be applied by an Aspect at a particular JoinPoint.

The Advice is often associated with a Pointcut expression and runs at any Joinpoint matched by the Pointcut.

4.5. Advice

An advice is an action taken by an aspect at a particular Joinpoint. Different types of advice include “around,” “before” and “after” advice.

In Spring, an Advice is modeled as an interceptor, maintaining a chain of interceptors around the Joinpoint.

4.6. Wiring Business Object and Aspect

Let’s look at how we can wire a Business object to an Aspect with an After-Returning advice.

Below is the config excerpt that we’d place in a standard Spring config in the “<beans>” tag:

<bean id="sampleAdder" class="org.baeldung.logger.SampleAdder" />
<bean id="doAfterReturningAspect"
  class="org.baeldung.logger.AdderAfterReturnAspect" />
<aop:config>
    <aop:aspect id="aspects" ref="doAfterReturningAspect">
       <aop:pointcut id="pointCutAfterReturning" expression=
         "execution(* org.baeldung.logger.SampleAdder+.*(..))"/>
       <aop:after-returning method="afterReturn"
         returning="returnValue" pointcut-ref="pointCutAfterReturning"/>
    </aop:aspect>
</aop:config>

As can be seen, we’ve defined a simple bean called simpleAdder which represents an instance of a Business Object. In addition, we’re creating an instance of an Aspect called AdderAfterReturnAspect.

XML is, of course, not our only option here; as mentioned before, AspectJ annotations are fully supported as well.

4.7. Configuration at Glance

Tag aop:config is used for defining AOP-related configuration. Within the config tag, we define the class that represents an aspect. We’ve given it a reference of “doAfterReturningAspect” aspect bean that we created.

Next, we define a Pointcut using the pointcut tag. Pointcut used in the example above is execution(* org.baeldung.logger.SampleAdder+.*(..)) which means apply an advice on any method within SampleAdder class that accepts any number of arguments and returns any value type.

Next, we define which advice we want to apply. In the above example, we’re going to apply after-returning advice which is defined in our Aspect AdderAfterReturnAspect by executing method named afterReturn defined using attribute method.

This advice within Aspect takes one parameter of type Object. The parameter gives us an opportunity to take an action before and/or after the target method call. In this case, we just log the method’s return value.

5. Conclusion

In this tutorial, we illustrated concepts used in AOP and example of using the AOP module of Spring.

Thanks for reading!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Set up Web App with Spring Boot and Spring Security

Download the Spring Boot Web App Example Project. Run the Initial Web App. Add Project Dependencies for Your Spring Boot + Spring Security Web App. Understand Your Spring Boot App. Set Up Okta for OAuth 2.0 Single Sign-On. Configure Your Spring Boot App for Single SignOn (SSO) Refine Our Permissions

Spring Boot Tutorials - Spring Boot Full Course

Spring Boot Tutorials | Full Course - What is Spring? Spring Boot is an open source Java-based framework used to create a Micro Service. Spring Boot contains a comprehensive infrastructure support for developing a micro service and enables you to develop enterprise-ready applications that you can “just run”.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.