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>

創(chuàng)建配置文件
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 {
}

創(chuàng)建個訂單接口
src/main/java/com/siyue/demo/service/IOrderService.java

package com.siyue.demo.service;

public interface IOrderService {
    void createOrder();
}

創(chuàng)建訂單實現(xiàn)類
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("創(chuàng)建訂單");
    }
}

創(chuàng)建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("方法執(zhí)行前:" + joinPoint.toString());
    }

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


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

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object proceed;
        System.out.println("環(huán)繞前");
        try {
            proceed = pjp.proceed();
        } catch (Throwable ex) {
            System.out.println("環(huán)繞異常");
            throw ex;
        }
        System.out.println("環(huán)繞后");
        return proceed;
    }

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

創(chuàng)建啟動入口
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();
    }
}

執(zhí)行結(jié)果

環(huán)繞前
方法執(zhí)行前:execution(void com.siyue.demo.service.impl.OrderService.createOrder())
創(chuàng)建訂單
方法返回前, 返回結(jié)果:nullexecution(void com.siyue.demo.service.impl.OrderService.createOrder())
方法執(zhí)行后:execution(void com.siyue.demo.service.impl.OrderService.createOrder())
環(huán)繞后
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容