Spring Boot核心原理

spring-boot-starter-xxx  方便开发和配置

@SpringBootApplication    //注解
public class Springbootdemo1Application { public static void main(String[] args) {
//严格意义上执行的是这块代码
SpringApplication.run(Springbootdemo1Application.class, args);
} }

一、 SpringBootApplication注解

注解的功能:参考https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/#boot-documentation

除了元注解,还有三个注解

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan( excludeFilters =
{@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
@SpringBootConfiguration 注解

让我们当前的Bean叫给Spring容器进行管理(IOC),让当前类变成配置类,不需要XML文件进行配置(配置类)。


EnableAutoConfiguration注解

@AutoConfigurationPackage
  让包中的类以及子包中的类能被自动扫描到Spring 容器中
@Import({AutoConfigurationImportSelector.class})
  程序中默认使用的类帮我们找到
AutoConfigurationImportSelector类如下: 里面的selectImports里面,调用了getAutoConfigurationEntry方法。

getAutoConfigurationEntry方法中调用getCandidateConfigurations方法

getCandidateConfigurations方法使用的文件在META-INF/spring.factories

META-INF/spring.factories保存了系统默认加载进来的类。
这个文件的路径如下图:


@ComponentScan注解
通常它会结合@Coponent相关东西进行使用 总结:@SpringBootApplication
结合Spring MVC:
系统可能用到的Bean,帮我们放在了spring.factories 文件夹中
自己需要加载的bean, @Component结合@ComponentScan

二、 SpringApplication.run(Springbootdemo1Application.class, args);

  程序启动的时候执行这段代码,

1、寻找内置的Tomcat执行的地方

this.refreshContext(context);
this.refresh(context);
((AbstractApplicationContext)applicationContext).refresh();
this.onRefresh()
onRefresh(ServletWebServerApplicationContext类中)
this.createWebServer();
 factory.getWebServer

最终找到内置创建Tomcat的方法

2、@SpringBootApplicaton注解是如何准备类的? 如何理解spring.factories准备的类,然后拿到准备类去创建具体的对象?

SpringApplication.run(Springbootdemo1Application.class, args):  拿到准备类中的文件--> 具体创建对象

以下图创建Tomcat实例为例

spring.factories中有一个与TomcatServletWebServerFactory相关的配置: ServletWebServerFactoryAutoConfiguration

ServletWebServerFactoryAutoConfiguration类如下

可以看到@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class})。

然后进入EmbeddedTomcat类

自动配置:auto-configuration

注解在spring.factories中帮你维护好了所谓的全路径

代码执行的过程中你会用到的话,就会寻找对应的类

最新文章

  1. git与github使用
  2. 使用git把项目提交到github
  3. (一)startup.bat
  4. POJ3211 Washing Clothes[DP 分解 01背包可行性]
  5. 数组内部对象排序(sort)
  6. loadrunner中lr_save_searched_string函数的使用
  7. python发送邮件方法
  8. 在Lua里写unity游戏笔记
  9. 用C#调用蓝牙编程
  10. Objective-C Runtime 运行时之五:协议与分类
  11. Windows下配置Nginx使之支持PHP(转)
  12. 无组件客户端js图片压缩
  13. 《SAS编程和数据挖掘商业案例》第14部分学习笔记
  14. egret GUI 文本混排+文本链接的聊天解决方案【取巧法】
  15. .Net Ajax跨域请求实现
  16. dao层、service和action的运用和区别
  17. PHP发送POST请求
  18. 第一个VS2015 Xaramin Android项目(续)
  19. Java线程中的join使用实例
  20. 小程序学习一 .json 文件配置

热门文章

  1. JavaScript: 自动类型转换
  2. 爬虫之cookie与代理
  3. python之while循环用法举例,break与continue的区别,格式化输出及运算符
  4. tengine编译安装及nginx高并发内核参数优化
  5. [openssl][nginx] 使用openssl模拟ssl/tls客户端测试nginx stream
  6. SELinux 权限设置
  7. CM的私有仓库搭建
  8. ymPrompt简介
  9. C#编译相关知识
  10. ubbuntu---多媒体播放器VLC