Spring中实现AOP的三种方式

1、什么是AOP

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。 面向方面的编程(AOP)是一种新的编程技术,它弥补了面向对象的编程(OOP)在跨越模块行为上的不足。AOP 引进了 Aspect,它将影响多个类的行为封装到一个可重用模块中,它允许程序员对横切关注点进行模块化,从而消除了 OOP 引起的代码混乱和分散问题,增强了系统的可维护性和代码的重用性

2、AOP在Spring中的作用

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …

  • 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。

  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。

  • 目标(Target):被通知对象。

  • 代理(Proxy):向目标对象应用通知之后创建的对象。

  • 切入点(PointCut):切面通知 执行的 “地点”的定义。

  • 连接点(JointPoint):与切入点匹配的执行点。

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:

64

即 Aop 在 不改变原有代码的情况下 , 去增加新的功能 .

3、使用Spring实现Aop

准备工作:需要导入一个依赖包:

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

第一种实现方式:

通过Spring API实现,编写业务接口和实现类

public interface UserService {
    /**
     * 增加
     */
    void add();
}
public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("执行了add方法");
    }
}

编写增强类,一个前置,一个后置

public class Log implements MethodBeforeAdvice {
    /**
     * target:目标对象
     * args:参数
     * method:目标对象的方法
     */
    @Override
    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:返回值
     */
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果"+returnValue);
    }
}

编写配置文件,注意导入aop约束

<?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
        https://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.lili.service.UserServiceImpl"/>
    <bean id="log" class="com.lili.log.Log"/>
    <bean id="afterLog" class="com.lili.log.AfterLog"/>

    <!--方式一:使用原生Spring API接口 -->
    <!--配置aop:需要导入aop的约束-->
        <aop:config>
        <!--切入点:expression:表达式,execution(要执行的位置! * * * * *) -->
        <aop:pointcut id="pointcut" expression="execution(* com.lili.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增加!-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
        </aop:config>
</beans>

测试:

    @Test
    public void test1(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }

输出:

com.lili.service.UserServiceImpl执行了add方法
执行了add方法
执行了add方法,返回结果null

第二种方式:

自定义类来实现AOP

编写植入类

public class OneSelfPointcut {
    public void before() {
        System.out.println("使用前");
    }

    public void after() {
        System.out.println("使用后");
    }
}

编写配置:

<beans>
<!--方式二:自定义类-->
        <bean id="oneSelfPointcut" class="com.lili.onself.OneSelfPointcut"/>
        <aop:config>
        <!--自定义切面, ref 要引用的类-->
        <aop:aspect ref="oneSelfPointcut">
        <!--切入点-->
        <aop:pointcut id="point" expression="execution(* com.lili.service.UserServiceImpl.*(..))"/>
        <!--通知-->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
        </aop:config>
</beans>

运行:

使用前
执行了add方法
使用后

第三种方式:

使用注解来实现AOP

编写使用注解实现的类

@Aspect// 标注此类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.lili.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("方法执行前");
    }
    @After("execution(* com.lili.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("方法执行后");
    }
}

编写配置:

    <!--方式三-->
    <bean id="annotationPointCut" class="com.lili.onself.AnnotationPointCut"/>
    <!--开启注解支持!   JDK(默认 proxy-target-class="false")   cglib(proxy-target-class="true")-->
    <aop:aspectj-autoproxy/>

运行:

方法执行前
执行了add方法
方法执行后
posted @   JamieChyi  阅读(43)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示