记录Spring Boot 2.3.4.RELEASE版注解方式实现AOP和通知的执行顺序

1.advice 按照以下的顺序执行

输出结果:(正常和异常)

说明:Spring boot 2.3.4.RELEASE 版本使用的AOP是spring-aop-5.2.9.RELEASE,AOP的通知顺序不一样。

可以测试下Spring boot 2.1.1.RELEASE 版做对比,发现结果是不一样的。

2.代码实现

public class User {
    private String userName;
    private String password;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [userName=" + userName + ", password=" + password + "]";
    }
    
}
实体类:User
public interface UserService {
    public void printUser(User user);
}
Service接口
@Service
public class UserServiceImpl implements UserService {

    @Override
    public void printUser(User user) {
        System.out.println(user.toString());
    }

}
Service实现类
//定义切面
@Aspect
public class MyAspect {
    
    //定义切点
    @Pointcut("execution(* com.springbootTest.aop.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before() {
        System.out.println("-----Before-----");
    }
    
    @After("pointCut()")
    public void after() {
        System.out.println("-----After-----");
    }
    
    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("-----AfterReturning-----");
        //System.out.println("事务提交");
    }
    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("-----AfterThrowing-----");
        //System.out.println("事务回滚");
    }
    
    @Around("pointCut()")
    public void around(ProceedingJoinPoint pjp) throws  Throwable{
        System.out.println("around advise 1");
        pjp.proceed();//回调目标对象的原有方法
        System.out.println("around advise2");
    }
}
切面
@SpringBootApplication
public class AopApplication {

    @Bean("myAspect")
    public MyAspect getMyAspect() {
        return new MyAspect();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(AopApplication.class, args);
    }

}
Spring Boot启动类
<?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.4.RELEASE</version>
        <!-- <version>2.1.1.RELEASE</version> -->
        
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springbootTest.aop</groupId>
    <artifactId>aop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>aop测试</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- SpringBoot 核心包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- SpringBoot Web容器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot 拦截器 -->
        <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>
pom.xml

 

转载指明出处:https://www.cnblogs.com/dennyLee2025/p/13724981.html

posted @ 2020-09-24 16:57  渊渟岳  阅读(836)  评论(0编辑  收藏  举报