同步操作将从 huifer/Code-Analysis 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
类全路径: org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy
类图
整个类中重点关注 内部类即可. 方法可以忽略. 主要都在内部类上. 对外公开的方法也是依赖内部类执行
private static class CglibSubclassCreator {
/**
* 回调类型
*/
private static final Class<?>[] CALLBACK_TYPES = new Class<?>[]
{ NoOp.class, LookupOverrideMethodInterceptor.class, ReplaceOverrideMethodInterceptor.class };
/**
* beanDefinition
*/
private final RootBeanDefinition beanDefinition;
/**
* bean Factory
*/
private final BeanFactory owner;
}
org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.CglibSubclassCreator#instantiate
这段代码涉及到 CGLIB 这一块内容. 在这里不做具体展开. 仅作方法的分析
public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {
// 创造 CGLIB 对原有 BeanClass 的增强类
Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
Object instance;
if (ctor == null) {
// 构造函数为空的情况下创建
instance = BeanUtils.instantiateClass(subclass);
}
else {
try {
// 从 subClass 中查询对应的 构造函数
Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
// 创建对象
instance = enhancedSubclassConstructor.newInstance(args);
}
catch (Exception ex) {
throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
"Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
}
}
// SPR-10785: set callbacks directly on the instance instead of in the
// enhanced class (via the Enhancer) in order to avoid memory leaks.
Factory factory = (Factory) instance;
// 设置 Factory 的回调
factory.setCallbacks(new Callback[] { NoOp.INSTANCE,
new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner) });
return instance;
}
在这里第一行出现了一个方法createEnhancedSubclass
这个方法主要作用是通过 Spring 自己做的 CGLIB 分装将 beanClass 通过 cglib 进行增强
/**
* Create an enhanced subclass of the bean class for the provided bean
* definition, using CGLIB.
* 创造出 CGLIB 对 BeanClass 的增强子类
*/
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanDefinition.getBeanClass());
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
if (this.owner instanceof ConfigurableBeanFactory) {
ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
}
enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
enhancer.setCallbackTypes(CALLBACK_TYPES);
return enhancer.createClass();
}
在得到增强类后在做创建行为和,后续资源的补充
创建形式又回到了BeanUtils.instantiateClass
方法,这种事传入的 构造函数为空的情况下执行
当构造函数不为空的情况下会进行getConstructor
方法来找到构造函数
通过构造函数newInstance
创建
最后的资源补充是指 回调方法的设置
factory.setCallbacks(new Callback[] { NoOp.INSTANCE,
new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner) });
类全路径:org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.CglibIdentitySupport
作用: 存储 BeanDefinition
private static class CglibIdentitySupport {
private final RootBeanDefinition beanDefinition;
public CglibIdentitySupport(RootBeanDefinition beanDefinition) {
this.beanDefinition = beanDefinition;
}
public RootBeanDefinition getBeanDefinition() {
return this.beanDefinition;
}
@Override
public boolean equals(@Nullable Object other) {
return (other != null && getClass() == other.getClass() &&
this.beanDefinition.equals(((CglibIdentitySupport) other).beanDefinition));
}
@Override
public int hashCode() {
return this.beanDefinition.hashCode();
}
}
org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.MethodOverrideCallbackFilter
MethodOverrideCallbackFilter
中有一个方法来判断 方法重载的形式.
if (methodOverride == null) {
return PASSTHROUGH;
}
else if (methodOverride instanceof LookupOverride) {
return LOOKUP_OVERRIDE;
}
else if (methodOverride instanceof ReplaceOverride) {
return METHOD_REPLACER;
}
整体上这个类没有什么特别的难点.
类全路径: org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.LookupOverrideMethodInterceptor
LookupOverrideMethodInterceptor
通过 LookupOverride
获取 实例
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
// Cast is safe, as CallbackFilter filters are used selectively.
LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
Assert.state(lo != null, "LookupOverride not found");
Object[] argsToUse = (args.length > 0 ? args : null); // if no-arg, don't insist on args at all
if (StringUtils.hasText(lo.getBeanName())) {
return (argsToUse != null ? this.owner.getBean(lo.getBeanName(), argsToUse) :
this.owner.getBean(lo.getBeanName()));
}
else {
return (argsToUse != null ? this.owner.getBean(method.getReturnType(), argsToUse) :
this.owner.getBean(method.getReturnType()));
}
}
类全路径: org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.ReplaceOverrideMethodInterceptor
ReplaceOverrideMethodInterceptor
通过ReplaceOverride
进行实例化,在这里还需要涉及到一个接口MethodReplacer
.
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
ReplaceOverride ro = (ReplaceOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
Assert.state(ro != null, "ReplaceOverride not found");
// TODO could cache if a singleton for minor performance optimization
MethodReplacer mr = this.owner.getBean(ro.getMethodReplacerBeanName(), MethodReplacer.class);
return mr.reimplement(obj, method, args);
}
MethodReplacer
其实是我们开发者在编写代码时候使用到的, 使用方式查看面这段代码
<bean id="apple" class="org.source.hot.spring.overview.ioc.bean.lookup.Apple">
<replaced-method replacer="methodReplacerApple" name="hello" >
<arg-type>String</arg-type>
</replaced-method>
</bean>
<bean id="methodReplacerApple" class="org.source.hot.spring.overview.ioc.bean.lookup.MethodReplacerApple">
</bean>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。