SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。
使用了Spring框架后已经简化了我们的开发,而SpringBoot又是对Spring开发进行简化的,可想而知SpringBoot使用的简单及广泛性。
既然SpringBoot是用来简化Spring开发的,那我们就先回顾一下,以SpringMVC开发为例
创建一个maven工程,并在pom.xml中导入所需依赖的坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
编写web3.0的配置类
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("utf-8");
return new Filter[]{filter};
}
}
编写SpringMvc配置类
@Configuration
@ComponentScan("com.blog.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
}
编写Controller类
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public boolean save(@RequestBody Book book) {
return bookService.save(book);
}
@PutMapping
public boolean update(@RequestBody Book book) {
return bookService.update(book);
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id) {
return bookService.delete(id);
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id) {
return bookService.getById(id);
}
@GetMapping
public List<Book> getAll() {
return bookService.getAll();
}
}
从上面的 SpringMVC 程序开发可以看到,前三步都是在搭建环境,而且这三步基本都是固定的。SpringBoot 就是对这三步进行简化了。接下来我们通过一个入门案例来体现 SpingBoot 简化 Spring 开发。
开发步骤
SpringBoot
的开发步骤后,下面我们进行具体的操作创建新模块;在IDEA下创建一个新模块,选择Spring Initializr,用来创建SpringBoot工程
选中 Web,然后勾选 Spring Web,由于我们需要开发一个 web 程序,使用到了 SpringMVC 技术,所以按照下图红框进行勾选
最后点击创建,就大功告成了,经过以上步骤后就创建了如下结构的模块,它会帮我们自动生成一个 Application
类,而该类一会再启动服务器时会用到
<aside> 🌷 注意:
.mvn
.gitignore
HELP.md
mvnw
mvnw.cmd
</aside>创建Controller; 在com.blog.controller
包下创建BookController,代码如下JAVA
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("get id ==> " + id);
return "hello,spring boot!";
}
}
启动服务器; 运行 SpringBoot
工程不需要使用本地的 Tomcat
和 插件,只运行项目 com.blog
包下的 Application
类,我们就可以在控制台看出如下信息
. ____ _ __ _ _
/\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
\\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.3)
2022-09-14 20:01:16.764 INFO 10956 --- [ main] c.b.Springboot01QuickstartApplication : Starting Springboot01QuickstartApplication using Java 1.8.0_321 on Kyle with PID 10956 (D:\\Study\\SpringBoot\\springboot_01_quickstart\\target\\classes started by Kyle in D:\\Study\\SpringBoot)
2022-09-14 20:01:16.770 INFO 10956 --- [ main] c.b.Springboot01QuickstartApplication : No active profile set, falling back to 1 default profile: "default"
2022-09-14 20:01:17.891 INFO 10956 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-09-14 20:01:17.900 INFO 10956 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-09-14 20:01:17.900 INFO 10956 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-14 20:01:18.060 INFO 10956 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-09-14 20:01:18.060 INFO 10956 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1211 ms
2022-09-14 20:01:18.428 INFO 10956 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-09-14 20:01:18.437 INFO 10956 --- [ main] c.b.Springboot01QuickstartApplication : Started Springboot01QuickstartApplication in 2.305 seconds (JVM running for 3.861)
进行测试
依旧是使用PostMan来测试,发送GET请求访问localhost:8080/books/9527
可以看到响应回来的结果hello,spring boot!
同时控制台也输出了get id ==> 9527
通过上面的入门案例我们可以看到使用 SpringBoot
进行开发,使整个开发变得很简单,那它是如何做到的呢?
要研究这个问题,我们需要看看 Application
类和 pom.xml
都书写了什么。先看看 Applicaion
类,该类内容如下:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这个类中的东西很简单,就在类上添加了一个 @SpringBootApplication
注解,而在主方法中就一行代码。我们在启动服务器时就是执行的该类中的主方法。
再看看 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> <https://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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.blog</groupId>
<artifactId>springboot_01_quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--JDK 的版本-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--该依赖就是我们在创建 SpringBoot 工程勾选的那个 Spring Web 产生的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
我们代码之所以能简化,就是因为指定的父工程和 Spring Web
依赖实现的。具体的我们后面在聊。
做完 SpringBoot
的入门案例后,接下来对比一下 Spring
程序和 SpringBoot
程序。
类/配置文件 | Spring | SpringBoot |
---|---|---|
pom文件中的坐标 | 手工添加 | 勾选添加 |
web3.e配置类 | 手工制作 | 无 |
Spring/SpringMVC配置类 | 手工制作 | 无 |
控制器 | 手工制作 | 手工制作 |
Spring
程序中的坐标需要自己编写,而且坐标非常多SpringBoot
程序中的坐标是我们在创建工程时进行勾选自动生成的Spring
程序需要自己编写这个配置类。这个配置类我们之前编写过,肯定感觉很复杂SpringBoot
程序不需要我们自己书写Spring/SpringMVC
程序的配置类需要自己书写。而 SpringBoot
程序则不需要书写。注意:基于Idea的 Spring Initializr
快速构建 SpringBoot
工程时需要联网。
在入门案例中之所以能快速构建 SpringBoot 工程,是因为 Idea 使用了官网提供了快速构建 SpringBoot 工程的组件实现的。 首先进入SpringBoot官网 https://spring.io/projects/spring-boot ,拉到页面最下方,会有一个Quickstart your project 然后点击Spring Initializr超链接,就会跳转到如下页面,构建工程的步骤与我们在IDEA中几乎没什么区别
点击GENERATE,就可以生成工程并下载到本地了,打开下载好的压缩包,可以看到工程的内容与IDEA生成的一模一样。 通过上面官网的操作,我们知道 Idea 中快速构建 SpringBoot 工程其实就是使用的官网的快速构建组件,那以后即使没有 Idea 也可以使用官网的方式构建 SpringBoot 工程。
Tomcat
和 Idea
,在自己电脑上启动后端程序,这显然不现实。我们后端可以将 SpringBoot
工程打成 jar
包,该 jar
包运行不依赖于 Tomcat
和 Idea
这些工具也可以正常运行,只是这个 jar
包在运行过程中连接和我们自己程序相同的 Mysql
数据库即可,这样就可以解决这个问题。SpringBoot
工程时已经在 pom.xml
中配置了如下插件<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
所以我们只需要使用 Maven
的 package
指令打包就会在 target
目录下生成对应的 Jar
包。
注意:该插件必须配置,不然打好的 jar
包也是有问题的。
进入 jar
包所在位置,在 命令提示符
中输入如下命令
java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar
执行上述命令就可以看到 SpringBoot
运行的日志信息,同时使用PostMan发送GET请求访问localhost:8080/books/9527
,也可以正常输出get id ==> 9527
D:\\Study\\SpringBoot\\springboot_01_quickstart\\target>java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
\\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.3)
2022-09-14 21:01:13.777 INFO 15992 --- [ main] c.b.Springboot01QuickstartApplication : Starting Springboot01QuickstartApplication v0.0.1-SNAPSHOT using Java 1.8.0_321 on Kyle with PID 15992 (D:\\Study\\SpringBoot\\springboot_01_quickstart\\target\\springboot_01_quickstart-0.0.1-SNAPSHOT.jar started by Kyle in D:\\Study\\SpringBoot\\springboot_01_quickstart\\target)
2022-09-14 21:01:13.783 INFO 15992 --- [ main] c.b.Springboot01QuickstartApplication : No active profile set, falling back to 1 default profile: "default"
2022-09-14 21:01:14.855 INFO 15992 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-09-14 21:01:14.869 INFO 15992 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-09-14 21:01:14.870 INFO 15992 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-14 21:01:14.977 INFO 15992 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-09-14 21:01:14.977 INFO 15992 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1150 ms
2022-09-14 21:01:15.306 INFO 15992 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-09-14 21:01:15.314 INFO 15992 --- [ main] c.b.Springboot01QuickstartApplication : Started Springboot01QuickstartApplication in 1.888 seconds (JVM running for 2.252)
2022-09-14 21:01:26.096 INFO 15992 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-09-14 21:01:26.096 INFO 15992 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-09-14 21:01:26.098 INFO 15992 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
get id ==> 9527
SpringBoot
是由Pivotal团队提供的全新框架,其设计目的是用来简化
Spring应用的初始搭建
以及开发过程
。
原始 Spring
环境搭建和开发存在以下问题
SpringBoot
程序优点恰巧就是针对 Spring
的缺点Spring
程序配置繁琐的问题Spring
程序依赖设置繁琐的问题SpringBoot
程序时既没有使用本地的 tomcat
也没有使用 tomcat
插件,而是使用 SpringBoot
内置的服务器。接下来我们来说一下 SpringBoot
的起步依赖
我们使用 Spring Initializr
方式创建的 Maven
工程的的 pom.xml
配置文件中自动生成了很多包含 starter
的依赖
<parent>
<groupId>org.springframework.boot</groupId>
<!-- ↓↓↓ -->
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/>
</parent>
<groupId>com.blog</groupId>
<artifactId>springboot_01_quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!-- ↓↓↓ -->
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<!-- ↓↓↓ -->
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
这些依赖就是启动依赖,接下来我们探究一下它是如何实现的。
从上面的文件中可以看到指定了一个父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/>
</parent>
我们进入到父工程,发现父工程中又指定了一个父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.3</version>
</parent>
再进入到该父工程中,在该工程中我们可以看到配置内容结构如下
properties
标签中定义了各个技术软件依赖的版本,避免了我们在使用不同软件技术时考虑版本的兼容问题。dependencyManagement
标签是进行依赖版本锁定,但是并没有导入对应的依赖;如果我们工程需要那个依赖只需要引入依赖的 groupid
和 artifactId
不需要定义 version
。build
标签中也对插件的版本进行了锁定XML看完了父工程中 pom.xml
的配置后不难理解我们工程的的依赖为什么都没有配置 version
。
在我们创建的工程中的pom.xml中配置了如下依赖