• Springboot1.x与Springboot2.x版本下aop执行顺序


    Springboot1.x与Springboot2.x版本下aop执行顺序

    Springboot1.x 集成的Spring版本为4.x,Springboot2.x 集成的Spring版本为5.x。

    创建SpringBoot项目

    1.x版本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>1.5.9.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.chinda</groupId>
        <artifactId>interview</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>interview</name>
    
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    2.x版本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.3.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.chinda</groupId>
        <artifactId>interview</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>interview</name>
    
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        
    </project>
    

    编写AOP切面

    此案例是用注解实现的切面

    AopAnnotation

    package com.chinda.interview.annotation;
    
    import java.lang.annotation.*;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface AopAnnotation {
    }
    

    AopAspect

    package com.chinda.interview.aspect;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    /**
     * @author Wang Chinda
     * @date 2021/4/11
     */
    @Aspect
    @Component
    public class AopAspect {
    
        @Pointcut("@annotation(com.chinda.interview.annotation.AopAnnotation)")
        private void pointcut() {
    
        }
    
        @Before("pointcut()")
        public void beforeNotify() {
            System.out.println("*************************** @Before 我是前置通知AopAspect");
        }
    
        @After("pointcut()")
        public void afterNotify() {
            System.out.println("*************************** @After 我是后置通知AopAspect");
        }
    
        @AfterReturning("pointcut()")
        public void returnNotify() {
            System.out.println("*************************** @AfterReturning 我是返回通知AopAspect");
        }
    
        @AfterThrowing("pointcut()")
        public void throwingNotify() {
            System.out.println("*************************** @AfterThrowing 我是异常通知AopAspect");
        }
    
        @Around("pointcut()")
        public Object around(ProceedingJoinPoint joinPoint) {
            Object proceed = null;
            try {
                System.out.println("=========================@Around 我是环绕通知目标方法执行前");
                proceed = joinPoint.proceed();
                System.out.println("=========================@Around 我是环绕通知目标方法执行后");
            } catch (Throwable throwable) {
                System.out.println("=========================@Around 我是环绕通知的执行异常");
                throwable.printStackTrace();
            } finally {
                System.out.println("=========================@Around 我是环绕通知的finally");
            }
            return proceed;
        }
    }
    

    AopService

    package com.chinda.interview.service;
    
    /**
     * @author Wang Chinda
     * @date 2021/4/11
     */
    public interface AopService {
        int div(int x, int y);
    }
    

    AopServiceImpl

    package com.chinda.interview.service.impl;
    
    import com.chinda.interview.annotation.AopAnnotation;
    import com.chinda.interview.service.AopService;
    import org.springframework.stereotype.Service;
    
    /**
     * @author Wang Chinda
     * @date 2021/4/11
     */
    @Service
    public class AopServiceImpl implements AopService {
    
        @Override
        @AopAnnotation
        public int div(int x, int y) {
            return x / y;
        }
    }
    

    编写测试用例

    1.x 版本

    package com.chinda.interview;
    
    import com.chinda.interview.service.AopService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringBootVersion;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.core.SpringVersion;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class InterviewApplicationTests {
        @Autowired
        private AopService aopService;
    
    
        @Test
        public void boot1() {
            System.out.println("Spring版本: " + SpringVersion.getVersion() + "	 SpringBoot版本: " + SpringBootVersion.getVersion());
            System.out.println();
            aopService.div(10, 2);
        }
    
        @Test
        public void boot1Throwing() {
            System.out.println("Spring版本: " + SpringVersion.getVersion() + "	 SpringBoot版本: " + SpringBootVersion.getVersion());
            System.out.println();
            aopService.div(10, 0);
        }
    
        @Test
        public void boot2() {
            System.out.println("Spring版本: " + SpringVersion.getVersion() + "	 SpringBoot版本: " + SpringBootVersion.getVersion());
            System.out.println();
            aopService.div(10, 2);
        }
    
        @Test
        public void boot2Throwing() {
            System.out.println("Spring版本: " + SpringVersion.getVersion() + "	 SpringBoot版本: " + SpringBootVersion.getVersion());
            System.out.println();
            aopService.div(10, 0);
        }
    
    }
    

    2.x版本

    package com.chinda.interview;
    
    import com.chinda.interview.service.AopService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringBootVersion;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.core.SpringVersion;
    
    @SpringBootTest
    class InterviewApplicationTests {
        @Autowired
        private AopService aopService;
    
    
        @Test
        void boot1() {
            System.out.println("Spring版本: " + SpringVersion.getVersion() + "	 SpringBoot版本: " + SpringBootVersion.getVersion());
            System.out.println();
            aopService.div(10, 2);
        }
    
        @Test
        void boot1Throwing() {
            System.out.println("Spring版本: " + SpringVersion.getVersion() + "	 SpringBoot版本: " + SpringBootVersion.getVersion());
            System.out.println();
            aopService.div(10, 2);
        }
    
        @Test
        void boot2() {
            System.out.println("Spring版本: " + SpringVersion.getVersion() + "	 SpringBoot版本: " + SpringBootVersion.getVersion());
            System.out.println();
            aopService.div(10, 2);
        }
    
        @Test
        void boot2Throwing() {
            System.out.println("Spring版本: " + SpringVersion.getVersion() + "	 SpringBoot版本: " + SpringBootVersion.getVersion());
            System.out.println();
            aopService.div(10, 2);
        }
    
    }
    

    结果比对

    throwing

    normal

  • 相关阅读:
    [LeetCode] 456. 132 Pattern
    [LeetCode] 606. Construct String from Binary Tree
    [LeetCode] 536. Construct Binary Tree from String
    [LeetCode] 925. Long Pressed Name
    [LeetCode] 652. Find Duplicate Subtrees
    [LeetCode] 743. Network Delay Time
    [LeetCode] 1209. Remove All Adjacent Duplicates in String II
    [LeetCode] 1047. Remove All Adjacent Duplicates In String
    [LeetCode] 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
    [LeetCode] 859. Buddy Strings
  • 原文地址:https://www.cnblogs.com/chinda/p/14644709.html
Copyright © 2020-2023  润新知