设为首页 加入收藏

TOP

给她讲最爱的SpringBoot源码(七)
2023-07-25 21:30:44 】 浏览:85
Tags:SpringBoot 源码
是通过读取spring.factories加载) applyInitializers(context);//设置初始化工作(不用看) // 发布上下文准备完成事件到所有监听器 listeners.contextPrepared(context);//触发监听器 bootstrapContext.close(context); if (this.logStartupInfo) { //日志操作 logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // 获取工厂 DefaultListableBeanFactory ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); //注册单例对象 beanFactory.registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { //注册banner单例对象 beanFactory.registerSingleton("springBootBanner", printedBanner); } if (beanFactory instanceof DefaultListableBeanFactory) { //是否覆盖bean ((DefaultListableBeanFactory) beanFactory) .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.lazyInitialization) { //是否懒加载 context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()); } // Load the sources Set<Object> sources = getAllSources(); Assert.notEmpty(sources, "Sources must not be empty"); //加载(业务类的注解需要扫描) bean到上下文 load(context, sources.toArray(new Object[0])); // 发送上下文加载完成事件 listeners.contextLoaded(context); }

6)刷新应用程序上下文

ioc容器初始化

重要!

tomcat的启动在这里!

  //核心方法
	private void refreshContext(ConfigurableApplicationContext context) {
		// ……
		// 开始执行启动容器(调用模板方法)
		refresh((ApplicationContext) context);
	}

扩展问题:

如果在springboot里使用了web容器,它是如何启动的?

refreshContext 里面,沿着 refresh - onRefresh,注意是 ServletWebServerApplicationContext的

我们说,在普通的spring里onRefresh是个空方法,留给子类去实现,那么,

看看这个 ServletWebServerApplicationContext 实现类它的 onRefresh偷偷干了些啥见不得人的事?……

7)容器回调方法

空方法

	protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args) {
	}

run方法启动后
主要做如下几件事情:

1、发出启动结束事件

2、执行实现ApplicationRunner、CommandLineRunner的run方法
3、发布应用程序已启动(ApplicationStartedEvent)事件

4、异常处理

小疑问:

boot启动了一个web,那么一定有一个DispacherServlet,它是啥时候被加载的呢???

提示:

@EnableAutoConfiguration 注解的spi,在spring-boot-autoconfigure的spring.factories里

EnableAutoConfiguration的加载类里有个:DispatcherServletAutoConfiguration 做了自动装配

秘密就藏在这货里

那自动装配又是什么鬼呢?除了DS,还有各种starter,怎么加载的呢?下节课继续……

2.2 boot自定义Banner

banner自动生成工具,ascii文字展示

http://www.network-science.de/ascii/

file
Spring boot启动如下

file
在路径

\spring-boot-tests\spring-boot-smoke-tests\spring-boot-smoke-test-hibernate52\src\main\resources

下创建banner.txt(注意:文件名称不能变,否则无法加载)

banner.txt内容如下

88b           d88             88888888ba                                  
888b         d888             88      "8b                          ,d     
88`8b       d8'88             88      ,8P                          88     
88 `8b     d8' 88 8b       d8 88aaaaaa8P'  ,adPPYba,   ,adPPYba, MM88MMM  
88  `8b   d8'  88 `8b     d8' 88""""""8b, a8"     "8a a8"     "8a  88     
88   `8b d8'   88  `8b   d8'  88      `8b 8b       d8 8b       d8  88     
88    `888'    88   `8b,d8'   88      a8P "8a,   ,a8" "8a,   ,a8"  88,    
88     `8'     88     Y88'    88888888P"   `"YbbdP"'   `"YbbdP"'   "Y888  
                      d8'                                                 
                     d8'                                               

2.3 面试题

1、Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的

启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:

  • @SpringBootConfiguration:组合了 @Configuration 注解,实现配置文件的功能。
  • @EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源自动配置功能: @SpringBootApplication(exclude = { DataSourc
首页 上一页 4 5 6 7 下一页 尾页 7/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇分布式事务框架 Seata 入门案例 下一篇ysoserial CommonsCollections2 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目