spring--aop_1_源码分析之JdkDynamicAopProxy实现

本文详细分析了Spring AOP中JdkDynamicAopProxy的实现,从ProxyFactoryBean的角度阐述了如何通过JDK动态代理创建AopProxy对象。内容包括ProxyFactoryBean的配置、AopProxy接口的作用、JdkDynamicAopProxy作为InvocationHandler的实现以及拦截器的调用流程。重点关注了JdkDynamicAopProxy的invoke方法和ReflectiveMethodInvocation的proceed方法在AOP功能中的核心作用。
摘要由CSDN通过智能技术生成

aop 实现有两种方式
1. ProxyFactoryBean方式: 这种方式是通过配置实现
2. ProxyFactory方式:这种方式是通过编程实现

这里说 ProxyFactoryBean ,先上一张ProxyFactoryBean 的关系图,后面能用到。

示例代码:

/**
 * 代理接口,如果不是接口使用CGLIB代理
 */
public interface ProxyInterface {
    void m();
    void t();
}
/**
 * 目标对象,接口的实现类
 */
public class Target implements ProxyInterface {
    @Override
    public void m() {
        System.out.println("m");
    }

    @Override
    public void t() {
        System.out.println("t");
    }
}

/**
 * 通知器
 */
public class BeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("BeforeAdvice.....");
    }
}
/**
 *  通知器
 */
public class AfterAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("AfterAdvice.....");
    }
}

定义一个接口ProxyInterface, 实现类为 Target,声明了两个advice,用来增强Target 方法。配置文件如下(advice 配置的顺序就是调用的顺序):

<?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-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 目标类 -->
    <bean id="target" class="com.masz.springtest.aop.proxy.Target"/>

    <!-- advice -->
    <bean id="beforeAdvice" class="com.masz.springtest.aop.proxy.BeforeAdvice"/>
    <bean id="afterAdvice" class="com.masz.springtest.aop.proxy.AfterAdvice"/>

    <!-- 增强代理对象,ProxyFactoryBean 是一个FactoryBean , getBean() 时会调用它的getObject() 方法 -->
    <bean id="targetProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

        <!-- 代理接口,如果没有接口使用CGLIB生成代理类-->
        <property name="proxyInterfaces">
            <list>
                <value>com.masz.springtest.aop.proxy.ProxyInterface</value>
            </list>
        </property>
        <property name="singleton" value="true"/>  <!-- 是否是单例 -->
        <property name="target" ref="target"/>  <!-- 指定增强目标bean -->

        <!-- 拦截器名称 -->
        <property name="interceptorNames">
            <list>
                <value>beforeAdvice</value>
                <value>afterAdvice</value>
            </list>
        </property>
    </bean>
</beans>

测试:

public static void main(String[] args) {
	ConfigurableApplicationContext applicationContext = SpringApplication.run(AopTest.class, args);

	//直接获取目标类
	Target t = applicationContext.getBean(Target.class);
	t.m();

	//获取目标的代理。 关键的是:ProxyFactoryBean  是一个FactoryBean
	ProxyInterface  proxy = (ProxyInterface)applicationContext.getBean("targetProxy");
	proxy.m();
	System.exit(0);
}

运行结果:

m
BeforeAdvice.....
m
AfterAdvice.....

第一个方法没有被增强,第二个方法被增强了。

 

源码分析:

从代码中可以看到,通过applicationContext.getBean()获取ProxyFactoryBean, ProxyFactoryBean 是一个FactoryBean 所以getObject() 方法就是获取代理对象的入口方法。

ProxyFactoryBean.getObject() 源码:

@Override
@Nullable
public Object getObject() throws BeansException {
	//初始化advisor 链
	initializeAdvisorChain();
	
	//对si
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值