小架构step系列04:springboot提供的依赖

1 概述

搭建的工程例子中,指定了spring-boot的spring-boot-starter-parent作为,然后在依赖中指定了spring-boot-starter依赖,里面没有指定版本,也没看到有指定核心Spring,却能够正常运行,这是如何工作的?


    org.springframework.boot
    spring-boot-starter-parent
    2.1.18.RELEASE
    


    
        org.springframework.boot
        spring-boot-starter
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

2 依赖原理

2.1 找依赖的方法

每个依赖都有和节点,把节点和节点的信息使用/(正斜线)拼接起来,如果有点分割的地方,点也换成/(正斜线)。

然后在前面加上 https://repo.maven.apache.org/maven2/ 仓库链接,这样就可以找到这个依赖包的版本列表。选一个版本进去,找xxx.pom文件,可从里面看到该依赖包依赖了哪些其它依赖包。

比如:Springboot的包groupId为org.springframework.boot,artifactId为spring-boot-starter,那么先把org.springframework.boot转成/org/springframework/boot,在接上artifactId得/org/springframework/boot/spring-boot-starter,在前面加上仓库域名得:https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter

如果是在IDE里,也可以直接点击对应的包,如果IDE支持的话,也可以直接跳转到依赖的pom文件上。

2.2 spring-boot-starter-parent管理依赖

在 https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.7.18/spring-boot-starter-parent-2.7.18.pom 找到一个版本的pom文件,看看spring-boot-starter-parent依赖了哪些包:


    org.springframework.boot
    spring-boot-dependencies
    2.7.18

spring-boot-starter-parent

可以看到,里面也指定了节点,但没有节点,也就是说依赖应该来源于里的spring-boot-dependencies。

同样的方式找到spring-boot-dependencies的pom文件:https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/2.7.18/spring-boot-dependencies-2.7.18.pom


    5.3.31


    
        
            org.springframework
            spring-framework-bom
            ${spring-framework.version}
            pom
            import
        
        
    

 里面有个节点,这个是Maven用来管理依赖,把所有需要用到的依赖在这个节点管理起来,配置好依赖的版本,在这管理的依赖并不是马上就使用了,到了具体的pom.xml文件再挑需要使用的依赖,在真正使用依赖的时候不需要再指定版本。这样管理还有个好处就是统一版本,减少版本冲突。

在里面还是没有看到Spring的具体依赖包,但有个spring-framework-bom。再次用同样的办法找到该依赖的pom文件:https://repo.maven.apache.org/maven2/org/springframework/spring-framework-bom/5.3.31/spring-framework-bom-5.3.31.pom


    
        
            org.springframework
            spring-aop
            5.3.31
        
        
            org.springframework
            spring-aspects
            5.3.31
        
        
            org.springframework
            spring-beans
            5.3.31
        
        
            org.springframework
            spring-context
            5.3.31
        
        
            org.springframework
            spring-core
            5.3.31
        
        
            org.springframework
            spring-expression
            5.3.31
        
        
            org.springframework
            spring-jdbc
            5.3.31
        
        
    

在这里就能够看到Spring提供的各种各样的依赖。同样,这些依赖还是在节点中,只是纳入了管理,并没有真正使用。

2.3 spring-boot-starter使用依赖

在例子中的pom.xml文件里,指定了两个依赖,其中有一个是测试用的,也就是真正给业务代码用的就一个依赖spring-boot-starter,那它里面用了哪些依赖呢?

找spring-boot-starter的pom文件:https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter/2.7.18/spring-boot-starter-2.7.18.pom 


    
        org.springframework.boot
        spring-boot
        2.7.18
        compile
    
    
        org.springframework.boot
        spring-boot-autoconfigure
        2.7.18
        compile
    
    
        org.springframework.boot
        spring-boot-starter-logging
        2.7.18
        compile
    
    
        jakarta.annotation
        jakarta.annotation-api
        1.3.5
        compile
    
    
        org.springframework
        spring-core
        5.3.31
        compile
    
    
        org.yaml
        snakeyaml
        1.30
        compile
    
里面引了6个依赖:
  • spring-boot:提供SpringBoot的主体功能
  • spring-boot-autoconfigure:提供自动配置
  • spring-boot-starter-logging:提供日志打印
  • jakarta.annotation-api:提供注解API能力
  • spring-core:提供Spring的主体功能
  • snakeyaml:提供yaml文件解析和序列化功能

2.4 Spring核心依赖包

深入看一下spring-boot依赖的包: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot/2.7.18/spring-boot-2.7.18.pom 

    
        org.springframework
        spring-core
        5.3.31
        compile
    
    
        org.springframework
        spring-context
        5.3.31
        compile
    

里面多依赖一个提供Spring上下文信息的spring-context。

也深入看一下spring-core里面有没有引其它的依赖:


    
        org.springframework
        spring-jcl
        5.3.31
        compile
    

里面并没有引更多Spring核心的包。

但Spring的依赖注入、面向切面等核心功能包是在哪引入的呢?继续看starter多引的spring-context:https://repo.maven.apache.org/maven2/org/springframework/spring-context/5.3.31/spring-context-5.3.31.pom 


    
        org.springframework
        spring-aop
        5.3.31
        compile
    
    
        org.springframework
        spring-beans
        5.3.31
        compile
    
    
        org.springframework
        spring-core
        5.3.31
        compile
    
    
        org.springframework
        spring-expression
        5.3.31
        compile
    

可以看到Spring的核心包在这里都引到了,也就是说只需要用一个简单的spring-boot-starter依赖,就可以正常使用Spring的主要功能了。

另外,spring-boot-starter及相关的依赖都是明确指定了版本的,这说明spring-boot-starter并不是非得和spring-boot-starter-parent一起使用,其也可以单独使用。

3 架构一小步

引入spring-boot-starter包,使用Spring的核心功能。

你可能感兴趣的:(spring,boot,架构,java)