栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot之parent、starter、引导类、内嵌tomcat

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot之parent、starter、引导类、内嵌tomcat

文章目录
  • 1、parent
  • 2、starter
  • 3、引导类
  • 4、内嵌tomcat

1、parent

使用parent可以帮助开发者进行版本的统一管理

  • 项目中的pom.xml中继承了一个坐标

    org.springframework.boot
    spring-boot-starter-parent
    2.5.4

  • 打开后可以查阅到其中又继承了一个坐标

    org.springframework.boot
    spring-boot-dependencies
    2.5.4

  • 这个坐标中定义了两组信息,第一组是各式各样的依赖版本号属性

    5.16.3
    1.9.7
    3.19.0
    1.15
    2.8.0
    3.12.0
    1.6
    2.9.0
    1.4.200
    5.4.32.Final
    6.2.0.Final
    4.5.13
    2.12.4
    2.0.1
    1.1.4
    1.1
    9.0.48
    4.13.2

  • 第二组是各式各样的的依赖坐标信息,可以看出依赖坐标定义中没有具体的依赖版本号,而是引用了第一组信息中定义的依赖版本属性值

    
        
            org.hibernate
            hibernate-core
            ${hibernate.version}
        
        
            junit
            junit
            ${junit.version}
        
    

  • 第二组依赖坐标是在依赖管理标签内,则表示只是引入申明,只有在子pom中使用(不用写版本号)依赖才会导入,所有即使坐标瞎写也不影响,没有子pom使用前提下。所有平常点源码可以看的好的红,因为没有实际使用,所有项目没有导入,这里只是申明

  • 如果项目有统一父pom,又需要导入spring-boot-starter-parent依赖和版本管理,也可以通过导入的方式


    
        
            org.springframework.boot
            spring-boot-dependencies
            ${spring-boot.version}
            pom
            import
        
    

总结:

  1. 开发SpringBoot程序要继承spring-boot-starter-parent
  2. spring-boot-starter-parent中定义了若干个依赖管理
  3. 继承parent模块可以避免多个依赖使用相同技术时出现依赖版本冲突
  4. 继承parent的形式也可以采用引入依赖的形式实现效果
2、starter

把所有的技术(jar)使用的固定搭配格式都给开发出来,以后你用某个技术,就不用一次写一堆依赖了

  • 项目中的pom.xml定义了使用SpringMVC技术,但是并没有写SpringMVC的坐标,而是添加了一个名字中包含starter的依赖

    org.springframework.boot
    spring-boot-starter-web

  • 在spring-boot-starter-web中又定义了若干个具体依赖的坐标

    
        org.springframework.boot
        spring-boot-starter
        2.5.4
        compile
    
    
        org.springframework.boot
        spring-boot-starter-json
        2.5.4
        compile
    
    
        org.springframework.boot
        spring-boot-starter-tomcat
        2.5.4
        compile
    
    
        org.springframework
        spring-web
        5.3.9
        compile
    
    
        org.springframework
        spring-webmvc
        5.3.9
        compile
    

starter与parent的区别

  • starter是一个坐标中定了若干个坐标,以前写多个的,现在写一个,是用来减少依赖配置的书写量的
  • parent是定义了几百个依赖版本号,以前写依赖需要自己手工控制版本,现在由SpringBoot统一管理,这样就不存在版本冲突了,是用来减少依赖冲突的

总结:

  1. 开发SpringBoot程序需要导入坐标时通常导入对应的starter
  2. 每个不同的starter根据功能不同,通常包含多个依赖坐标
  3. 使用starter可以实现快速配置的效果,达到简化配置的目的
3、引导类
  • Spring程序运行的基础是需要创建自己的Spring容器对象(IoC容器)
  • 将所有的对象交给Spring的容器管理,也就是一个一个的Bean
  • 当前这个类运行后就会产生一个Spring容器对象,并且可以将这个对象保存起来,通过容器对象直接操作Bean
@SpringBootApplication
public class SpringbootApplication {
    public static void main(String[] args) {
        //获取ioc容器
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        //获取组件
        User user = context.getBean(User.class);
        System.out.println(user);
    }
}

总结:

  1. SpringBoot工程提供引导类用来启动程序
  2. SpringBoot工程启动后创建并初始化Spring容器
  3. 默认扫描包为引导类所在包及其子包
4、内嵌tomcat
  • 查看web相关的starte

    org.springframework.boot
    spring-boot-starter-web

  • 打开查看web的starter导入了哪些东西

    
        org.springframework.boot
        spring-boot-starter
        2.5.4
        compile
    
    
        org.springframework.boot
        spring-boot-starter-json
        2.5.4
        compile
    
    
        org.springframework.boot
        spring-boot-starter-tomcat
        2.5.4
        compile
    
    
        org.springframework
        spring-web
        5.3.9
        compile
    
    
        org.springframework
        spring-webmvc
        5.3.9
        compile
    

  • 第三个依赖就是这个tomcat对应的东西了,居然也是一个starter,再打开看看

    
        jakarta.annotation
        jakarta.annotation-api
        1.3.5
        compile
    
    
        org.apache.tomcat.embed
        tomcat-embed-core
        9.0.52
        compile
        
            
                tomcat-annotations-api
                org.apache.tomcat
            
        
    
    
        org.apache.tomcat.embed
        tomcat-embed-el
        9.0.52
        compile
    
    
        org.apache.tomcat.embed
        tomcat-embed-websocket
        9.0.52
        compile
        
            
                tomcat-annotations-api
                org.apache.tomcat
            
        
    

  • 这里面有一个核心的坐标,tomcat-embed-core,叫做tomcat内嵌核心

内嵌Tomcat运行原理

  • tomcat服务器是一款软件,而且是一款使用java语言开发的软件
  • tomcat被包装为一个对象,并使用spring容器管理
  • 也可以使用依赖排除的方式,移除tomcat,添加jetty或者undertow服务器

    
        org.springframework.boot
        spring-boot-starter-web
        
            
                org.springframework.boot
                spring-boot-starter-tomcat
            
        
    
    
        org.springframework.boot
        spring-boot-starter-jetty
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号