《SSM笔记——Spring》9、AOP

本文详细介绍了Spring AOP在业务场景中的应用,包括AOP的概念、在Spring中的作用,以及如何通过API接口、自定义切面和注解实现AOP织入。重点讲解了如何使用`org.aspectjweaver`依赖和配置不同方式的切面编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为博主ExcelMann的原创文章,未经博主允许不得转载。

9、AOP

作者:ExcelMann,转载需注明。

9.1、什么是AOP

在这里插入图片描述在这里插入图片描述

9.2、AOP在Spring中的作用

作用

提供声明式事务;允许用户自定义切面;

一些名词的介绍(不用死记硬背):
在这里插入图片描述
在SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:
在这里插入图片描述
即:AOP可以在不改动原有代码的基础上,去增加新的功能。

9.3、使用Spring实现AOP

【重点】使用AOP织入,需要先导入一个依赖包!

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

方式一:采用Spring的API接口实现

  1. xml文件
<?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-->
    <bean id="userService" class="com.excelman.service.UserServiceImpl"/>
    <bean id="log" class="com.excelman.log.BeforeLog"/>
    <bean id="afterLog" class="com.excelman.log.AfterLog"/>

    <!--方式一:使用原生Spring AOP接口-->
    <!--配置AOP:需要导入aop的约束-->
    <aop:config>
        <!--切入点:expression为表达式,execution(要执行的位置 * * * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.excelman.service.UserServiceImpl.*(..))"/>

        <!--执行环绕增强-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>

    </aop:config>

</beans>
  1. Log类(需要加载的类)
public class BeforeLog implements MethodBeforeAdvice {

    //method:要执行的目标对象的方法
    //args:参数
    //target:目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

public class AfterLog implements AfterReturningAdvice {

    //returnValue:返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+",返回了"+returnValue);
    }
}
  1. 测试类
public class MyTest {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //因为代理的是接口,所以最终返回的代理类是接口
        UserService userService = (UserService) context.getBean("userService");

        userService.add();
    }
}

方式二:采用自定义的形式实现

  1. xml文件
<!--方式二:自定义类-->
<bean id="diy" class="com.excelman.diy.DiyPointCut"/>

<aop:config>
    <!--自定义切面(也就是要插入的类),ref为要引用的类-->
    <aop:aspect ref="diy">
        <!--定义切入点-->
        <aop:pointcut id="point" expression="execution(* com.excelman.service.UserServiceImpl.*(..))"/>

        <!--通知(即类中要执行的方法)-->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>
  1. 切面(自定义的类)
public class DiyPointCut {

    public void before(){
        System.out.println("=======方法执行前=======");
    }

    public void after(){
        System.out.println("=======方法执行后=======");
    }
}

  1. 测试(同上)

方式三:采用注解的形式实现

  1. 自定义切面,且采用注解定义
//使用注解,标记这个类为一个切面
@Aspect
public class AnnotationPointCut {

    //使用注解,标记该通知为before,且参数为切入点
    @Before("execution(* com.excelman.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=========方法执行前========");
    }

    @After("execution(* com.excelman.service.UserServiceImpl.*(..)))")
    public void after(){
        System.out.println("=========方法执行后========");
    }

    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.excelman.service.UserServiceImpl.*(..)))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");

        Object proceed = jp.proceed();  //执行方法

        System.out.println("环绕后");
    }

}

  1. xml文件中,注册切面的bean,并且开启aop注解
<!--第三种方式:使用注解-->
<bean id="annotationPointCut" class="com.excelman.diy.AnnotationPointCut"/>
<!--aop开启注解支持-->
<aop:aspectj-autoproxy/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值