SpringAOP的实现方式

本文详细介绍如何使用Spring框架实现面向切面编程(AOP),包括通过Spring API、自定义类及注解三种方式设置切入点和切面,实现方法前、后增强。

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

1.使用SpringAPI实现AOP

<aop:config>
        <!-- 切入点:需要操作的目标类中的目标方法
            execution中只需要修改全类名
        -->
        <aop:pointcut id="pointcut" expression="execution(* 全类名.*(..))" />

        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

2.自定义类实现AOP

<!-- 自定义类 -->
<bean id="diy" class="com.jay.pojo.diy"/>
    
<aop:config>
        <!-- 切面 -->
        <aop:aspect ref="diy">
            <!-- 切入点 -->
            <aop:pointcut id="pointcut" expression="execution(* 全类名.*(..))"/>

            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
 </aop:config>

3.使用注解实现AOP

注解实现类
//必须写切面注解,否则无法切入
@Aspect
public class diy {

    @Before("execution(* com.jay.service.serviceImpl.*(..))")
    public void before(){
        System.out.println("这是before方法");
    }

    @After("execution(* com.jay.service.serviceImpl.*(..))")
    public void after(){
        System.out.println("这是after方法");
    }
}
XML文件配置
<bean id="service" class="com.jay.service.serviceImpl"/>

<!-- 注解实现的AOP类 -->
<bean id="diy" class="com.jay.pojo.diy"/>

<!-- 识别注解,自动代理 -->
<aop:aspectj-autoproxy/>

 

转载于:https://www.cnblogs.com/chenxi-mxj/p/11454567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值