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的过程。  

  从一个简单的Springboot下项目的main类进入,可以看到SpringApplication.class文件,然后是创建了这个类。

参考博文:SpringApplication类解读

参考资料 《深入分析javaWeb》

posted @ 2020-03-15 22:16  懂得了才能做一些改变  阅读(432)  评论(0)    收藏  举报