spring 5.x 纯注解使用aop

pom.xml



    4.0.0

    com.siyue
    demo
    1.0-SNAPSHOT
    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
        
            org.aspectj
            aspectjweaver
            1.9.6
        
    
    
        8
        8
        5.3.5
    


创建配置文件
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())
环绕后

你可能感兴趣的:(spring 5.x 纯注解使用aop)