spring IOC:DefaultListableBeanFactory

DefaultSingletonBeanRegistry完成对单例bean的控制
AbstractBeanFactory完成对不同getBean请求的响应,查询Bean定义及销毁方法的注册
AbstractAutowireCapableBeanFactory完成Bean的创建,属性的注入,切面代理的产生,bean的生命周期的管理
最后由DefaultListableBeanFactory提供registerBeanDefinition方法完成bean定义的注册
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)throws BeanDefinitionStoreException {

		if (beanDefinition instanceof AbstractBeanDefinition) {
			try {
				((AbstractBeanDefinition) beanDefinition).validate();
			}
			catch (BeanDefinitionValidationException ex) {
//.....
			}
		}

		Object oldBeanDefinition = this.beanDefinitionMap.get(beanName);
		if (oldBeanDefinition != null) {
			if (!this.allowBeanDefinitionOverriding) {
				//.....

		}
		else {
			this.beanDefinitionNames.add(beanName);
		}
		this.beanDefinitionMap.put(beanName, beanDefinition);
		removeSingleton(beanName);
	}

Spring采用这样层层继承的结构,逐步完成IOC容器的各个功能。

XmlBeanFactory继承自DefaultListableBeanFactory,由它和XmlBeanDefinitionReader完成资源的读取工作。

XmlBeanFactory:
public XmlBeanFactory(Resource resource) throws BeansException {
		this(resource, null);
	}
	public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
		super(parentBeanFactory);
		this.reader.loadBeanDefinitions(resource);
	}

这里reader.loadBeanDefinitions(resource);
接下来
//调用doLoadBeanDefinitions方法
loadBeanDefinitions(EncodedResource encodedResource)
//验证读取xml配置文件,嗲用registerBeanDefinitions方法
doLoadBeanDefinitions(inputSource, encodedResource.getResource());
//调用documentReader.registerBeanDefinitions方法,由默认的documentReader对象解析xml
registerBeanDefinitions

最后在registerBeanDefinitions方法中,有具体的BeanDefinitionParserDelegate来完成bean的注册工作
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
		this.readerContext = readerContext;

		logger.debug("Loading bean definitions");
		Element root = doc.getDocumentElement();

		BeanDefinitionParserDelegate delegate = createHelper(readerContext, root);

		preProcessXml(root);
		parseBeanDefinitions(root, delegate);
		postProcessXml(root);
	}

在parseBeanDefinitions(root, delegate);我们找到了->
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
这是最后注册bean定义的方法,在这个方法中会调用DefaultListableBeanFactory提供的
registerBeanDefinition(beanName, bdHolder.getBeanDefinition());这个方法。

还要提到的就是这个ApplicationContext请看其类图
spring IOC:DefaultListableBeanFactory_第1张图片
AbstractApplicationContext中的refresh()方法做为一个模板方法详细描述了整个容器的启动过程。同时在实现了BeanFactory ,ListableBeanFactory ,HierarchicalBeanFactory它作为一个IOC容器的门面,来负责IOC容器的具体运行。

注意在这里AbstractRefreshableApplicationContext直接持有了对DefaultListableBeanFactory的引用

回看一下它的IOC容器的图
spring IOC:DefaultListableBeanFactory_第2张图片

一些对IOC容器直接操作的方法
	//---------------------------------------------------------------------
	// Implementation of BeanFactory interface
	//---------------------------------------------------------------------
public Object getBean(String name) throws BeansException {
		return getBeanFactory().getBean(name);
	}

	public Object getBean(String name, Class requiredType) throws BeansException {
		return getBeanFactory().getBean(name, requiredType);
	}

	public boolean containsBean(String name) {
		return getBeanFactory().containsBean(name);
	}

	public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
		return getBeanFactory().isSingleton(name);
	}

	public Class getType(String name) throws NoSuchBeanDefinitionException {
		return getBeanFactory().getType(name);
	}

	public String[] getAliases(String name) {
		return getBeanFactory().getAliases(name);
	}
	//---------------------------------------------------------------------
	// Implementation of ListableBeanFactory interface
	//---------------------------------------------------------------------

	public boolean containsBeanDefinition(String name) {
		return getBeanFactory().containsBeanDefinition(name);
	}

	public int getBeanDefinitionCount() {
		return getBeanFactory().getBeanDefinitionCount();
	}

	public String[] getBeanDefinitionNames() {
		return getBeanFactory().getBeanDefinitionNames();
	}

	public String[] getBeanNamesForType(Class type) {
		return getBeanFactory().getBeanNamesForType(type);
	}

	public String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean allowEagerInit) {
		return getBeanFactory().getBeanNamesForType(type, includePrototypes, allowEagerInit);
	}

	public Map getBeansOfType(Class type) throws BeansException {
		return getBeanFactory().getBeansOfType(type);
	}

	public Map getBeansOfType(Class type, boolean includePrototypes, boolean allowEagerInit)
			throws BeansException {

		return getBeanFactory().getBeansOfType(type, includePrototypes, allowEagerInit);
	}
	//---------------------------------------------------------------------
	// Implementation of HierarchicalBeanFactory interface
	//---------------------------------------------------------------------

	public BeanFactory getParentBeanFactory() {
		return getParent();
	}

	public boolean containsLocalBean(String name) {
		return getBeanFactory().containsLocalBean(name);
	}

这个 refresh()描述了整个容器的启动过程,提供了很多抽象方法,由子类来实现,层层向下
public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			this.startupTime = System.currentTimeMillis();

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

			//由子类AbstractRefreshableApplicationContext实现,创建IOC容器,并调用loadBeanDefinitions方法(该方法继续由具体子类来完成)
			refreshBeanFactory();
			ConfigurableListableBeanFactory beanFactory = getBeanFactory();

			//设置类加载器
			beanFactory.setBeanClassLoader(getClassLoader());


			beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));

			beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
			beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
			beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
			beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
			beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

			// Allows post-processing of the bean factory in context subclasses.
			postProcessBeanFactory(beanFactory);

			//启动工厂前处理器
			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处理器
				registerBeanPostProcessors();

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

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

				// Last step: publish corresponding event.
				publishEvent(new ContextRefreshedEvent(this));
			}

			catch (BeansException ex) {
				beanFactory.destroySingletons();
				throw ex;
			}
		}
	}

AbstractRefreshableApplicationContext提供了创建IOC容器的方法
protected DefaultListableBeanFactory createBeanFactory() {
		return new DefaultListableBeanFactory(getInternalParentBeanFactory());
	}

其它的诸如AbstractXmlApplicationContext类则提供了读取资源文件等方法,逐层完成整个功能

你可能感兴趣的:(spring,bean,xml,配置管理,IOC)