一.背景
简化开发
springboot通过默认配置了很多框架的使用方式帮我们大大简化了项目初始搭建以及开发过程;
通常搭建一个基于spring的web应用,我们需要做以下工作:https://www.cnblogs.com/hjwublog/p/10332042.html
1、pom文件中引入相关jar包,包括spring、springmvc、redis、mybaits、log4j、mysql-connector-java 等等相关jar ...
2、配置web.xml,Listener配置、Filter配置、Servlet配置、log4j配置、error配置 ...
3、配置数据库连接、配置spring事务
4、配置视图解析器
5、开启注解、自动扫描功能
6、配置完成后部署tomcat、启动调试
......
搭个初始项目不一会就一个小时甚至半天过去了。而用springboot后,一切都变得很简便快速
二.搭建springboot项目
可以通过https://start.spring.io/来创建springboot基础项目骨架
生成的.zip解压到本地,导入ide中可以看到springboot基本目录结构:
着重看下pom的配置:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
spring-boot-starter 是官方提供的springbot启动类
直接运行 GwzApplication
springboot已经启动成功了。
pom中我们可以添加springboot的web模块的依赖,默认使用tomcat启动服务
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
再次运行GwzApplication
可以看到web模块已经加入,默认tomcat启动,端口是8080
假如不想要默认的tomcat启动服务,换成jetty,则可以通过以下pom引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
再次运行观察启动信息,可以看到已经切换成了jetty启动