博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot入门(二)——起步依赖
阅读量:6969 次
发布时间:2019-06-27

本文共 5326 字,大约阅读时间需要 17 分钟。

本文来自

在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置。这一篇先来看看起步依赖。

项目构建过程解析

前面提到,Spring Boot构建出来的也是一个Maven项目,可以看下自动生成的pom.xml文件:

    
4.0.0
    
top.godtm
    
blog-demo
    
0.0.1-SNAPSHOT
    
jar
    
blog-demo
    
Demo project for Spring Boot
    
        
org.springframework.boot
        
spring-boot-starter-parent
        
2.0.0.RELEASE
        
 
    
    
        
UTF-8
        
UTF-8
        
1.8
    
    
        
            
org.springframework.boot
            
spring-boot-starter-thymeleaf
        
        
            
org.springframework.boot
            
spring-boot-starter-web
        
        
            
org.springframework.boot
            
spring-boot-starter-test
            
test
        
    
    
        
            
                
org.springframework.boot
                
spring-boot-maven-plugin
            
        
    

去掉一些必要的配置,可以看到只引入了3个依赖。其中spring-boot-starter-thymeleaf是我自己额外引入的可以忽略,那么剩下的就只有spring-boot-starter-web和spring-boot-starter-test了。spring-boot-starter-test是用于编写测试使用的,可以认为跟项目功能没有直接关系。

结果就是:我们为了编写一个简单的Hello World Web项目,只需要引入一个依赖即可,就这么easy!

起步依赖

这里看到的spring-boot-starter-xxx就是SpringBoot的起步依赖。SpringBoot通过提供众多起步依赖降低项目依赖的复杂度。起步依赖本质上是一个Maven项目对象模型,定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。很多起步依赖的命名都暗示了他们提供的某种或某类功能。

以spring-boot-starter-web为例,追踪它的pom文件可以看到熟悉的东西:

  
4.0.0
  
    
org.springframework.boot
    
spring-boot-starters
    
2.0.0.RELEASE
  
           
org.springframework.boot
  
spring-boot-starter-web
  
2.0.0.RELEASE
  
Spring Boot Web Starter
  
Starter for building web, including RESTful, applications using Spring        MVC. Uses Tomcat as the default embedded container
  
https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-web
  
    
Pivotal Software, Inc.
    
https://spring.io
  
  
    
      
Apache License, Version 2.0
      
http://www.apache.org/licenses/LICENSE-2.0
    
  
  
    
      
Pivotal
      
info@pivotal.io
      
Pivotal Software, Inc.
      
http://www.spring.io
    
  
  
    
scm:git:git://github.com/spring-projects/spring-boot.git/spring-boot-starters/spring-boot-starter-web
    
scm:git:ssh://git@github.com/spring-projects/spring-boot.git/spring-boot-starters/spring-boot-starter-web
    
http://github.com/spring-projects/spring-boot/spring-boot-starters/spring-boot-starter-web
  
  
    
Github
    
https://github.com/spring-projects/spring-boot/issues
  
  
    
      
org.springframework.boot
      
spring-boot-starter
      
2.0.0.RELEASE
      
compile
    
    
      
org.springframework.boot
      
spring-boot-starter-json
      
2.0.0.RELEASE
      
compile
    
    
      
org.springframework.boot
      
spring-boot-starter-tomcat
      
2.0.0.RELEASE
      
compile
    
    
      
org.hibernate.validator
      
hibernate-validator
      
6.0.7.Final
      
compile
    
    
      
org.springframework
      
spring-web
      
5.0.4.RELEASE
      
compile
    
    
      
org.springframework
      
spring-webmvc
      
5.0.4.RELEASE
      
compile
    
  

在这一层已经能看到它为我们传递了spring-web和spring-webmvc。

关于依赖的版本号

说到起步依赖,还有一个不得不提的好处——版本号管理。

回想以前,当我们需要为项目添加一个新的依赖时是不是挺纠结?

我们不可能对每个引入依赖都了如指掌,很难确定我们选择的版本是否合适,是否会与其他依赖产生冲突,是否是一个存在问题的版本等等。

SpringBoot官方提供的起步依赖都和SpringBoot版本紧密相连,为我们传递的第三方依赖是经过足够测试后敲定下来最合适的版本。

这是一种解脱~

小结

这一章我们介绍了SpringBoot能够快速构建项目的魔力之一——起步依赖。基于不同的功能,官方为我们整合了大量的起步依赖,简化了我们搭建项目的工作。同时,起步依赖提供了可靠的依赖管理,降低了项目引入问题版本和依赖冲突的风险。

 

相关阅读:

 

网易云新用户大礼包:

 

本文来自网易云社区,经作者金港生授权发布。

转载地址:http://toisl.baihongyu.com/

你可能感兴趣的文章
intelliJ idea运行新的test功能时,报错:class not found "....." empty test suite
查看>>
3.使用Maven构建Web项目
查看>>
Eclipse常用设置
查看>>
原生css3作响应式布局
查看>>
.net 中对于ToString的使用
查看>>
cygwin 安装apt-cyg命令
查看>>
linux fdisk
查看>>
HDU4625 JZPTREE——第二类斯特林数
查看>>
linux 排查page的状态问题
查看>>
初学者对编译原理是什么,作用,优点,
查看>>
Codeforces 556D - Case of Fugitive
查看>>
Codeforces 932D - Tree
查看>>
BZOJ 1010: [HNOI2008]玩具装箱toy
查看>>
django之路由分发
查看>>
51nod1832 先序遍历与后序遍历
查看>>
MD5 带salt 加密
查看>>
13 用Css做下拉菜单
查看>>
homework-01
查看>>
修改WAMPServer中MySql中文乱码的方法
查看>>
【下载】推荐一款免费的人脸识别SDK
查看>>