论坛首页 Java企业应用论坛

从源代码解读spring IOC容器

浏览 6631 次
精华帖 (2) :: 良好帖 (6) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-09-19   最后修改:2008-11-27
spring IOC容器(Inversion of Control container)做项目的时候经常要用到,但是好像感觉不到它的存在,因为代码里面很少用到spring,主要是写配置。但是我不得不说:spring做的确实很巧妙,这种和代码的松耦合很大的提高了代码的灵活性和可扩展性。最近手头的开发任务比较少,于是抽空看了看spring1.2.9的源代码,现在把心得贴出来和网友共享,欢迎大家批评指正。


1. spring IOC容器接口定义

org.springframework.beans.factory 这里是定义spring IOC容器接口的包,在这个包里有我们熟悉的BeanFactory

package org.springframework.beans.factory;

public interface BeanFactory {
    
	/**
	 *这里是对FactoryBean的转义定义,如果使用bean的名字检索FactoryBean,得到的是工厂生成的对象,   
	 *如果需要得到工厂本身,需要转义。For example, if the bean named
	 * <code>myJndiObject</code> is a FactoryBean, getting <code>&myJndiObject</code>
	 * will return the factory 
	 */         
	String FACTORY_BEAN_PREFIX = "&";   
  
  	/**
	 *这里根据bean的名字,在IOC容器中得到bean实例,这个IOC容器就是一个大的抽象工厂。
	 */   
	Object getBean(String name) throws BeansException;   
  
	/**
	 *这里根据bean的名字和Class类型来得到bean实例,和上面的方法不同在于它会抛出异常:如果
	 *根据名字取得的bean实例的Class类型和需要的不同的话。
	 */   
	Object getBean(String name, Class requiredType) throws BeansException;   
  
	/**
	*这里提供对bean的检索,看看是否在IOC容器有这个名字的bean
	*/   
	boolean containsBean(String name);   
  
	/**
	 *这里根据bean名字得到bean实例,并同时判断这个bean是不是单例
	 */  
	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;   
  
	/**
	 *这里得到bean实例的Class类型 
	 */
	Class getType(String name) throws NoSuchBeanDefinitionException;   
  
	/**
	 *这里得到bean的别名,如果根据别名检索,那么其原名也会被检索出来
	 */   
	String[] getAliases(String name);   
}


在BeanFactory里只对IOC容器的基本行为作了定义,根本不关心你的bean是怎样定义怎样加载的,就像我们只关心从这个工厂里能得到什么产品对象,至于工厂是怎么生产这些对象的,这个基本的接口不关心这些。如果要关心工厂是怎样产生对象的,应用程序需要使用具体的IOC容器实现

spring已经为我们准备了丰富的IOC容器的具体实现(当然,有兴趣的朋友也可以尝试实现自己的一套IOC容器来证明自己的实力也未尝不可),现在先让我们看看spring是怎样实现的:


2. spring IOC容器接口的实现

package org.springframework.beans.factory.support;

public abstract class AbstractBeanFactory implements ConfigurableBeanFactory {
	
	/** 
	 * 放置单例bean实例的缓存(IOC容器)
	 * Cache of singletons: bean name --> bean instance 
	 */
	private final Map singletonCache = CollectionFactory.createLinkedMapIfPossible(16);
	
	/**
	 * 根据bean名称从缓存(IOC容器)中获得bean实例,如果缓存中没有,则根据bean的定义
	 * 信息造bean的实例,如果是单例,就放进缓存(IOC容器)
	 */
	public Object getBean(String name, Class requiredType, Object[] args) throws BeansException {
		String beanName = transformedBeanName(name);
		Object bean = null;

		// 在缓存中根据名字查找bean实例.
		Object sharedInstance = null;
		synchronized (this.singletonCache) {
			sharedInstance = this.singletonCache.get(beanName);
		}
		if (sharedInstance != null) {
			...
			// 如果容器里找的到,则从缓存返回bean
			bean = getObjectForSharedInstance(name, sharedInstance);
		} else {
 			...
			// Check if bean definition exists in this factory.
			// 如果当前BeanFactory里没有bean定义信息,则去ParentBeanFactory
			// 里查找
			if (getParentBeanFactory() != null && !containsBeanDefinition(beanName)) {
				// Not found -> check parent.
				if (getParentBeanFactory() instanceof AbstractBeanFactory) {
					// Delegation to parent with args only possible for AbstractBeanFactory.
					return ((AbstractBeanFactory) getParentBeanFactory()).getBean(name, requiredType, 

args);
				}
				else if (args == null) {
					// No args -> delegate to standard getBean method.
					return getParentBeanFactory().getBean(name, requiredType);
				}
				else {
					...
				}
			}
			// 获得bean定义信息
			RootBeanDefinition mergedBeanDefinition = getMergedBeanDefinition(beanName, false);
			checkMergedBeanDefinition(mergedBeanDefinition, beanName, requiredType, args);

			// Create bean instance.
			if (mergedBeanDefinition.isSingleton()) {
				synchronized (this.singletonCache) {
					// Re-check singleton cache within synchronized block.
					sharedInstance = this.singletonCache.get(beanName);
					if (sharedInstance == null) {
						... 
						this.currentlyInCreation.add(beanName);
						try {
							// 根据bean定义信息造bean的实例
							sharedInstance = createBean(beanName, mergedBeanDefinition, args);
							// 将bean实例放入缓存(IOC容器)
							addSingleton(beanName, sharedInstance);
						}
						catch (BeansException ex) {
							... 
						}
						finally {
							this.currentlyInCreation.remove(beanName);
						}
					}
				}
				bean = getObjectForSharedInstance(name, sharedInstance);
			}
			else {
				// 如果不是单例模式 just create a new instance.
				bean = createBean(beanName, mergedBeanDefinition, args);
			}
		}
		...
		return bean;
	}

	/**
	 * 抽象方法,在子类中实现
	 */
	protected abstract Object createBean(
			String beanName, RootBeanDefinition mergedBeanDefinition, Object[] args) throws 

BeanCreationException;

	/**
	 * 在缓存中注册bean实例
	 */
	public void registerSingleton(String beanName, Object singletonObject) throws BeanDefinitionStoreException {
		Assert.hasText(beanName, "Bean name must not be empty");
		Assert.notNull(singletonObject, "Singleton object must not be null");
		synchronized (this.singletonCache) {
			Object oldObject = this.singletonCache.get(beanName);
			if (oldObject != null) {
				... 
			}
			addSingleton(beanName, singletonObject);
		}
	}

	/**
	 * 在缓存中注册bean实例
	 */
	protected void addSingleton(String beanName, Object singletonObject) {
		Assert.hasText(beanName, "Bean name must not be empty");
		Assert.notNull(singletonObject, "Singleton object must not be null");
		synchronized (this.singletonCache) {
			this.singletonCache.put(beanName, singletonObject);
		}
	}

}


AbstractBeanFactory实现了BeanFactory接口里的getBean()方法,做为抽象的父类为所有继承它的子类提供getBean()的模板服务,getBean()方法是根据bean的名称在缓存--AbstractBeanFactory#singletonCache(IOC容器)里查找其实例,如果找不到则根据用户的配置文件里的bean定义信息(spring背后有一套功能十分强大的xml文件解析工具来完成bean定义文件的解析,有兴趣的朋友可以看看org.springframework.beans.factory.xml.XmlBeanDefinitionReader和org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser,解析好后会将bean定义信息放入缓存,限于篇幅这里不再赘述)造bean实例,如果该bean是单例模式,则同时放进缓存---AbstractBeanFactory#singletonCache。getBean()是IOC容器的核心方法。在getBean()里用到了createBean()方法,顾名思义--造bean实例的方法,该方法在子类中得到了实现,现在我们看看子类中到底是怎样createBean的。

package org.springframework.beans.factory.support;

public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory implements AutowireCapableBeanFactory {
	
	/**
	 * 造bean对象
	 */
	protected Object createBean(String beanName, RootBeanDefinition mergedBeanDefinition, Object[] args)
			throws BeanCreationException {

		// 实例化并初始化当前bean所依赖的所有bean.
		if (mergedBeanDefinition.getDependsOn() != null) {
			for (int i = 0; i < mergedBeanDefinition.getDependsOn().length; i++) {
				getBean(mergedBeanDefinition.getDependsOn()[i]);
			}
		}
		...
 		Object bean = null;

		// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
		if (mergedBeanDefinition.hasBeanClass()) {
			bean = applyBeanPostProcessorsBeforeInstantiation(mergedBeanDefinition.getBeanClass(), beanName);
			if (bean != null) {
				return bean;
			}
		}

		BeanWrapper instanceWrapper = null;
		Object originalBean = null;
		... 
		try {
			... 
			if (mergedBeanDefinition.getFactoryMethodName() != null)  {
				//反射factoryBean的工厂方法
				instanceWrapper = instantiateUsingFactoryMethod(beanName, mergedBeanDefinition, args);
			}
			else if (mergedBeanDefinition.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR 	

			|| mergedBeanDefinition.hasConstructorArgumentValues() )  {
				//实例化bean的方法
				instanceWrapper = autowireConstructor(beanName, mergedBeanDefinition);
			}
			else {
				//实例化bean的方法
				// No special handling: simply use no-arg constructor.
				instanceWrapper = instantiateBean(beanName, mergedBeanDefinition);
			}
			bean = instanceWrapper.getWrappedInstance();

			// Eagerly cache singletons to be able to resolve circular references
			// even when triggered by lifecycle interfaces like BeanFactoryAware.
			if (isAllowCircularReferences() && isSingletonCurrentlyInCreation(beanName)) {
				... 
				addSingleton(beanName, bean);
			}

 			...
			originalBean = bean;
			bean = applyBeanPostProcessorsBeforeInitialization(bean, beanName);
			// 调用bean实例里的init方法
			invokeInitMethods(beanName, bean, mergedBeanDefinition);
			bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
		}
		catch (BeanCreationException ex) {
			... 
		}

		// Register bean as disposable, and also as dependent on specified "dependsOn" beans.
		registerDisposableBeanIfNecessary(beanName, originalBean, mergedBeanDefinition);

		return bean;
	}

	/**
	 * 反射factoryBean里的工厂方法,实现依赖注入
	 */
	protected BeanWrapper instantiateUsingFactoryMethod(
			String beanName, RootBeanDefinition mergedBeanDefinition, Object[] explicitArgs) throws 

BeansException {
		... 
		BeanWrapperImpl bw = new BeanWrapperImpl();
		initBeanWrapper(bw);
		...
		Object beanInstance = this.instantiationStrategy.instantiate(
				mergedBeanDefinition, beanName, this, factoryBean, factoryMethodToUse, argsToUse);
 		...
		bw.setWrappedInstance(beanInstance);
		return bw;
	}

	/**
	 * 匹配bean构造方法来实例化bean
	 */
	protected BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mergedBeanDefinition)
			throws BeansException {
		...
		BeanWrapperImpl bw = new BeanWrapperImpl();
		initBeanWrapper(bw);
		...
		Object beanInstance = this.instantiationStrategy.instantiate(
				mergedBeanDefinition, beanName, this, constructorToUse, argsToUse);
		bw.setWrappedInstance(beanInstance);
		...
		return bw;
	}
	/**
	 * 用bean的默认构造方法造bean实例
	 */
	protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mergedBeanDefinition)
			throws BeansException {

		Object beanInstance = getInstantiationStrategy().instantiate(mergedBeanDefinition, beanName, this);
		BeanWrapper bw = new BeanWrapperImpl(beanInstance);
		initBeanWrapper(bw);
		return bw;
	}
}


其实create bean的过程就是通过bean的定义信息反射bean实例的过程。真正用反射造对象的代码这里没有体现出来,我们注意到了BeanWrapper仅仅是bean实例的包装类,不涉及到造对象的代码;instantiateUsingFactoryMethod()、autowireConstructor()和instantiateBean()方法里都用到了实例化策略类里面的方法--instantiate(),毫无疑问反射对象的代码就应该在instantiate()里面,现在就让我们来揭开实例化策略类的神秘面纱。

package org.springframework.beans.factory.support;

public class SimpleInstantiationStrategy implements InstantiationStrategy {
	
	public Object instantiate(
			RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) {

		// Don't override the class with CGLIB if no overrides.
		if (beanDefinition.getMethodOverrides().isEmpty()) {
			// 调用BeanUtils的静态方法实例化bean
			return BeanUtils.instantiateClass(beanDefinition.getBeanClass());
		}
		else {
			// Must generate CGLIB subclass.
			// 抛异常"Method Injection not supported in SimpleInstantiationStrategy"
			return instantiateWithMethodInjection(beanDefinition, beanName, owner);
		}
	}

	public Object instantiate(
			RootBeanDefinition beanDefinition, String beanName, BeanFactory owner,
			Constructor ctor, Object[] args) {

		if (beanDefinition.getMethodOverrides().isEmpty()) {
			// 调用BeanUtils的静态方法实例化bean
			return BeanUtils.instantiateClass(ctor, args);
		}
		else {
			return instantiateWithMethodInjection(beanDefinition, beanName, owner, ctor, args);
		}
	}

	public Object instantiate(
			RootBeanDefinition beanDefinition, String beanName, BeanFactory owner,
			Object factoryBean, Method factoryMethod, Object[] args) {

		try {
			// It's a static method if the target is null.
			if (!Modifier.isPublic(factoryMethod.getModifiers()) ||
					!Modifier.isPublic(factoryMethod.getDeclaringClass().getModifiers())) {
				// 如果方法不是公有方法,则修改它的访问权限
				factoryMethod.setAccessible(true);
			}
			// 用反射调用factoryBean实例中的方法
			return factoryMethod.invoke(factoryBean, args);
		}
		catch (IllegalArgumentException ex) {
		...
		}
	}
}


原来一部分实例化bean的代码放到BeanUtils.instantiateClass()方法里了,我们现在来看看BeanUtils类

package org.springframework.beans;

public abstract class BeanUtils {
	/**
	 * 实例化带默认构造方法的bean
	 */
	public static Object instantiateClass(Class clazz) throws BeanInstantiationException {
		...
		try {
			return instantiateClass(clazz.getDeclaredConstructor((Class[]) null), null);
		}
		...
	}

	/**
	 * 根据构造方法实例化bean
	 */
	public static Object instantiateClass(Constructor ctor, Object[] args) throws BeanInstantiationException {
		...
		try {
			if (!Modifier.isPublic(ctor.getModifiers()) ||
					!Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) {
				//如果是私有构造方法则修改访问权限
				ctor.setAccessible(true);
			}
			// 反射实例
			return ctor.newInstance(args);
		}
		...
	}
}


从以上代码我们不难看出,spring如果遇到bean的构造方法是私有的,那么它会去修改bean的访问权限,同样可以反射出bean的对象

3. spring依赖注入(dependence injection)的实现

所谓依赖,举个例子说明,一个类Person,另一个类Car,如果Person的某个方法比如说drive,需要引用Car,则称Person类依赖于Car类,延伸到对象,这种依赖关系依然成立。这其中的依赖关系,就导致了对象Person需要负责对象Car的创建,甚至是整个生命周期的管理,而这样显然会带来耦合度高,不易维护等缺点。spring框架的依赖注入为我们提供了很好的解决方案,将Person依赖的对象Car造好,然后注入到Person中去,而无需Person自己去引用Car,这个注入的过程,由spring IOC容器来完成,无需对象去关心。

Spring提供了3种类型的依赖注入:构造函数注入(constructor injection)、setter注入(setter injection)和方法注入(method injection)。说白了就是反射factoryBean里的方法,把所依赖的对象做为参数传进去完成注入。这里提到了factoryBean的概念,这个factoryBean其实就是IOC容器管理的bean,说白了就是有依赖关系的bean,例如我们上面举的例子里面的Person类,Person需要依赖Car对象,在IOC容器中Person的实例就是一个factoryBean,Car的对象将被注入这个factoryBean

spring究竟是怎样实现依赖注入的,其实上面已经有所提到,我们现在再来仔细看看AbstractAutowireCapableBeanFactory类的instantiateUsingFactoryMethod方法

package org.springframework.beans.factory.support;

public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
		implements AutowireCapableBeanFactory {
	protected BeanWrapper instantiateUsingFactoryMethod(
			String beanName, RootBeanDefinition mergedBeanDefinition, Object[] explicitArgs) throws 

BeansException {

		ConstructorArgumentValues cargs = mergedBeanDefinition.getConstructorArgumentValues();
		ConstructorArgumentValues resolvedValues = null;

		int minNrOfArgs = 0;
		if (explicitArgs == null) {
			// 如果没有工厂方法的参数直接传入,我们将从bean definition中得到
			resolvedValues = new ConstructorArgumentValues();
			minNrOfArgs = resolveConstructorArguments(beanName, mergedBeanDefinition, cargs, resolvedValues);
		}
		else {
			minNrOfArgs = explicitArgs.length;
		}

		boolean isStatic = true;
		Class factoryClass = null;
		Object factoryBean = null;

		if (mergedBeanDefinition.getFactoryBeanName() != null) {
			// 获得factoryBean的实例
			factoryBean = getBean(mergedBeanDefinition.getFactoryBeanName());
			factoryClass = factoryBean.getClass();
			isStatic = false;
		}
		else {
			// It's a static factory method on the bean class.
			factoryClass = mergedBeanDefinition.getBeanClass();
		}

		BeanWrapperImpl bw = new BeanWrapperImpl();
		initBeanWrapper(bw);

		// 尝试调用factoryBean里的方法,如果这个工厂方法里的参数和得到的参数匹配
		Method[] candidates = factoryClass.getMethods();

		Method factoryMethodToUse = null;
		Object[] argsToUse = null;
		int minTypeDiffWeight = Integer.MAX_VALUE;
		// 对工厂方法和参数做个遍历,挑出合适的方法和参数
		for (int i = 0; i < candidates.length; i++) {
			Method factoryMethod = candidates[i];

			if (Modifier.isStatic(factoryMethod.getModifiers()) == isStatic &&
					factoryMethod.getName().equals(mergedBeanDefinition.getFactoryMethodName()) &&
					factoryMethod.getParameterTypes().length >= minNrOfArgs) {

				Class[] argTypes = factoryMethod.getParameterTypes();
				ArgumentsHolder args = null;

				if (resolvedValues != null) {
					// Resolved contructor arguments: type conversion and/or autowiring necessary.
					try {
						args = createArgumentArray(
								beanName, mergedBeanDefinition, resolvedValues, bw, 

argTypes, "factory method");
					}
					catch (UnsatisfiedDependencyException ex) {
						if (logger.isDebugEnabled()) {
							logger.debug("Ignoring factory method [" + factoryMethod + "] of 

bean '" + beanName +
									"': " + ex.getMessage());
						}
						if (i == candidates.length - 1 && factoryMethodToUse == null) {
							throw ex;
						}
						else {
							// Swallow and try next overloaded factory method.
							continue;
						}
					}
				}

				else {
					// Explicit arguments given -> arguments length must match exactly.
					if (argTypes.length != explicitArgs.length) {
						continue;
					}
					args = new ArgumentsHolder(explicitArgs);
				}

				int typeDiffWeight = args.getTypeDifferenceWeight(argTypes);
				// Choose this constructor if it represents the closest match.
				if (typeDiffWeight < minTypeDiffWeight) {
					factoryMethodToUse = factoryMethod;
					argsToUse = args.arguments;
					minTypeDiffWeight = typeDiffWeight;
				}
			}
		}

		if (factoryMethodToUse == null) {
			throw new BeanCreationException(
					mergedBeanDefinition.getResourceDescription(), beanName,
					"Cannot find matching factory method '" + mergedBeanDefinition.getFactoryMethodName

() +
					"' on class [" + factoryClass.getName() + "]");
		}

		// 反射factoryBean的factory method,把依赖的bean对象做为参数传入,完成注入.
		Object beanInstance = this.instantiationStrategy.instantiate(
				mergedBeanDefinition, beanName, this, factoryBean, factoryMethodToUse, argsToUse);
		...
		bw.setWrappedInstance(beanInstance);
		...
		return bw;
	}
}


现在总结下:

spring的IOC容器的功能就是把实例化对象和代码解耦,现在造对象的活完全交给spring IOC容器来做了,如果有需要,spring容器还可以把bean注入到依赖它的其它bean中,方便程序使用。我们要做的仅仅是配置好bean定义文件,并告诉spring定义文件在哪,然后在代码里getBean()就行啦!

IOC容器的载体说白了就是Map,即我们常说的缓存。在spring中IOC缓存主要分bean实例的缓存(单例)和bean定义信息的缓存

spring通过定位资源-->解析bean定义文件并缓存bean定义信息-->根据bean定义造bean实例-->如果是单例模式则把这个bean放进缓存管理,否则直接造对象-->如果有依赖关系,则反射目标bean的方法(setter方法/构造方法/普通方法)并将其依赖的bean做为参数传入,完成注入-->至此,IOC容器就建立起来了。
   发表时间:2008-09-19  
讲的不错,有时候看看源代码确实能学习到不少的东西
0 请登录后投票
   发表时间:2008-09-19  
非常感谢,讲的非常详细。对spring的IOC又有了新的认识。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics