当前位置: 首页 > article >正文

Spring Boot 实例解析:HelloWorld 探究

  1.  POM 文件剖析:
    1. 父项目:
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring‐boot‐starter‐parent</artifactId>
          <version>1.5.9.RELEASE</version>
      </parent>
      他的父项目是
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring‐boot‐dependencies</artifactId>
          <version>1.5.9.RELEASE</version>
          <relativePath>../../spring‐boot‐dependencies</relativePath>
      </parent>
      他来真正管理 SpringBoot 应用里面的所有依赖版本
          SpringBoot版本控制中心
          以后倒入的依赖不需要写版本(没有在 dependencies 里面管理的依赖需要声明版本号)
    2. 启动器:
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring‐boot‐starter‐web</artifactId>
      </dependency>
    3. spring-boot-starter-web 依赖:
      1. spring-boot-starter-web:spring-boot 场景和启动器,帮我们导入了 web 模块正常运行所依赖的组件
      2. SpringBoot 将所有的功能场景都抽取出来,做成一个个的 starters (启动器),只需要在项目里面引入这些 starter
      3. 相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器
  2. 主程序类,主入口类:
    /**
    * @SpringBootApplication 来标注一个主程序类,说明这是一个SpringBoot应用
    */
    @SpringBootApplication
    public class HelloWorldMainApplication{
        public static void main(String[] args) {
             // Spring应用启动起来
            SpringApplication.run(HelloWorldMainApplication.class,args);
        }
    }
    1. @SpringBootApplication:
      1. SpringBoot 应用标注在某个类上,说明这个类是 SpringBoot 的主配置类,SpringBoot 就应该运行这个类的 Main 方法来启动 SpringBoot 应用
        @Target(ElementType.TYPE)
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        @Inherited
        @SpringBootConfiguration
        @EnableAutoConfiguration
        @ComponentScan(excludeFilters={
          @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
          @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
        public @interface SpringBootApplication{
                
                
    2. @SpringBootConfigurartion:SpringBoot 的配置类
      1. 标注在某个类上,这表示是一个 SpringBoot 的配置类
    3. @Configuration:配置类上来标注这个注解
      1. 配置类:配置文件,配置类也是容器中的一个组件。如:@Component
    4. @EnableAutoConfiguration:开启自动配置功能
      1. 以前我们需要配置的东西,SpringBoot 帮我们自动配置
    5. @EnableAutoConfiguration:告诉 SpringBoot 开启自动配置功能,这样自动配置才能生效
      
      @AutoConfigurationPackage
      @Import(EnableAutoConfigurationImportSelector.class)
      public @interface EnableAutoConfiguration {
            
            
    6. @AutoConfigurationPackage:自动配置包
    7. @Import(AutoConfigrationPackages.Regisrar.class):
      1. Spring 的底层注解,给容器中导入一个组件,导入的组件由 Auto Configuration Packages.Registart.class
      2. 将注解类(@SpringBootApplication 标注的类)的所在包下面所有的子包里面的所有组件扫描到 Spring 容器
    8. @Import(EnableAutoConfigurationInportSelector.class):
      1. 给容器中导入组件  @Enable Auto Configuration Import SelEctor:导入哪些组件的选择器
      2. 将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中。给容器中导入非常多的自动配置类(xxxAutoConfiguration),就是给容器中导入这个场景所需要的组件
    9. 配置好这些组件:
      1. 有了自动配置类,免去了我们手动编写配置注入功能的组件等工作
      2. SPringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)
      3. SpringBoot 在启动的时候从类路径下的 META-INF/spring.factories 中获取 Enable-AutoConfiguration 指定的指,这些值作为自动配置类导入容器中,自动配置类就生,帮我们进行自动装配工作

http://www.kler.cn/a/528507.html

相关文章:

  • 药店药品销售管理系统的设计与实现
  • [Linux]从零开始的STM32MP157 U-Boot移植
  • 具有HiLo注意力的快速视觉Transformer
  • 数据库性能优化(sql优化)_SQL执行计划03_yxy
  • java求职学习day23
  • python 语音识别
  • 【LeetCode 刷题】二叉树-二叉搜索树的属性
  • 我用Ai学Android Jetpack Compose之Box
  • 3 Flink 运行架构
  • 关于系统重构实践的一些思考与总结
  • 攻防世界_PHP2
  • Web-3.0(Solidity)ERC-20
  • 【Uniapp-Vue3】获取用户状态栏高度和胶囊按钮高度
  • 手撕Vision Transformer -- Day1 -- 基础原理
  • 【react-redux】react-redux中的 useDispatch和useSelector的使用与原理解析
  • 2 Flink 部署及启动
  • 基于Python的简单企业维修管理系统的设计与实现
  • TypeScript 运算符
  • 【毕业与课程大作业参考】基于 yolov8+pyqt5 界面自适应的表情识别检测系统 demo
  • Java中对消息序列化和反序列化并且加入到Spring消息容器中
  • 语音识别播报人工智能分类垃圾桶(论文+源码)
  • 使用HttpClient和HttpRequest发送HTTP请求
  • 软件工程中的需求工程
  • 电脑优化大师-解决电脑卡顿问题
  • FFmpeg(7.1版本)编译:Ubuntu18.04交叉编译到ARM
  • Scratch 《像素战场》系列综合游戏:像素战场游戏Ⅰ~Ⅲ 介绍