天天看點

Spring容器建立流程(2)建立beanFactory,加載BeanDefinition

Spring 容器的建立。obtainFreshBeanFactorr()中完成容器的建立。(BeanFactory關系類圖,之前的執行流程可在本系列部落格中看到)。接下來看容器建立的第二部,建立beanFactory

容器refresh總覽:

synchronized (this.startupShutdownMonitor) {
			// 設定環境,校驗參數。
			prepareRefresh();
			
			// 建立BeanFactory(DefaultListableBeanFactor),加載bean定義資訊。
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

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

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				// 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 all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
           

obtainFreshBeanFactory 容器建立

容器初始化refresh方法中 obtainFreshBeanFactory用來建立容器執行個體

// AbstractApplicationContext中,重要的方法是refreshBeanFactory()方法
// getBeanFactory()不過是直接擷取建立好的工廠
		protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
		refreshBeanFactory();
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		if (logger.isDebugEnabled()) {
			logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
		}
		return beanFactory;
	}
           

refreshBeanFactory()

refreshBeanFactory 方法在 AbstractApplicationContext的子類AbstractRefreshableApplicationContext中實作。

BeanDefinition: 是包含着類的定義資訊,類的名字,作用域,是否懶加載等等描述bean的資訊(也就是xml中配置的bean資訊的封裝)

@Override
	protected final void refreshBeanFactory() throws BeansException {
		// 如果工廠已經建立,銷毀工廠
		if (hasBeanFactory()) {
			destroyBeans();
			closeBeanFactory();
		}
		try {
			// 直接建立一個DefaultListableBeanFactory工廠
			DefaultListableBeanFactory beanFactory = createBeanFactory();
			beanFactory.setSerializationId(getId());
			// 交給子類自定義配置
			customizeBeanFactory(beanFactory);
			// 加載beanDefinition
			loadBeanDefinitions(beanFactory);
			synchronized (this.beanFactoryMonitor) {
				this.beanFactory = beanFactory;
			}
		}
		catch (IOException ex) {
			throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
		}
	}
           

AbstractRefreshableApplicationContext 中對 customizeBeanFactory的實作

protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
	// 是否允許bean定義覆寫(預設不允許)
		if (this.allowBeanDefinitionOverriding != null) {
			beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
		}
	// 是否允許循環依賴(預設不允許)
		if (this.allowCircularReferences != null) {
			beanFactory.setAllowCircularReferences(this.allowCircularReferences);
		}
	}
           

loadBeanDefinitions(beanFactory);

建立工廠中最重要的一步,加載bean的定義資訊。

首先明确幾個概念

  1. XXXResource:代表着資源檔案
  2. XXXBeanDefinitionReader: 用于讀取Resource,
  3. BeanDefinition: bean的描述資訊,也就是xml中bean配置的描述的封裝
@Override
	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
		// beanFactory本身是一個BeanDefinitionRegistry,用于注冊bean資訊
		// 建立 reader 讀取 resource
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

		// 設定資源加載環境
		beanDefinitionReader.setEnvironment(this.getEnvironment());
		// 設定資源加載器,beanFactory也實作了ResourceLoader,用于将制定位置的資源,加載為Resource
		beanDefinitionReader.setResourceLoader(this);
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

		// 給子類一個再次設定的機會
		initBeanDefinitionReader(beanDefinitionReader);
		// 加載bean定義資訊
		loadBeanDefinitions(beanDefinitionReader);
	}
	
	protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
		Resource[] configResources = getConfigResources();
		if (configResources != null) {
			reader.loadBeanDefinitions(configResources);
		}
		// 擷取資源檔案路徑
		String[] configLocations = getConfigLocations();
		if (configLocations != null) {
		// 使用XmlBeanDefinitionReader加載資源
			reader.loadBeanDefinitions(configLocations);
		}
	}
           
XmlBeanDefinitionReader 加載 Resource
@Override
	public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
		Assert.notNull(locations, "Location array must not be null");
		int counter = 0;
		for (String location : locations) {
			counter += loadBeanDefinitions(location);
		}
		return counter;
	}
	@Override
	public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
		return loadBeanDefinitions(location, null);
	}

    // 最終調用這個方法
	public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
	// 擷取資源加載器,也就是前面注冊的beanFactory(它就是一個資源加載器這裡用的是ClassPathXmlApplicationContext)
		ResourceLoader resourceLoader = getResourceLoader();
		if (resourceLoader == null) {
			throw new BeanDefinitionStoreException(
					"Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
		}

		if (resourceLoader instanceof ResourcePatternResolver) {
			// Resource pattern matching available.
			try {
			// 加載資源
				Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
			// 加載bean定義資訊
				int loadCount = loadBeanDefinitions(resources);
				if (actualResources != null) {
					for (Resource resource : resources) {
						actualResources.add(resource);
					}
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
				}
				return loadCount;
			}
			catch (IOException ex) {
				throw new BeanDefinitionStoreException(
						"Could not resolve bean definition resource pattern [" + location + "]", ex);
			}
		}
		else {
			// Can only load single resources by absolute URL.
			Resource resource = resourceLoader.getResource(location);
			int loadCount = loadBeanDefinitions(resource);
			if (actualResources != null) {
				actualResources.add(resource);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
			}
			return loadCount;
		}
	}
           

Resource[] resources ((ResourcePatternResolver)resourceLoader).getResources(location);

// this.resourcePatternResolver 在構造器中已經初始化完成。
	@Override
	public Resource[] getResources(String locationPattern) throws IOException {
		return this.resourcePatternResolver.getResources(locationPattern);
	}
           

PathMarchingResourcePatternResolver擷取資源

@Override
	public Resource[] getResources(String locationPattern) throws IOException {
		Assert.notNull(locationPattern, "Location pattern must not be null");
		// 如果以 classpath*:開頭
		if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
			// a class path resource (multiple resources for same name possible)
			// isPattern判斷資源路徑中是否包含* 或者 ?
			if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
				// a class path resource pattern
				return findPathMatchingResources(locationPattern);
			}
			else {
				// all class path resources with the given name
				return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
			}
		}
		else {
			// Generally only look for a pattern after a prefix here,
			// and on Tomcat only after the "*/" separator for its "war:" protocol.
			int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 :
					locationPattern.indexOf(':') + 1);
			if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
				// a file pattern
				return findPathMatchingResources(locationPattern);
			}
			else {
				// a single resource with the given name
				// 提供的資源路徑是 classpath:application.xml 最終調用這裡。
				// 路徑封裝為一個Resource
				return new Resource[] {getResourceLoader().getResource(locationPattern)};
			}
		}
	}
           

XmlBeanDefinitionReader 加載 Resource 已經完成,接下來就是使用Resource加載BeanDefinition。

XmlBeanDefinitionReader 加載BeanDefinition

AbstractBeanDefinitionReader.loadBeanDefinitions(String location, @Nullable Set actualResources) 方法中,getConfigResources()已經擷取資源完畢。

// 加載資源
Resource[] configResources = getConfigResources();
// 加載bean定義資訊
int loadCount = loadBeanDefinitions(resources);
           
@Override
	public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
		Assert.notNull(resources, "Resource array must not be null");
		int counter = 0;
		for (Resource resource : resources) {
			counter += loadBeanDefinitions(resource);
		}
		return counter;
	}
	@Override
	public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
		// 統一編碼封裝
		return loadBeanDefinitions(new EncodedResource(resource));
	}

	public int loadBeanDefinitions(EncodedResource encodedResource) throws 	BeanDefinitionStoreException {
		Assert.notNull(encodedResource, "EncodedResource must not be null");
		if (logger.isInfoEnabled()) {
			logger.info("Loading XML bean definitions from " + encodedResource.getResource());
		}
// resourceCurrentlyBeingLoaded是一個ThreadLocal,存放線程開始加載的資源
		Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
		if (currentResources == null) {
			currentResources = new HashSet<>(4);
			this.resourcesCurrentlyBeingLoaded.set(currentResources);
		}
		if (!currentResources.add(encodedResource)) {
			throw new BeanDefinitionStoreException(
					"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
		}
		try {
		// 擷取resource流
			InputStream inputStream = encodedResource.getResource().getInputStream();
			try {
			// 包裝為InputSource
				InputSource inputSource = new InputSource(inputStream);
				if (encodedResource.getEncoding() != null) {
					inputSource.setEncoding(encodedResource.getEncoding());
				}
			// 加載bean定義資訊
				return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
			}
			finally {
				inputStream.close();
			}
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException(
					"IOException parsing XML document from " + encodedResource.getResource(), ex);
		}
		finally {
			currentResources.remove(encodedResource);
			if (currentResources.isEmpty()) {
				this.resourcesCurrentlyBeingLoaded.remove();
			}
		}
	}
           
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
			throws BeanDefinitionStoreException {
		try {
			// 使用dom解析檔案
			Document doc = doLoadDocument(inputSource, resource);
			// 解析dom包裝為beanDefinition,具體的解析過程比較複雜。
			// beanDefinitionRegistry就是上面的beanFactory,解析dom封裝為beanDefinition後,儲存到beanFactory中(就是調用beanDefinitionRegistry的registerBeanDefinition()方法)
			return registerBeanDefinitions(doc, resource);
		}
		....
	}
           

自此,beanFactory建立完成并且,beanDefinition也加載完成。