• Spring源码分析refresh()第一篇


    prepareRefresh方法

    从命名来看,就知道这个方法主要做了一些刷新前的准备工作,和主流程关系不大,主要是保存了容器的启动时间,启动标志等
    
    • 1

    obtainFreshBeanFactory 方法

    获取告诉子类初始化Bean工厂 就是把beanFactory取出来而已。DefaultListableBeanFactory XML模式下会在这里读取BeanDefinition
    
    • 1
    AbstractRefreshableApplicationContext类	
    	protected final void refreshBeanFactory() throws BeansException {
    		if (hasBeanFactory()) {
    			destroyBeans();
    			closeBeanFactory();
    		}
    		try {
    			DefaultListableBeanFactory beanFactory = createBeanFactory();
    			beanFactory.setSerializationId(getId());
    			customizeBeanFactory(beanFactory);
    			loadBeanDefinitions(beanFactory);
    			this.beanFactory = beanFactory;
    		}
    		catch (IOException ex) {
    			throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    prepareBeanFactory 方法

    还是一些准备工作,添加了两个后置处理器:ApplicationContextAwareProcessor,ApplicationListenerDetector
    还设置了 忽略自动装配 和 允许自动装配 的接口,如果不存在某个bean的时候,spring就自动注册singleton bean ,还设置了bean表达式解析器 等
    
    • 1
    • 2

    方法说明

    	protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    		// Tell the internal bean factory to use the context's class loader etc.
    		//APPCLassLoader
    		beanFactory.setBeanClassLoader(getClassLoader());
    		//bean解析处理器 StandardBeanExpressionResolver
    		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
    
    		// Configure the bean factory with context callbacks.
    		//添加一个ApplicationContextAwareProcessor 扩展了BeanPostProcessor
    		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
    		//忽略给定的自动装配依赖接口。
    		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    		beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
    		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
    		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
    		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
    		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
    
    		// BeanFactory接口没有在普通工厂中注册为可解析类型。
    		// MessageSource被注册为一个bean(用于自动装配)。
    		//以下接口,允许自动装配,第一个参数是自动装配的类型,,第二个字段是自动装配的值
    
    		beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
    		beanFactory.registerResolvableDependency(ResourceLoader.class, this);
    		beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
    		beanFactory.registerResolvableDependency(ApplicationContext.class, this);
    
    		
        	//添加一个后置处理器:ApplicationListenerDetector,此后置处理器实现了BeanPostProcessor接口  用于检测内部bean的早期后处理器注册为applicationlistener
    		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
    
          //如果没有注册过bean名称为XXX,spring就自己创建一个名称为XXX的singleton bean
    		// Detect a LoadTimeWeaver and prepare for weaving, if found.
    		if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
    			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
    			// Set a temporary ClassLoader for type matching.
    			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    		}
    
    		// Register default environment beans.
    		if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { 
    			//environment
    			beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
    		}
    		if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
    			//systemProperties
    			beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
    		}
    		if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { 
    			//systemEnvironment
    			beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    主要做了如下的操作:

    1. 设置了一个类加载器
    2. 设置了bean表达式解析器
    3. 添加了属性编辑器的支持
    4. 添加了一个后置处理器:ApplicationContextAwareProcessor,此后置处理器实现了BeanPostProcessor接口
    5. 设置了一些忽略自动装配的接口
    6. 设置了一些允许自动装配的接口,并且进行了赋值操作
    7. 在容器中还没有XX的bean的时候,帮我们注册beanName为XX的singleton bean

    postProcessBeanFactory 方法

    //允许在上下文子类中对bean工厂进行后处理。
    postProcessBeanFactory(beanFactory);
    
    
    • 1
    • 2
    • 3
  • 相关阅读:
    MySQL数据库增删改查
    java后端返回数据给前端时去除值为空或NULL的属性、忽略某些属性
    原生JS中的Ajax
    基于51单片机直流电机PWM控制器设计
    Automation Anywhere推出新的生成式AI自动化平台,加速提高企业生产力
    MySQL事务:特性、使用、并发事务问题和隔离级别
    【探索C++】C++对C语言的扩展
    docker登录不上
    【Python工程师之高性能爬虫】
    开源项目管理工具Helper的安装及汉化
  • 原文地址:https://blog.csdn.net/huanglu0314/article/details/125619700