Spring中重要的核心类和设计理念

  Spring中又两个看着相似的接口,BeanFactory,FactoryBean,它们到底有什么作用和区别呢?

  Spring中管理的Bean是由什么类解析呢?答案就是BeanFactory。

  打开AbstractApplicationContext类,看refresh方法:

 public void refresh() throws BeansException, IllegalStateException {
        synchronized(this.startupShutdownMonitor) {
            this.prepareRefresh();
            ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
            this.prepareBeanFactory(beanFactory);

            try {
                this.postProcessBeanFactory(beanFactory);
                this.invokeBeanFactoryPostProcessors(beanFactory);
                this.registerBeanPostProcessors(beanFactory);
                this.initMessageSource();
                this.initApplicationEventMulticaster();
                this.onRefresh();
                this.registerListeners();
          // 创建BeanFactory,就相当于创建一个汽车制造厂。 this.finishBeanFactoryInitialization(beanFactory); this.finishRefresh(); } catch (BeansException var9) { if (this.logger.isWarnEnabled()) { this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9); } this.destroyBeans(); this.cancelRefresh(var9); throw var9; } finally { this.resetCommonCaches(); } } }

  可以看到创建bean和销毁bean的过程。  

参考资料 《深入分析javaWeb》

原文地址:https://www.cnblogs.com/Robin008/p/12500513.html