English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot AOP @Around

Around Advice is implemented by @Around Annotation indicates. It executes before and after the join point. This is the most powerful recommendation. It also provides more control to the end user, allowing them to handle ProceedingJoinPoint.

Let's implement the recommendation around the application.

Spring Boot @Around example

Steps1: Open Spring Initializr http://start.spring.io .

Steps2: Provide GroupName. We provide the group name com.w3codebox.

Steps3: Provide Artifact Id.Provide Artifact Id aop-around-advice-example.

Steps4: Add Spring Web dependency.

Steps5: Click Generatebutton. When we click the "Generate" button, it wraps all specifications in jar file and download it to the local system.

the6Step: ExtractDownloaded jar file.

Steps7: Use the following steps to importFolder:

File->Import->Existing Maven project->Next->Browse folder aop-around-advice-example ->Finished.

Steps8: Open pom.xml file and add the following AOP dependency. It is used Spring AOP and AspectJ This is an introduction to aspect-oriented programming.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>

pom.xml

<?xml version="1.0 encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.w3codebox</groupId>
<artifactId>aop-around-advice-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aop-around-advice-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Steps9: Create a name of com.w3codebox.service package.

Steps10: Create a package named BankService 类别。

In this class, we define a class named displayBalance() method. It checks the account number. If the account number matches, it returns the total amount, otherwise it returns a message.

BankService.java

package com.w3codebox.service;
import org.springframework.stereotype.Service;
@Service
public class BankService 
{
public void displayBalance(String accNum) 
{
System.out.println("Inside displayBalance() method");
if(accNum.equals("12345")) 
{
System.out.println("Total balance:") 10,000");
}
else 
{
System.out.println("Sorry! wrong account number.");
}
}
}

Steps11: Create another package named com.w3codebox.aspect package.

Steps12: Create a package named BankAspect class.

In the following class, we define two classes named logDisplayingBalance()and aroundAdvice()method method.

BankAspect.java

package com.w3codebox.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
//Enable spring AOP functionality in the application
@Aspect
@Component
public class BankAspect
{
    //Display all available methods, which will notify all method calls
    @Pointcut(value= "execution(* com.w3codebox.service.BankService.*(..)))
    private void logDisplayingBalance() 
    { 
    }
    //declares the around advice applied before and after the method matches the切入点expression
    @Around(value= "logDisplayingBalance()")
    public void aroundAdvice(ProceedingJoinPoint jp) throws Throwable 
    {
        System.out.println("Before method call, aroundAdvice() method ") + jp.getSignature().getName() + " method ");
        try 
        {
            jp.proceed();
        } 
        finally 
        {
        }
        System.out.println("After method call, aroundAdvice() method ") + jp.getSignature().getName() + " method ");
    }
}

Steps13: Open AopAroundAdviceExampleApplication.java file and add annotations @EnableAspectJAutoProxy.

This annotation enables support for processing AspectJ annotations @Aspect components annotated with the annotation. It is used together with @Configuration annotation.

ConfigurableApplicationContext is an interface that provides tools for configuring the application context in addition to the application context client methods in ApplicationContext.

AopAroundAdviceExampleApplication.java

package com.w3codebox;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.w3codebox.service.BankService;
@SpringBootApplication
//@EnableAspectJAutoProxy annotation supports processing components marked with @Aspect annotation. It is similar to the tag in XML configuration.
@EnableAspectJAutoProxy
public class AopAroundAdviceExampleApplication 
{
    public static void main(String[] args) 
    {
    ConfigurableApplicationContext context = SpringApplication.run(AopAroundAdviceExampleApplication.class, args);
    //Get the employee object from the application context.
    BankService bank = context.getBean(BankService.class);
    //Display account balance
    String accnumber = "12345";
    bank.displayBalance(accnumber);
    //Close the context object
    context.close();
    }
}

After creating all packages and classes, the project directory is as follows:

Now, run the application.

Steps14: Open AopAroundAdviceExampleApplication.java And run the Java application as it is.

In the above output, we see that the method aroundAdvice() is called twice. First, in the execution displayBalance()Before the method, first, in the execution displayBalance()After the method. Called consultation.