spring初始化策略



实例化对象,spring核心用到两个类,分别是

java.lang.reflect.Constructor和org.springframework.cglib.proxy.Enhancer,具体是ctor.newInstance(args)和enhancer.create()。


1、bean的创建,主要是AbstractAutowireCapableBeanFactory.doCreateBean方法内。这个方法内,有bean的实例化、初始化、代理处理等,这里只说实例化的问题。AbstractAutowireCapableBeanFactory.doCreateBean方法内会调用AbstractAutowireCapableBeanFactory.createBeanInstance方法。

2、AbstractAutowireCapableBeanFactory.createBeanInstance方法会调用AbstractAutowireCapableBeanFactory.instantiateBean方法。

3、AbstractAutowireCapableBeanFactory.instantiateBean方法内,会执行getInstantiationStrategy().instantiate(mbd, beanName, parent)代码。先通过AbstractAutowireCapableBeanFactory.getInstantiationStrategy()方法会返回InstantiationStrategy接口的实例,这个方法内的实现是返回AbstractAutowireCapableBeanFactory类的instantiationStrategy属性,默认是CglibSubclassingInstantiationStrategy。然后调用InstantiationStrategy接口实例的instantiate方法。这里,默认调用的是CglibSubclassingInstantiationStrategy的instantiate方法。

public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory implements AutowireCapableBeanFactory {
.........
	/** Strategy for creating bean instances */
	private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();

	/**
	 * Return the instantiation strategy to use for creating bean instances.
	 */
	protected InstantiationStrategy getInstantiationStrategy() {
		return this.instantiationStrategy;
	}
.........
}


4、instantiate方法的调用。因为CglibSubclassingInstantiationStrategy类并没有instantiate方法,所以这里调用的是CglibSubclassingInstantiationStrategy类的父类SimpleInstantiationStrategy的instantiate方法。SimpleInstantiationStrategy类的instantiate方法内,根据类是否配置lookup-method或者replaced-method标签或者@Lookup注解,而调用两种不同的方式实例化bean。没有配置,则调用BeanUtils.instantiateClass(Constructor<T> ctor, Object... args)方法;配置了则调用CglibSubclassingInstantiationStrategy.instantiateWithMethodInjection(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner)方法。
public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationStrategy {...}
public class SimpleInstantiationStrategy implements InstantiationStrategy {...}

5、BeanUtils.instantiateClass方法的调用。BeanUtils.instantiateClass方法内,会通过反射类java.lang.reflect.Constructor的实例,实例化对象,代码ctor.newInstance(args)。

这个也反向说明了,在一定情况下,在spring中,如果自定义了构造方法,那么一定要实现无参构造方法。因为spring初始化时,因为配置原因,很可能最终调用的是类的无参构造方法初始化的。而实现了自定义构造方法后,编译器就不会再自动为我们生成无参构造函数了。

6、CglibSubclassingInstantiationStrategy.instantiateWithMethodInjection方法调用。方法内,会创建CglibSubclassCreator类实例,然后调用CglibSubclassCreator类实例的instantiate方法。CglibSubclassCreator类是CglibSubclassingInstantiationStrategy类的内部类。

import java.lang.reflect.Constructor;
import org.springframework.cglib.proxy.Enhancer;
public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationStrategy {

@Override
	protected Object instantiateWithMethodInjection(
			RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) {

		// Must generate CGLIB subclass.
		return new CglibSubclassCreator(beanDefinition, owner).instantiate(null, null);
	}

	@Override
	protected Object instantiateWithMethodInjection(
			RootBeanDefinition beanDefinition, String beanName, BeanFactory owner,
			Constructor<?> ctor, Object[] args) {

		return new CglibSubclassCreator(beanDefinition, owner).instantiate(ctor, args);
	}

/**
	 * An inner class created for historical reasons to avoid external CGLIB dependency
	 * in Spring versions earlier than 3.2.
	 */
	private static class CglibSubclassCreator {

		private static final Log logger = LogFactory.getLog(CglibSubclassCreator.class);

		private final RootBeanDefinition beanDefinition;

		private final BeanFactory owner;

		public CglibSubclassCreator(RootBeanDefinition beanDefinition, BeanFactory owner) {
			this.beanDefinition = beanDefinition;
			this.owner = owner;
		}

		/**
		 * Create a new instance of a dynamically generated subclass implementing the
		 * required lookups.
		 * @param ctor constructor to use. If this is {@code null}, use the
		 * no-arg constructor (no parameterization, or Setter Injection)
		 * @param args arguments to use for the constructor.
		 * Ignored if the {@code ctor} parameter is {@code null}.
		 * @return new instance of the dynamically generated subclass
		 */
		public Object instantiate(Constructor<?> ctor, Object[] args) {
			Enhancer enhancer = new Enhancer();
			enhancer.setSuperclass(this.beanDefinition.getBeanClass());
			enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
			enhancer.setCallbackFilter(new CallbackFilterImpl());
			enhancer.setCallbacks(new Callback[] {
					NoOp.INSTANCE,
					new LookupOverrideMethodInterceptor(),
					new ReplaceOverrideMethodInterceptor()
			});
			//使用cglib的create生成实例化的bean对象
			return (ctor != null ? enhancer.create(ctor.getParameterTypes(), args) : enhancer.create());
		}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值