Spring源码解析——start from BeanFactory(一)



         先来看一下我们学习Spring时候的ABC代码:


	BeanFactory beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserManager userManager=(UserManager)beanFactory.getBean("UserManagerImpl");
		userManager.add("dd","d");

             通过加在含有bean配置关系的映射文件,get到我们的beanFactory容器,之后就可以从容器中get bean了。接着,我们将跟着这几行最简单代码,来深入search下spring是如何实现IOC 容器的。



 一,Spring容器的核心规范


            先上源码:


public interface BeanFactory {

	/**
	 * Used to dereference a FactoryBean and distinguish it from beans
	 * <i>created</i> by the FactoryBean. For example, if the bean named
	 * <code>myEjb</code> is a FactoryBean, getting <code>&myEjb</code> will
	 * return the factory, not the instance returned by the factory.
	 */
	String FACTORY_BEAN_PREFIX = "&";


	/**
	 * Return an instance, which may be shared or independent, of the given bean name.
	 * This method allows a Spring BeanFactory to be used as a replacement for the
	 * Singleton or Prototype design pattern.
	 * <p>Callers may retain references to returned objects in the case of Singleton beans.
	 * <p>Translates aliases back to the corresponding canonical bean name.
	 * Will ask the parent factory if the bean cannot be found in this factory instance.
	 * @param name the name of the bean to return
	 * @return the instance of the bean
	 * @throws NoSuchBeanDefinitionException if there is no bean definition
	 * with the specified name
	 * @throws BeansException if the bean could not be obtained
	 */
	Object getBean(String name) throws BeansException;

	/**
	 * Return an instance (possibly shared or independent) of the given bean name.
	 * <p>Behaves the same as getBean(String), but provides a measure of type safety by
	 * throwing a Spring BeansException if the bean is not of the required type.
	 * This means that ClassCastException can't be thrown on casting the result correctly,
	 * as can happen with <code>getBean(String)</code>.
	 * @param name the name of the bean to return
	 * @param requiredType type the bean must match. Can be an interface or superclass
	 * of the actual class, or <code>null</code> for any match. For example, if the value
	 * is <code>Object.class</code>, this method will succeed whatever the class of the
	 * returned instance.
	 * @return an instance of the bean (never <code>null</code>)
	 * @throws BeanNotOfRequiredTypeException if the bean is not of the required type
	 * @throws NoSuchBeanDefinitionException if there's no such bean definition
	 * @throws BeansException if the bean could not be created
	 */
	Object getBean(String name, Class requiredType) throws BeansException;

	/**
	 * Does this bean factory contain a bean definition with the given name?
	 * <p>Will ask the parent factory if the bean cannot be found in this factory instance.
	 * @param name the name of the bean to query
	 * @return whether a bean with the given name is defined
	 */
	boolean containsBean(String name);

	/**
	 * Is this bean a singleton? That is, will <code>getBean</code> always return the same object?
	 * <p>Will ask the parent factory if the bean cannot be found in this factory instance.
	 * @param name the name of the bean to query
	 * @return is this bean a singleton
	 * @throws NoSuchBeanDefinitionException if there is no bean with the given name
	 * @see #getBean
	 */
	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

	/**
	 * Determine the type of the bean with the given name.
	 * More specifically, checks the type of object that <code>getBean</code> would return.
	 * For a FactoryBean, returns the type of object that the FactoryBean creates.
	 * @param name the name of the bean to query
	 * @return the type of the bean, or <code>null</code> if not determinable
	 * @throws NoSuchBeanDefinitionException if there is no bean with the given name
	 * @since 1.1.2
	 * @see #getBean
	 * @see FactoryBean#getObjectType()
	 */
	Class getType(String name) throws NoSuchBeanDefinitionException;

	/**
	 * Return the aliases for the given bean name, if defined.
	 * <p>If the given name is an alias, the corresponding original bean name
	 * and other aliases (if any) will be returned, with the original bean name
	 * being the first element in the array.
	 * <p>Will ask the parent factory if the bean cannot be found in this factory instance.
	 * @param name the bean name to check for aliases
	 * @return the aliases, or an empty array if none
	 */
	String[] getAliases(String name);

}


           上面一段是从spring源码里面拽出来的,在BeanFactory里面,定义了Spring容器最基本的规范,下面我们来依次讲解下各个方法:

   

         1,String FACTORY_BEAN_PREFIX = "&";


                       对FactoryBean的转义定义,因为如果使用bean的名字检索FactoryBean得到的对象是工厂生成的对象,如果需要得到工厂本身,需要转义;例如:myEjb是一个FactoryBean,&myEjb将会得到一个工厂,而不是工厂返回的实例。

           关于BeanFacoty与FactoryBean: BeanFactory是个Factory,也就是IOC容器或对象工厂,FactoryBean是个Bean。在Spring中,所有的Bean都是由BeanFactory(也就是IOC容器)来进行管理的。但对FactoryBean而言,这个Bean不是简单的Bean,而是一个能生产或者修饰对象生成的工厂Bean,它的实现与设计模式中的工厂模式和修饰器模式类似。(from :http://chenzehe.iteye.com/blog/1481476)


          可以看出FactoryBean主要是为了简化复杂bean的装配而产生的。


          2,Object getBean(String name) throws BeansException;


                         根据bean的名字,获取在IOC容器中得到bean实例


        3,Object getBean(String name, Class requiredType) throws BeansException;



                      根据了bean的名字和类型来获取bean实例,比上个方法多了类型条件验证,增强了类型安全验证机制。



        4,boolean containsBean(String name);



                         根据名称查找是否容器中含有这个bean


        5,boolean isSingleton(String name) throws NoSuchBeanDefinitionException;


                      判断bean是否为单例的。


        6,Class getType(String name) throws NoSuchBeanDefinitionException;



                       得到bean类型的类类型,可能会抛出类不存在的异常。

       


         7,String[] getAliases(String name);



              得到bean的别名,如果根据别名检索,那么其原名也会被检索出来。



二,从ClassPathXmlApplicationContext到BeanFactory



            BeanFactory只是定义了容器的规范,spring为容器的实现提供了很多实现类,先来看看我们代码里面,从ClassPathXmlApplicationContext到BeanFactory经历了多少级:



         Spring源码解析——start from BeanFactory(一)

         ClassPathXmlApplication虽然提供了好几个构造函数,但是,最终还是会调用:


               

	public ClassPathXmlApplicationContext(String[] paths, Class clazz, ApplicationContext parent)
			throws BeansException {

		super(parent);
		Assert.notNull(paths, "Path array must not be null");
		Assert.notNull(clazz, "Class argument must not be null");
		this.configResources = new Resource[paths.length];
		for (int i = 0; i < paths.length; i++) {
			this.configResources[i] = new ClassPathResource(paths[i], clazz);
		}
		refresh();
	}



            构造函数里面,主要就是调用父类的构造函数,然后我们沿着继承链找:


        Spring源码解析——start from BeanFactory(一)

        

       之后的代码估计是加在配置资源,接着是refresh方法,这个方法的真正实现在AbstractApplicationContext这个抽象类中:

          

/*创建 BeanFactory 工厂的过程
		refresh是一个刷新配置的过程,如果Context有可更新的子类,
		当BeanFactory已存在就可更新,如果没有就新创建
*/
public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			this.startupTime = System.currentTimeMillis();

			synchronized (this.activeMonitor) {
				this.active = true;
			}

			//准备刷新上下文环境
			refreshBeanFactory();

			//初始化beanFactory,并进行配置文件的读取
			ConfigurableListableBeanFactory beanFactory = getBeanFactory();

			// Tell the internal bean factory to use the context's class loader.
			beanFactory.setBeanClassLoader(getClassLoader());

			// Populate the bean factory with context-specific resource editors.
			beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));

			// Configure the bean factory with context semantics.
			beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
			beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
			beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
			beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
			beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

			// 子类覆盖方法做额外处理
			postProcessBeanFactory(beanFactory);

			// Invoke factory processors registered with the context instance.
			for (Iterator it = getBeanFactoryPostProcessors().iterator(); it.hasNext();) {
				BeanFactoryPostProcessor factoryProcessor = (BeanFactoryPostProcessor) it.next();
				factoryProcessor.postProcessBeanFactory(beanFactory);
			}

			if (logger.isInfoEnabled()) {
				if (getBeanDefinitionCount() == 0) {
					logger.info("No beans defined in application context [" + getDisplayName() + "]");
				}
				else {
					logger.info(getBeanDefinitionCount() + " beans defined in application context [" + getDisplayName() + "]");
				}
			}

			try {
				// **各种BeanFactory处理器
				invokeBeanFactoryPostProcessors();

				// 注册拦截Bean创建的Bean处理器,这里只是注册,真正调用是在getBean的时候
				registerBeanPostProcessors();

				// 为上下文初始化Message源,即不同语言的消息体,国际化处理
				initMessageSource();

				// 初始化应用消息广播器,并放入“applicationEventMulticaster”bean中
				/*用户可以在配置文件中为容器定义一个自定义的事件广播器,只要实现ApplicationEventMulticaster就可以了,Spring会通过 反射的机制将其注册成容器的事件广播器,如果没有找到配置的外部事件广播器,Spring自动使用 SimpleApplicationEventMulticaster作为事件广播器。*/
				initApplicationEventMulticaster();

				// 留给子类来初始化其他bean
				onRefresh();

				// 在所有注册的bean中查找Listener bean,注册到消息广播器中
				/* Spring根据反射机制,使用ListableBeanFactory的getBeansOfType方法,从BeanDefinitionRegistry中找出所有实现 org.springframework.context.ApplicationListener的Bean,将它们注册为容器的事件监听器,实际的操作就是将其添加到事件广播器所提供的监听器注册表中。*/
				registerListeners();

				// Instantiate singletons this late to allow them to access the message source.
				beanFactory.preInstantiateSingletons();

				// 最后一步:发布事件
				/*在AbstractApplicationContext的publishEvent方法中, Spring委托ApplicationEventMulticaster将事件通知给所有的事件监听器*/
				publishEvent(new ContextRefreshedEvent(this));
			}

			catch (BeansException ex) {
				// Destroy already created singletons to avoid dangling resources.
				beanFactory.destroySingletons();
				throw ex;
			}
		}
	}


           刚开始扒源码,感觉挺杂乱的,画画图什么的还是挺有帮助的,昨天就是在一直跟断点,看看要实现我的getBean都走了哪些代码,今天会从整体结构上先看看。