spring AOP

spring配置大概步骤:

接口:提供接口
目标类:要实现接口
切面类(功能增强类);切面有前置通知,后置通知,
代理类有3个配置:要实现接口,要实现切面(增强的部分),和委托类(目标类)

AOP注解开发

注解配置方式 AspectJ 框架允许使用注解定义切面、切入点和增强处理,而 Spring 框架则可以识别并 根据这些注解生成 AOP 代理。

4.2.3.1 常用注解注解名称

注解说明
@Before 前置通知
@AfterReturning 后置通知
@Around 环绕通知
@After 最终通知
@Pointcut 配置切点
@Aspect 指定切面
@Order 执行顺序(此注解由 Spring 提供)

配置切面类

点击查看代码
package com.bjsxt.MyAspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 配置切面类
 */
@Aspect
public class MyAspect {
    //配置切点
    @Pointcut("execution(* com.bjsxt.service.*.*(..))")
    public void myPointcut(){ }

    /**
     * 前置通知
     *
     * @param joinPoint
     */
    @Before("myPointcut()")
    public void myBefore(JoinPoint joinPoint) {
        System.out.println("myBefore..." + joinPoint.getSignature().getName());
    }

    /**
     * 后置通知
     *
     * @param joinPoint
     */
    @AfterReturning("myPointcut()")
    public void myAfterReturning(JoinPoint joinPoint) {
        System.out.println("myAfterReturning..." + joinPoint.getSignature().getName());
    }

    /**
     * 环绕通知
     *
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    //@Around("execution(* com.bjsxt.service.*.*(..))")
    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("aroundBefore...");
        Object obj = proceedingJoinPoint.proceed();
        System.out.println("aroundAfter...");
        return obj;
    }

    /**
     * 最总通知
     */
    @After("myPointcut()")
    public void myAfter() {
        System.out.println("myAfter...");
    }

    /**
     *
     */
    @AfterThrowing(value = "myPointcut()", throwing = "e")
    public void myAfterThrowing(Exception e) {
        System.out.println("myAfterThrowing:" + e);
    }

}

配置切面

点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    配置目标对象-->
    <bean id="usersService" class="com.bjsxt.service.impl.UsersServiceImpl"/>
<!--    配置切面对象-->
    <bean id="myAspect" class="com.bjsxt.MyAspect.MyAspect"/>
    <!--在 AspectJ 框架中开启注解处理。声明自动为 IOC 容器的那些配置了@AspectJ 的切面的 bean 对象创建代理,织入切面。-->
    <!--默认为 false:false:使用 JDK 的 Proxy 对象创建代理对象。true:使用 CGLIB 创建代理对象。如果目标对象实现了接口会使用 Proxy,如果目标 对象没有实现接口则自动使用 CGLIB 创建代理对象。-->
    <aop:aspectj-autoproxy proxy-target-class="false"/>
</beans>

遇到的bug:配置切面时,少加了括号。 @Before("myPointcut()")
image

posted @   风的指引  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示