Spring AOP两种实现方式

本文详细介绍如何在Spring框架中使用AOP进行代码增强,包括通过实现Spring的Advice接口和自定义类实现AOP两种方式,以及在applicationContext.xml配置文件中的具体配置步骤。

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

在pom.xml文件中需要导入依赖

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

新建一个用于测试的Service类

public class UserService {
    public void addUser(){
        System.out.println("增加用户");
    }
    public void deleteUser(){
        System.out.println("删除用户");
    }
}

方式一:使用Spring的API接口

Spring AOP 提供了 Advice 接口多个子接口来支持增强。如下所示:

接口 MethodBeforeAdvice:在目标方法调用之前调用的;
接口 AfterReturningAdvice:在目标方法调用并返回之后调用;
接口 MethodInterceptor:在目标方法的执行前后有效;
接口 ThrowsAdvice:在目标方法抛出异常时调用;

第一步:

新建AfterReturningLog实现AfterReturningAdvice接口

public class AfterReturningLog implements AfterReturningAdvice {
    //returnValue: 返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了" + method.getName() + "返回结果是:" + returnValue);
    }
}

新建BeforeLog实现MethodBeforeAdvice接口

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() + "被执行了");
    }
}

第二步:

在applicationContext.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 https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 注册bean -->
    <bean id="userService" class="com.zhangzhilin.service.UserService"></bean>
    <bean id="beforeLog" class="com.zhangzhilin.log.BeforeLog"/>
    <bean id="afterReturningLog" class="com.zhangzhilin.log.AfterReturningLog"/>

    <!-- 配置aop-->
    <aop:config>
        <!-- 切入点:expression:表达式 execution(要执行的位置)-->
        <aop:pointcut id="pointcut" expression="execution(* com.zhangzhilin.service.UserService.*(..))"/>
        <!-- 配置环绕增强 -->
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterReturningLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

方式二:自定义类实现AOP

第一步:

新建DiyLog类作为切面

public class DiyLog {
    public void before(){
        System.out.println("方法执行前");
    }
    public  void after(){
        System.out.println("方法执行后");
    }
}

第二步:

在applicationContext.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 https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 注册bean -->
    <bean id="diyAspect" class="com.zhangzhilin.log.DiyLog"/>
    
	<!-- 配置aop-->
    <aop:config>
    	<!-- 配置切面 -->
        <aop:aspect ref="diyAspect">
            <aop:pointcut id="pointcut" expression="execution(* com.zhangzhilin.service.UserService.*(..))"/>
            <!-- 这里的method指向自定义类中的方法 -->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值