Spring AOP创建代理对象源码解析

引言

概述: AOP系列文章:
【1】Spring Aop初始化源码分析
【2】Spring AOP创建代理对象源码解析
【3】Spring AOP 链式调用过程源码解析
【4】Spring 事务执行过程源码解析

1 工程简介

在这里插入图片描述

1.1 pom

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.2.8.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <!-- 日志相关依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.10</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

1.2 AOP配置文件


@Configuration
@EnableAspectJAutoProxy
@Import({ServiceAopConfig.class, DaoAopConfig.class})
public class AopConfig {


}

@Configuration
@Aspect
public class DaoAopConfig {

    /**
     * 声明切点
     */
    @Pointcut("execution(* com.rosh.dao.*.*(..))")
    public void pointCut() {

    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("DaoAopConfig invoke around start");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("DaoAopConfig invoke around end");
        return proceed;
    }
}



@Configuration
@Aspect
public class ServiceAopConfig {

    /**
     * 声明切点
     */
    @Pointcut("execution(* com.rosh.service.*.*(..))")
    public void pointCut() {

    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("ServiceAopConfig invoke around start");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("ServiceAopConfig invoke around end");
        return proceed;
    }

    @Before("pointCut()")
    public void before(){
        System.out.println("ServiceAopConfig invoke before");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("ServiceAopConfig invoke after");
    }
}


1.3 StudentDao

@Repository
public class StudentDao {

    public void addStudent() {

        System.out.println("StudentDao invoke addStudent");

    }

}

1.4 Service

public interface StudentService {

     void addStudent();

}


@Service
public class StudentServiceImpl implements StudentService {

    @Override
    public void addStudent() {
        System.out.println("StudentServiceImpl invoke addStudent()");
    }
}


1.5 RoshTest

public class RoshTest {

    @Test
    public void mainTest(){

        AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext("com.rosh");
        StudentService studentService = (StudentService) applicationContext.getBean("studentServiceImpl");
        studentService.addStudent();

        System.out.println("===============分割线================");

        StudentDao studentDao=(StudentDao) applicationContext.getBean("studentDao");
        studentDao.addStudent();


    }

}

在这里插入图片描述

2 AbstractAutoProxyCreator概述

描述: 该类作用在,Spring Aop初始化源码分析中具体分析过,核心作用是,在bean初始化时,后置加强。
在这里插入图片描述
在这里插入图片描述

protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
		if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
			return bean;
		}
		if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
			return bean;
		}
		if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
			this.advisedBeans.put(cacheKey, Boolean.FALSE);
			return bean;
		}

		// Create proxy if we have advice.
		/**
		 * 【1】获取当前bean有效的Advisor,Advisor = advice + pointCut
		 */
		Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);

		if (specificInterceptors != DO_NOT_PROXY) {
			this.advisedBeans.put(cacheKey, Boolean.TRUE);

			/**
			 * 【2】创建代理对象
			 */
			Object proxy = createProxy(
					bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
			this.proxyTypes.put(cacheKey, proxy.getClass());
			return proxy;
		}

		this.advisedBeans.put(cacheKey, Boolean.FALSE);
		return bean;
	}

3 创建代理对象源码解析

3.1 createProxy

描述: 主流程调用方法AbstractAutoProxyCreator类createProxy 方法。

protected Object createProxy(Class<?> beanClass, @Nullable String beanName,
			@Nullable Object[] specificInterceptors, TargetSource targetSource) {

		if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
			AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
		}

		/**
		 *  【1】创建代理工厂,赋值。
		 */
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.copyFrom(this);

		/**
		 * 【2】 proxyTargetClass为true时代表使用cglib,为false使用jdk,默认值是false。
		 *  判断当前被代理对象是使用jdk增强还是使用cglib增强
		 *
		 */
		if (!proxyFactory.isProxyTargetClass()) {
			//根据最开始@EnableAspectJAutoProxy注解中的proxyTargetClass参数判断是否应该使用cglib代理
			if (shouldProxyTargetClass(beanClass, beanName)) {
				proxyFactory.setProxyTargetClass(true);
			}
			else {
				//如果是接口增强那么使用jdk,否则使用cglib
				evaluateProxyInterfaces(beanClass, proxyFactory);
			}
		}
		//构建advisor
		Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
		//把advisor加入到代理工厂中
		proxyFactory.addAdvisors(advisors);
		//把targetSource对象加入代理工厂(实际对象、被代理对象)
		proxyFactory.setTargetSource(targetSource);
		customizeProxyFactory(proxyFactory);

		proxyFactory.setFrozen(this.freezeProxy);
		if (advisorsPreFiltered()) {
			proxyFactory.setPreFiltered(true);
		}

		/**
		 * 【3】 获取代理对象
		 */
		return proxyFactory.getProxy(getProxyClassLoader());
	}

3.2 ProxyFactory 获取代理对象

描述: 先获取AOP代理

	public Object getProxy(@Nullable ClassLoader classLoader) {
		/**
		 *  创建AOP代理(JDK、CGLIB)并且获得代理对象
		 */
		return createAopProxy().getProxy(classLoader);
	}
protected final synchronized AopProxy createAopProxy() {
		if (!this.active) {
			activate();
		}
		return getAopProxyFactory().createAopProxy(this);
	}

@Override
	public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
		if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {

			// 如果targetClass是接口类,使用JDK来生成Proxy
			Class<?> targetClass = config.getTargetClass();
			if (targetClass == null) {
				throw new AopConfigException("TargetSource cannot determine target class: " +
						"Either an interface or a target is required for proxy creation.");
			}
			if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
				return new JdkDynamicAopProxy(config);
			}

			// 如果不是接口类,使用CGLIB生成Proxy
			return new ObjenesisCglibAopProxy(config);
		}
		else {
			return new JdkDynamicAopProxy(config);
		}
	}

3.3 根据AOP不同的代理获取代理对象

在这里插入图片描述
在这里插入图片描述

4 StudentDao 代理Debug

描述: 每个代理对象都对应一个代理工厂,工厂是new 创建的。
在这里插入图片描述
描述: 选择cglib、jdk代理生成代理对象。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
描述: 创建AOP代理。
在这里插入图片描述
描述: CglibAopProxy获取代理对象。
在这里插入图片描述
描述: 获取到cglib增强代理对象。
在这里插入图片描述

5 StudentServiceImpl 代理Debug

描述: StudentServiceImpl 实现StudentService接口使用jdk代理。
在这里插入图片描述
描述: AOP获取JDK代理。
在这里插入图片描述
描述: 获取代理对象。
在这里插入图片描述
在这里插入图片描述

  • 25
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 23
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

响彻天堂丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值