aop面向切面编程

本文深入探讨了面向切面编程(AOP)的概念及其在Spring框架中的应用,详细讲解了AOP的关键术语如关注点、切面、连接点和通知,并介绍了通过Spring API、自定义类和注解三种方式实现AOP的具体步骤。

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

1、面向切面编程(AOP)通过提供另外一种思考程序结构的途径来弥补面向对象编程(OOP)的不足。在OOP中模块化的关键单元是类,而在AOP中模块化的单元则是切面。切面能对关注点进行模块化。

2、AOP在Spring中作用:提供声明式服务;允许用户实现自定义切面;

3、名词解释

>关注点:增加的某个业务。如日志、安全、缓存、事务

>切面(Aspect):一个关注点的模块化

>连接点(Joinpoint):在程序执行过程中某个特定的点,一个连接点总是表示一个方法的执行

>通知(Advice):在切面的某个特定的连接点上执行的动作(通过配置使通知在某个或某些方法的前/后/中期执行)

通知类型:前置通知(before advice)

后置通知(After Returning advice)

环绕执行(前后都执行):Around

异常通知(throws advice):实现接口:ThrowsAdvice,这个接口起标记作用,接口中并未定义方法。

4、使用Spring实现AOP(三种方式)

>通过Spring的API实现

导包:aopalliance.jar;aspectjweaver.jar

//后置通知
public class AfterLog implements AfterReturningAdvice {
    //目标方法执行后执行的通知(返回值,被调用的方法对象,被调用方法对象的参数,被调用方法对象的目标对象)
    @Override
    public void afterReturning(Object returnValue,Method method,Object[] args,Object target) throws Throwable {
        system.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了,返回值是"+returnValue);
    }

}

<?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-2.5.xsd
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <bean id="userService" class="" />
        <!--配置通知类-->
        <bean id="log" class="" />
        <aop:config>
            <!--指定通知在哪个/些方法执行时执行,方法中..表示有参数,*表示所有(可以在任何位置*()表示所有无参方法)-->
            <aop:pointcut expression="execution(* com.service.add(..))" id="pointcut" />
            <aop:advisor advice-ref="log" pointcut-ref="pointcut" />
        </aop:config>

</beans>

>自定义类实现AOP

原本的通知类不再用实现指定接口,而是直接写入想要执行的方法,通过修改配置文件,实现该方法在指定处执行:

<aop:config>
    <!--指定类-->
    <aop:aspect ref="log">
        <!--在哪个/些方法前后执行-->
        <aop:pointcut expression="execution(* com.service.add(..))" id="pointcut" />
        <!--在指定方法(id)的什么时候(before)执行 执行(log类中的)什么方法-->
        <aop:before method="before" pointcut-ref="pointcut" />
        <aop:after method="after" pointcut-ref="pointcut" 
    </aop:aspect>
</aop:config>

>通过注解来实现

配置文件中:<aop:aspectj-autoproxy />

注解:

@Aspect
public class After {
    @After("execution(* com.service.impl.*.*(..))")
    public void after() {
        system.out.println("方法执行后");
    }
    @Around("execution(* com.service.impl.*.*(..))")
    public Object around(ProceedingJoinPoint jp) {
        system.out.println("环绕前");
        //方法签名(即输出执行的是哪个方法)
        system.out.println("签名:"+jp.getSingnature);
        //执行方法
        Object result = jp.proceed();
        system.out.println("环绕后");
        return result;
    }
}//输出时:先有环绕前,再有方法执行前;先有环绕后,再有方法执行后

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值