Spring Boot - spring-boot-starter-aop

spring-boot-starter-aop

At work, spring-boot-starter-aop is often used to implement aspect programming in applications, such as logging, performance monitoring, transaction management, etc. Here's a full code example using logging as an example:

Create a Maven project and add the following dependencies to the pom.xml file:

<dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Starter AOP -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
    </dependencies>

Create a Java class UserService.java that contains a simple method getUser():


package com.lfsun.springbootstarteraop;

import org.springframework.stereotype.Component;

@Component
public class UserService {
    
    

    public String getUser(String username) {
    
    
        System.out.println("Fetching user: " + username);
        return "User: " + username;
    }

}

Create an aspect class LoggingAspect.java, which will print logs before and after the method execution of the UserService class:

package com.lfsun.springbootstarteraop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
    
    

    @Before("execution(* UserService.getUser(String)) && args(username)")
    public void beforeAdvice(JoinPoint joinPoint, String username) {
    
    
        System.out.println("Fetching user beforeAdvice: " + username);
    }

    @After("execution(* UserService.getUser(String)) && args(username)")
    public void afterAdvice(JoinPoint joinPoint, String username) {
    
    
        System.out.println("Finished fetching user afterAdvice: " + username);
    }

}

Create a startup class Application.java, which is used to start the Spring Boot application:


package com.lfsun.springbootstarteraop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringBootStarterAopApplication {
    
    

    public static void main(String[] args) {
    
    
        ConfigurableApplicationContext context = SpringApplication.run(SpringBootStarterAopApplication.class, args);

        UserService userService = context.getBean(UserService.class);
        userService.getUser("lfsun666");
    }

}

In this example, we create a UserService class that has a getUser()
method that returns information about a user. Then, we created an aspect class LoggingAspect, which uses @Before and @After annotations to define pre-notifications and post-notifications, which will print logs before and after the execution of the getUser()
method of the UserService class. Finally, in the main() method of the Application class, we get an instance of UserService and call the getUser() method.

When you run this Spring Boot application, you will see log messages printed before and after the execution of the getUser() method.

Guess you like

Origin blog.csdn.net/qq_43116031/article/details/131135091