zoukankan      html  css  js  c++  java
  • spring ioc实现原理

    spring ioc实现原理

    1、一个普通的调用

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    GsonFormatBean bean = (GsonFormatBean) context.getBean("GsonFormatBean");
    bean.isPalindrome(100);

    1.1 分析第一步

     创建一个classPathXmlApplication的context,主要的实现在abstractAppplicationContext的refresh()这个方法中

    	@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.
    				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();
    			}
    		}
    	}

    使用对象锁保证只有一个线程访问逻辑

     第一步:

    设置容器的一些全局性参数,比如closed,active等参数

    第二步:

    告诉父类创建一个bean工厂

     刷新整个bean工厂,获取一个ConfigurableListableBeanFacotory,返回bean工厂。

    刷新beanFactory

     创建一个DefaultListableBeanFactory,设置一些属性,加载beanDefinition,具体的实现在abstractxmlApplicationContext、AnnotationWebConfigApplicationContext等

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    容器怎么实现加载所有的bean

     两个关键的方法:

     容器注册所有的beanDefinitions

    解析单个bean的操作 

     最终生成beanDefination,也就是将xml映射成为一个bean

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • 相关阅读:
    rn 安卓开发报错
    rn 打包如果真机或者模拟机打包编译报错
    fluter 导航以及其他
    fluter 相关布局组件
    fluter 万事皆组件。常用组件
    rn开发问题简要
    app配置环境和打包方式以及其他问题
    小程序大概的开发流程
    ant-design-pro 开发 基于react 框架涉及到技术你的本地环境需要安装 yarn、node 和 git。我们的技术栈基于 ES2015+、React、UmiJS、dva、g2 和 antd,
    unity打包的webgl结合react使用
  • 原文地址:https://www.cnblogs.com/zhangchiblog/p/11431476.html
Copyright © 2011-2022 走看看