文章目录
什么是SpringBoot
SpringBoot官方有一句话可以概括这个问题,那就是「约定大于配置」。这句话什么意思?相信学过Spring 的人都知道,我们要手动写一大堆的xml文件用于配置,集成项目,才能使这个项目具备web的功能。而SpringBoot 做了那些没有它你也会去做的SpringBean配置。它使用「约定大于配置」的理念让你的项目快速运行起来。使用SpringBoot 很容易创建一个独立运行(运行jar,内嵌Servlet容器)、准生产级别的基于Spring框架的项目,使用SpringBoot 你可以不用或者只需要很少的Spring配置。
如果说 Spring 是一辆汽车的引擎,那SpringMVC就给这辆汽车装上了轮子,而SpringBoot的出现就相当于赋予了这辆汽车自动驾驶的功能。
为什么要使用SpringBoot(摘自官网)
- Create stand-alone Spring applications
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
- Provide opinionated ‘starter’ dependencies to simplify your build configuration
- Automatically configure Spring and 3rd party libraries whenever possible
- Provide production-ready features such as metrics, health checks, and externalized configuration
- Absolutely no code generation and no requirement for XML configuration
前置步骤
第一步 安装JDK
参照此前文章进行安装: JDK安装步骤
第二步 安装maven
-
官网下载: maven下载官网
-
下载完成后解压缩
-
在同级目录下新建maven仓库,命名:repo(其实随意)
-
进入conf文件夹下,打开settings.xml文件,在settings标签下设置maven本地仓库
<localRepository>(本地maven仓库地址)</localRepository>
在mirrors下面写入阿里云镜像(maven自身的中央仓库下载太慢了)
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
第三步 安装IDEA
- 官网下载: idea官网
- 按照一步步往下操作即可
第四步 IDEA配置maven
利用idea创建SpringBoot项目
-
打开idea
-
新建项目
点击"下一步"进入下一步,这一步就是选你的项目依赖包,前文所说的「约定大于配置」就体现在这里。进入选择S pring Boot 版本和依赖管理的窗口。在这里值得我们关注的是,它不仅包含了Spring Boot Starter POMs 中的各个依赖,还包含了Spring Cloud的各种依赖。
比如,你需要集成前端模板功能,你就到Template Engines选项卡上,勾选你想要访问的前端模板引擎 ,项目需要访问数据库,就到SQL选项卡,选择你项目里使用的数据库类型。选择完成并加以简单的配置,项目就具备了集成前端模板能力与数据库访问能力。
这里注意一下,无论你选择哪些依赖包,其中web选项卡下的Web 是必选的。这个包是整个项目的基础。这个包里面集成了 Spring、WebMvc、tomcat以及其他各种基本能力。