spring 5.x 纯注解使用aop

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.siyue</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring.version>5.3.5</spring.version>
    </properties>

</project>

创建配置文件
src/main/java/com/siyue/demo/config/SpringConfig.java

package com.siyue.demo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan(basePackages = {"com.siyue.demo"})
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class SpringConfig {
}

创建个订单接口
src/main/java/com/siyue/demo/service/IOrderService.java

package com.siyue.demo.service;

public interface IOrderService {
    void createOrder();
}

创建订单实现类
src/main/java/com/siyue/demo/service/impl/OrderService.java

package com.siyue.demo.service.impl;

import com.siyue.demo.service.IOrderService;
import org.springframework.stereotype.Service;

@Service
public class OrderService implements IOrderService {
    public OrderService() {
    }

    public void createOrder() {
        System.out.println("创建订单");
    }
}

创建aop切面类
src/main/java/com/siyue/demo/component/AspectComponent.java

package com.siyue.demo.component;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AspectComponent {
    @Pointcut("execution(* com.siyue.demo.service.impl.*Service.*(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("方法执行前:" + joinPoint.toString());
    }

    @After("pointCut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("方法执行后:" + joinPoint.toString());
    }


    @AfterReturning(pointcut = "pointCut()", returning = "returnVal")
    public void afterReturning(JoinPoint joinPoint, Object returnVal) {
        System.out.println("方法返回前, 返回结果:"
                + returnVal + joinPoint.toString());
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object proceed;
        System.out.println("环绕前");
        try {
            proceed = pjp.proceed();
        } catch (Throwable ex) {
            System.out.println("环绕异常");
            throw ex;
        }
        System.out.println("环绕后");
        return proceed;
    }

    @AfterThrowing(pointcut = "pointCut()", throwing = "error")
    public void afterThrowing(JoinPoint joinPoint, Throwable error) {
        System.out.println("方法抛出异常:" + error + joinPoint.toString());
    }
}

创建启动入口
src/main/java/com/siyue/demo/Main.java

package com.siyue.demo;

import com.siyue.demo.config.SpringConfig;
import com.siyue.demo.service.IOrderService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        IOrderService orderService = ctx.getBean(IOrderService.class);
        orderService.createOrder();
    }
}

执行结果

环绕前
方法执行前:execution(void com.siyue.demo.service.impl.OrderService.createOrder())
创建订单
方法返回前, 返回结果:nullexecution(void com.siyue.demo.service.impl.OrderService.createOrder())
方法执行后:execution(void com.siyue.demo.service.impl.OrderService.createOrder())
环绕后
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容