springboot 源码分析

84 阅读1分钟

从图中可以看出,分为两部分:initialize, run.

SpringApplication初始化时主要做三件事情:

  • 1.根据classpath下是否存在(ConfigurableWebApplicationContext)判断是否要启动一个web applicationContext
  • 2.SpringFactoriesInstances加载classpath下所有可用的ApplicationContextInitializer
  • 3.SpringFactoriesInstances加载classpath下所有可用的ApplicationListener

实例化完成后调用run()方法 调用run()方法执行的过程主要分为以下几步:

  • 1.遍历SpringApplication初始化过程中加载的SpringApplicationRunListeners

  • 2.调用Starting()监听SpringApplication的启动

  • 3.加载SpringBoot配置环境(ConfigurableEnvironment)

  • 4.设置banner属性

  • 5.创建ConfigurableApplicationContext(应用配置上下文)

  • 6.将listeners、environment、applicationArguments、bannner等重要组件与上下文对象关联

  • 7.bean的实例化完成

  • @SpringbootApplication 标注 springboot 入口类

  • @PropertySource 读取配制文件

  • @Controller @RestController 映射url-> 函数 @RequestMapping 定义url,method, 是否json @RequestBody JSONObject data 解析json 对象. @RequestParam String type 定义key,value 键值对.

  • @Service 处理逻辑的类,实现 Component.

  • @Component 注解的类,才能被 @Autowired 自动注入

  • @MapperScan 扫描目录下文件,解析dao文件,把interface 转成实现类. @Mapper

  • @EnableScheduling @Scheduled(cron = "0 0 12 * * ?") 配合Scheduled 使用,实现定时任务.