0
点赞
收藏
分享

微信扫一扫

从零开始造Spring09---实现AOP的JDK代理

修炼之士 2022-05-05 阅读 96

前言

接上一篇从零开始造Spring08—AOP(介绍以及实现ReflectiveMethodInvocation和AopProxyFactory),这篇文章我们接着来讲Spring的AOP的JDK代理,这是学习刘欣老师的《从零开始造Spring》的学习笔记。

JDK代理的说明

与Cglib代理有所不同的是,JDK代理是针对接口的代理。所有要使用JDK代理必须要有接口。

测试类

public interface IPetStoreService {
    void placeOrder();
}
@Component(value = "petStoreService")
public class PetStoreService implements IPetStoreService {
    public PetStoreService() {
    }

    @Override
    public void placeOrder() {
        System.out.println("place order");
        MessageTracker.addMsg("place order");
    }
}

XML中的配置

        <context:component-scan
        base-package="com.jay.spring.service.v6">
    </context:component-scan>

    <!--作为一个切面-->
    <bean id="tx" class="com.jay.spring.tx.TransactionManager" />

    <aop:config>

        <aop:aspect ref="tx">
            <!--切点-->
            <aop:pointcut id="placeOrder"
                expression="execution(* com.jay.spring.service.v6.*.placeOrder(..))" />
            <aop:after-throwing pointcut-ref="placeOrder" method = "rollback"/>
            <aop:after-returning pointcut-ref="placeOrder"
                method="commit" />

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

JdkAopProxyFactory

。。。。。。。。。。。。。。。。。


版权原因,完整文章,请参考如下:从零开始造Spring09---实现AOP的JDK代理

举报

相关推荐

0 条评论