spring实例化三:CglibSubclassingInstantiationStrategy

 
     在SimpleInstantiationStrategy类中,留下了包含MethodOverride对象的bd对象定义未做实现,做了抽象。CglibSubclassingInstantiationStrategy类是其中的一种实现方式,采用cglib生成之类方式,实例化对象。
     
下面看SimpleInstantiationStrategy类抽象的两个方法的实现。
@Override
protected Object instantiateWithMethodInjection(RootBeanDefinition bd, String beanName, BeanFactory owner) {
    return instantiateWithMethodInjection(bd, beanName, owner, null);
}
 
@Override
protected Object instantiateWithMethodInjection(RootBeanDefinition bd, String beanName, BeanFactory owner,
        Constructor<?> ctor, Object... args) {
    // 
    return new CglibSubclassCreator(bd, owner).instantiate(ctor, args);
}

 

 
可以看出,全部的对象实例化,都集中到类CglibSubclassCreator的方法instantiate中。
 
private static class CglibSubclassCreator {
 
        private static final Class<?>[] CALLBACK_TYPES = new Class<?>[]
                {NoOp.class, LookupOverrideMethodInterceptor.class, ReplaceOverrideMethodInterceptor.class};
 
        private final RootBeanDefinition beanDefinition;
 
        private final BeanFactory owner;
 
        CglibSubclassCreator(RootBeanDefinition beanDefinition, BeanFactory owner) {
            this.beanDefinition = beanDefinition;
            this.owner = owner;
        }
 
 
        public Object instantiate(Constructor<?> ctor, Object... args) {
            Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
            Object instance;
            // 没传构造方法,直接实例化
            if (ctor == null) {
                instance = BeanUtils.instantiate(subclass);
            }
            else {
                try {
                    // 实例化对象
                    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);
                }
            }
            Factory factory = (Factory) instance;
            factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
                    new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
                    new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
            return instance;
        }
 
         // cglib生成子类
        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();
        }
    }

 

 
其他都是cglib动态代理的内容,这里不往细走了。 
 
posted @ 2016-08-11 20:44  li.zhiliang  阅读(1773)  评论(0编辑  收藏  举报