springboot应该是在spring体系基础上发展起来的,使用springboot,可以快速构建开发项目,并快速集成相关组件(很多开源的组件都有springboot的实现了),有人说它的设计理念为约定大于配置,就是好比说在springboot中集成了web模块,那么不用自己配置,默认的配置就是自带springmvc+注解等常用功能。
springboot也把不同的功能模块化了,比如mybaitis模块,jpa模块等等,在搭建项目开始,通过maven引入相关的模块即可完成快速搭建框架。
下面从最简单的开始,创建一个springboot应用并跑起来。
一、单纯springboot应用
打开:https://start.spring.io/,直接点击完成,下载project到本地。
看下项目maven的pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1、从配置内容:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
可以看出,springboot的maven都会集成一个spring-boot-starter-parent,所以在maven中springboot的版本都是跟parent一致的。官方文档说如果项目引用了其他parent,那么这里可以不引用这个parent,改为用dependencyManagement,里面也会有一个包含所有springboot parent内容的pom被import。
2、springboot必须使用jdk1.8以上。
3、最简单的project引入了spring-boot-starter,它加载的jar包如下:
3、另外还引入了spring-boot-starter-test,这个依赖于spring-boot-starter,实现的功能基本跟junit类似,主要用测试使用,使用注解@SpringBootTest标识class。
4、引入spring-boot-starter依赖的所有jar包后,可以在@SpringBootApplication注解的类中运行main方法来启动springboot,如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
因为没有引入web模块,所有运行main方法后启动的是一个单应用,执行完毕后立刻退出:
二、启动springboot的web应用
其实idea也集成了springboot的项目构建工具,初始化后选择web模块,完成后自动下载到idea:
再来看maven的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
点击spring-boot-starter-web,可以看到它依赖了spring-boot-starter,同时依赖了spring-boot-starter-tomcat,说明web模块是在核心模块基础上构建,并且自带了嵌入的tomcat实现。
从引入的包可以发现,多了json、xml和webmvc相关的包:
运行main方法,启动的是一个内置的tomcat的web应用,可以从浏览器访问到,因为没写controller所以报错:
就是这几个步骤,从start.spring.io选择好模块,下载,启动运行。下面看几个有趣的用法:
1、运行jsp视图解析器
这个跟springmvc中的大同小异,
首先建好jsp页面:
在application.properties配置文件中指定jsp视图的前后缀:
必须引入jsp的依赖:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
写个访问的controller:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JspViewController {
@RequestMapping("/jsp")
public String returnJspView(){
return "index";
}
}
最后运行main启动web,成功访问jsp页面:
2、导出可部署在tomcat中的war包:
一是maven的配置里包含 <packaging>war</packaging>
:
<groupId>com.example</groupId>
<artifactId>demo4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo4</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
二是打包时要去除springboot内置的tomcat的所有jar包,否则冲突。
(provided作用域表示,在编译期间引用,但是发布打war包的时候去除)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
三是需要继承一个类SpringBootServletInitializer,并重写其configure方法:
(目的是加载Demo4Application类的注解配置,原理是web启动后会调用SpringBootServletInitializer的子类,功能相当于读入spring的配置文件)
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class MyInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Demo4Application.class);
}
}
最后,运行maven命令,mvn clean package打包拿到war包。