zoukankan      html  css  js  c++  java
  • ConfigurationClassPostProcessor解析

    ConfigurationClassPostProcessor解析

    主要是为了解析Configuration类

    类图

    何时使用

    继承了BeanFactoryPostProcessor,会在容器加载的时候使用。

    @Override
    	public void refresh() throws BeansException, IllegalStateException {
    		synchronized (this.startupShutdownMonitor) {
    			// Prepare this context for refreshing.
    			prepareRefresh();
    
    			// Tell the subclass to refresh the internal bean factory.
    			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.
    				// 执行BeanFactoryPostProcessor
    				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();
    			}
    		}
    	}
    

    加载Configuration流程

    入口方法:org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry

    	@Override
    	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
    		int registryId = System.identityHashCode(registry);
    		if (this.registriesPostProcessed.contains(registryId)) {
    			throw new IllegalStateException(
    					"postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);
    		}
    		if (this.factoriesPostProcessed.contains(registryId)) {
    			throw new IllegalStateException(
    					"postProcessBeanFactory already called on this post-processor against " + registry);
    		}
    		this.registriesPostProcessed.add(registryId);
    
    		processConfigBeanDefinitions(registry);
    	}
    
    1. org.springframework.context.annotation.ConfigurationClassParser#parse(java.util.Set<org.springframework.beans.factory.config.BeanDefinitionHolder>)
    2. this.deferredImportSelectorHandler.process();
    3. org.springframework.context.annotation.ConfigurationClassParser.DeferredImportSelectorGroupingHandler#processGroupImports
    4. org.springframework.context.annotation.ConfigurationClassParser.DeferredImportSelectorGrouping#getImports
    5. org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.AutoConfigurationGroup#process
    6. org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry
    7. org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getCandidateConfigurations
    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    		List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
    				getBeanClassLoader());
    	// 加载了所有的AutoConfiguration类
    		Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
    				+ "are using a custom packaging, make sure that file is correct.");
    		return configurations;
    	}
    

    参考

  • 相关阅读:
    在jenkins和sonar中集成jacoco(一)--使用jacoco收集单元测试的覆盖率
    持续集成一天一美元
    在jenkins和sonar中集成jacoco(二)--在jenkins中生成jacoco覆盖率报告
    day02--课后练习
    python之路-day02
    day01--课后练习
    python之路-day01
    python-FTP程序
    python-选课系统
    python- ATM与购物商城
  • 原文地址:https://www.cnblogs.com/seekwind/p/12600297.html
Copyright © 2011-2022 走看看