Spring--AOP--执行顺序--用法/实例

原文网址:Spring--AOP--执行顺序--用法/实例_IT利刃出鞘的博客-CSDN博客

简介

        本文用示例介绍Spring(SpringBoot)的AOP的执行顺序。

公共代码

启动类

package com.example.demo;

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

@SpringBootApplication
public class DemoSpringaopSimpleApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoSpringaopSimpleApplication.class, args);
    }
}

Entity

package com.example.demo.entity;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String userName;
    private String note;
}

Service

package com.example.demo.service;

import com.example.demo.entity.User;

public interface UserService {
    public void printUser(User user);
}
package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public void printUser(User user) {
        if (user == null) {
            throw new RuntimeException("检查用户参数是否为空");
        }
        System.out.print("id = " + user.getId());
        System.out.print("\t userName = " + user.getUserName());
        System.out.println("\t note = " + user.getNote());
    }
}

Controller

package com.example.demo.controller;

import com.example.demo.annotation.OperationLog;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/user")
@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/print")
    @ResponseBody
    public User testPrint(User user) {
        userService.printUser(user);
        return user;
    }

    @RequestMapping("/add")
    @ResponseBody
    @OperationLog(type = "添加", desc = "添加数据")
    public User testAdd(User user) {
        return user;
    }

    @RequestMapping("/update")
    @ResponseBody
    @OperationLog(type = "更新", desc = "更新数据")
    public User testUpdate(User user) {
        userService.printUser(user);
        return user;
    }
}

两AOP+@Order(都不调用proceed())

代码

AOP1

package com.example.demo.aspect;

import com.example.demo.annotation.OperationLog;
import com.example.demo.entity.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Order(1)
public class SimpleAspect {
    @Pointcut("execution(* com.example.demo.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before() {
        System.out.println("SimpleAspect.before");
    }

    @Around("pointCut()")
    public void around2(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("SimpleAspect.around2:"+ "around before");
        // joinPoint.proceed();
        System.out.println("SimpleAspect.around2:" + "around after");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("SimpleAspect.after");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("SimpleAspect.afterReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("SimpleAspect.afterThrowing");
    }
}

 AOP2

package com.example.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Order(2)
public class OrderTestAspect {
    @Pointcut("execution(* com.example.demo.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before() {
        System.out.println("OrderTestAspect.before");
    }

    @Around("pointCut()")
    public void around1(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("OrderTestAspect.around1:" + "around before");
        // joinPoint.proceed();
        System.out.println("OrderTestAspect.around1:" + "around after");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("OrderTestAspect.after");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("OrderTestAspect.afterReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("OrderTestAspect.afterThrowing");
    }
}

测试

正常顺序

postman访问:http://localhost:8080/user/print?id=321&userName=Tony&note=abc

后端结果

SimpleAspect.around2:around before
SimpleAspect.around2:around after

前端结果

{
    "id": 321,
    "userName": "Tony",
    "note": "abc"
}

颠倒顺序

postman访问:http://localhost:8080/user/print?id=321&userName=Tony&note=abc

后端结果

OrderTestAspect.around1:around before
OrderTestAspect.around1:around after

前端结果

{
    "id": 321,
    "userName": "Tony",
    "note": "abc"
}

两AOP+@Order(都调用proceed())

代码

AOP1

package com.example.demo.aspect;

import com.example.demo.annotation.OperationLog;
import com.example.demo.entity.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Order(1)
public class SimpleAspect {
    @Pointcut("execution(* com.example.demo.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before() {
        System.out.println("SimpleAspect.before");
    }

    @Around("pointCut()")
    public void around1(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("SimpleAspect.around1:" + "around before");
        joinPoint.proceed();
        System.out.println("SimpleAspect.around1:" + "around after");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("SimpleAspect.after");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("SimpleAspect.afterReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("SimpleAspect.afterThrowing");
    }
}

AOP2 

package com.example.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Order(2)
public class OrderTestAspect {
    @Pointcut("execution(* com.example.demo.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before() {
        System.out.println("OrderTestAspect.before");
    }

    @Around("pointCut()")
    public void around2(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("OrderTestAspect.around1:" + "around before");
        joinPoint.proceed();
        System.out.println("OrderTestAspect.around1:" + "around after");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("OrderTestAspect.after");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("OrderTestAspect.afterReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("OrderTestAspect.afterThrowing");
    }
}

测试

正常顺序

后端结果

SimpleAspect.around1:around before
SimpleAspect.before
OrderTestAspect.around1:around before
OrderTestAspect.before
id = 321	 userName = Tony	 note = abc
OrderTestAspect.afterReturning
OrderTestAspect.after
OrderTestAspect.around1:around after
SimpleAspect.afterReturning
SimpleAspect.after
SimpleAspect.around1:around after

postman结果

{
    "id": 321,
    "userName": "Tony",
    "note": "abc"
}

颠倒顺序之后

后端结果

OrderTestAspect.around1:around before
OrderTestAspect.before
SimpleAspect.around1:around before
SimpleAspect.before
id = 321	 userName = Tony	 note = abc
SimpleAspect.afterReturning
SimpleAspect.after
SimpleAspect.around1:around after
OrderTestAspect.afterReturning
OrderTestAspect.after
OrderTestAspect.around1:around after

postman结果:

{
    "id": 321,
    "userName": "Tony",
    "note": "abc"
}

两AOP+@Order(高优先级调用proceed())

代码

AOP1

package com.example.demo.aspect;

import com.example.demo.annotation.OperationLog;
import com.example.demo.entity.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Order(1)
public class SimpleAspect {
    @Pointcut("execution(* com.example.demo.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before() {
        System.out.println("SimpleAspect.before");
    }

    @Around("pointCut()")
    public void around1(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("SimpleAspect.around1:" + "around before");
        joinPoint.proceed();
        System.out.println("SimpleAspect.around1:" + "around after");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("SimpleAspect.after");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("SimpleAspect.afterReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("SimpleAspect.afterThrowing");
    }
}

AOP2 

package com.example.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Order(2)
public class OrderTestAspect {
    @Pointcut("execution(* com.example.demo.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before() {
        System.out.println("OrderTestAspect.before");
    }

    @Around("pointCut()")
    public void around1(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("OrderTestAspect.around1:" + "around before");
        // joinPoint.proceed();
        System.out.println("OrderTestAspect.around1:" + "around after");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("OrderTestAspect.after");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("OrderTestAspect.afterReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("OrderTestAspect.afterThrowing");
    }
}

测试

正常顺序

postman访问:http://localhost:8080/user/print?id=321&userName=Tony&note=abc

后端结果

SimpleAspect.around1:around before
SimpleAspect.before
OrderTestAspect.around1:around before
OrderTestAspect.around1:around after
SimpleAspect.afterReturning
SimpleAspect.after
SimpleAspect.around1:around after

postman结果

{
    "id": 321,
    "userName": "Tony",
    "note": "abc"
}

颠倒顺序(低优先级调用proceed())

postman访问:http://localhost:8080/user/print?id=321&userName=Tony&note=abc

后端结果

OrderTestAspect.around1:around before
OrderTestAspect.around1:around after

postman结果

{
    "id": 321,
    "userName": "Tony",
    "note": "abc"
}

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/125523137