愿你如阳光,明媚不忧伤。
目録
- 1. 什么是SpringBoot
- 2. 访问 Spring 官网下载 Spring Boot Demo
- 3. 使用 Spring Tool 插件创建
- 4. 使用 Maven 创建
- 【每日一面】
- SpringMVC 和 SpringBoot 的区别
1. 什么是SpringBoot
SpringBoot 基于 Spring Framework 构建,本质上是一些库的集合,它能够被任意项目的构建系统所使用,它使用 “习惯优于配置” (Convention over Configuration 项目中存在大量的配置,此外还内置一个习惯性的配置)的理念让你的项目快速运行起来。是一个配置工具,整合工具,辅助工具。
2. 访问 Spring 官网下载 Spring Boot Demo
下载完成后,解压,打开 Eclipse → File → Import 选择从文件夹中导入项目,输入刚才解压好的 Demo 路径,一路确定。
稍等片刻,可以看到项目管理中已经出现 demo 项目了。 Run As Spring Boot App
成功运行!!!
3. 使用 Spring Tool 插件创建
Help → Eclipse Marketplace 进入到 Eclipse 应用市场
搜索 “sts” 下载 Spring Tools 4 插件,安装。
File → New → Project 创建新的项目
可以看到可供选择的向导中多出了 Spring Boot 选项,选择创建 Spring Starter Project
填写一些基本信息
添加依赖,这一步可以跳过,以后可以在 Maven 的 pom.xml 里根据需求进行添加
稍等片刻,可以看到项目管理中已经出现 SpringBootDemo 项目了,在 webapp 下新建 index.jsp 里面随便写点什么
部署到 Tomcat 中,运行。访问 http://localhost:8080/SpringBootDemo/
4. 使用 Maven 创建
- 创建 Maven 项目
Alt+Shift+N
填上项目的基本信息,直接完成
选择建好的 MavenBoot Alt+Enter
更改 JRE,应用并关闭
更改项目特征,应用并关闭
回到项目 Alt+F5
- application.yml 全局配置文件(样例,仅供参考,以后篇章会详细介绍,里面可暂时为空)
#内置Tomcat配置
server:
tomcat:
uri-encoding: UTF-8
max-http-form-post-size: -1
connection-timeout: 5000ms
threads:
min-spare: 30
max: 1000
port: 8089
servlet:
context-path: /
session:
cookie:
http-only: true
- 修改 pom.xml 添加 Starter 起步依赖
<?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>
<!-- lookup parent from repository -->
<!-- Spring Boot的版本仲裁中心,控制了所有依赖的版本号,以后导入依赖包就不用写版本号了 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath />
</parent>
<!-- spngboot中项目部署信息 -->
<groupId>com.ITGodRoad</groupId>
<artifactId>MavenBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>MavenBoot</name>
<description>MavenBoot project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- spngboot中web项目的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot中tomcat的依赖,provided表明该包只在编译和测试的时候用,所以,当启动tomcat的时候,就不会冲突了 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- springboot测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- springboot项目的编译配置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- Alt+F5
***********【DemoApplication】Spring Boot 启动类***********
package com.example.demo;
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);
}
}
***********【ServletInitializer】servlet初始化类***********
package controller;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}
在 webapp 下新建 index.jsp 然后把项目部署到 Tomcat 中,启动
浏览器输入网址:localhost:8080/MavenBoot
【每日一面】
SpringMVC 和 SpringBoot 的区别
SpringMVC 提供了一种轻度耦合的方式来开发web应用,Spring MVC是Spring的一个模块,一个web框架。通过Dispatcher Servlet, ModelAndView 和 View Resolver,开发web应用变得很容易。解决的问题领域是网站应用程序或者服务开发——URL路由、Session、模板引擎、静态Web资源等等。
SpringBoot 只是承载者,辅助简化项目搭建过程。如果承载的是WEB项目,使用Spring MVC作为MVC框架,那么工作流程和是完全一样的,因为这部分工作是Spring MVC做的而不是Spring Boot。同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等)。