当前位置: 代码迷 >> 综合 >> Spring5 源码阅读笔记(3.2)createProxy 创建代理
  详细解决方案

Spring5 源码阅读笔记(3.2)createProxy 创建代理

热度:87   发布时间:2024-01-26 22:31:18.0

跟代码

类 AbstractAutoProxyCreator

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);}//创建代理工厂ProxyFactory proxyFactory = new ProxyFactory();proxyFactory.copyFrom(this);/*** isProxyTargetClass()** true* 1、目标对象实现了接口 – 使用CGLIB代理机制* 2、目标对象没有接口(只有实现类) – 使用CGLIB代理机制** false* 1、目标对象实现了接口 – 使用JDK代理机制(代理所有实现了的接口)* 2、目标对象没有接口(只有实现类) – 使用CGLIB代理机制**默认false*/if (!proxyFactory.isProxyTargetClass()) {if (shouldProxyTargetClass(beanClass, beanName)) {//proxyTargetClass 是否对类进行代理,而不是对接口进行代理,设置为true时,使用CGLib代理。proxyFactory.setProxyTargetClass(true);}else {evaluateProxyInterfaces(beanClass, proxyFactory);}}//把advice类型的增强包装成advisor切面 见下3.2.1Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);proxyFactory.addAdvisors(advisors);proxyFactory.setTargetSource(targetSource);customizeProxyFactory(proxyFactory);////用来控制代理工厂被配置后,是否还允许修改代理的配置,默认为falseproxyFactory.setFrozen(this.freezeProxy);if (advisorsPreFiltered()) {proxyFactory.setPreFiltered(true);}//获取代理实例 见3.2.2return proxyFactory.getProxy(getProxyClassLoader());
}

3.2.1 buildAdvisors

protected Advisor[] buildAdvisors(@Nullable String beanName, @Nullable Object[] specificInterceptors) {// Handle prototypes correctly...//自定义MethodInterceptor.拿到AnnotationAwareAspectJAutoProxyCreator对象调用setInterceptorNames方法Advisor[] commonInterceptors = resolveInterceptorNames();List<Object> allInterceptors = new ArrayList<>();if (specificInterceptors != null) {allInterceptors.addAll(Arrays.asList(specificInterceptors));if (commonInterceptors.length > 0) {if (this.applyCommonInterceptorsFirst) {allInterceptors.addAll(0, Arrays.asList(commonInterceptors));}else {allInterceptors.addAll(Arrays.asList(commonInterceptors));}}}if (logger.isTraceEnabled()) {int nrOfCommonInterceptors = commonInterceptors.length;int nrOfSpecificInterceptors = (specificInterceptors != null ? specificInterceptors.length : 0);logger.trace("Creating implicit proxy for bean '" + beanName + "' with " + nrOfCommonInterceptors +" common interceptors and " + nrOfSpecificInterceptors + " specific interceptors");}Advisor[] advisors = new Advisor[allInterceptors.size()];for (int i = 0; i < allInterceptors.size(); i++) {//对自定义的advice要进行包装,把advice包装成advisor对象,切面对象advisors[i] = this.advisorAdapterRegistry.wrap(allInterceptors.get(i));}return advisors;
}

3.2.2 getProxy

类 proxyFactory

public Object getProxy(@Nullable ClassLoader classLoader) {//根据目标对象是否有接口来判断采用什么代理方式,cglib代理还是jdk动态代理return createAopProxy().getProxy(classLoader);
}

跟 getProxy:
类 AopProxy
在这里插入图片描述
选 JDKDynamicAopProxy
在这里插入图片描述

@Override
public Object getProxy(@Nullable ClassLoader classLoader) {if (logger.isTraceEnabled()) {logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource());}//advised是代理工厂对象Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true);findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);//熟悉的方法return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}
  相关解决方案