Spring Boot 优点
快速构建独立运行的Spring项目;
无须依赖外部Servlet容器,应用无需打成WAR包;项目可以打成jar包独自运行;
提供 一系列 starter pom 来简化 Maven 的依赖加载;
大量的自动配置,对主流开发框架的无配置集成;
无须配置XML,开箱即用,简化开发,同时也可以修改默认值来满足特定的需求;
Spring Boot 并不是对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式;
极大提高了开发、部署效率。
环境要求
jdk1.8 (Spring Boot 推荐jdk1.8及以上): java version "1.8.0_151"
Maven 3.x (maven 3.2 以上版本):Apache Maven 3.3.9
IntelliJ IDEA :IntelliJ IDEA 2018.2.2 x64
SpringBoot 使用当前最新稳定版本:第5章web开发前 2.0.6.RELEASE ,后面使用 2.1.0.RELEASE
修改Maven配置文件
在 Maven 安装目录下的 settings.xml 配置文件中, 添加如下配置
<!--开始处更改下载依赖的存放路径, 以下目录需要已经创建-->
<localRepository>D:\javasource\maven-repository</localRepository>
<!--在 mirrors 标签下 添加阿里云maven私服库-->
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<!-- 在 profiles 标签下指定jdk版本 -->
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
IntelliJ IDEA 设置
在idea上将 maven 环境添加进来
快速构建 Spring Boot 项目
需求:浏览器发送 /hello 请求,服务器接受请求并处理,响应 Hello World 字符串
分析 :构建 Spring Boot 项目,事实上建立的就是一个 Maven 项目
创建 Maven工程
在 IDEA上新建一个空的jar类型 的 maven 工程
修改 pom.xml
在 pom.xml 中添加 Spring Boot 相关的父级依赖, spring-boot-starter-parent 是一个特殊的starter,
它提供了项目相关的默认依赖,使用它之后 ,常用的包依赖可以省去 version 标签。
在 dependencies 添加构建 Web 项目相关的依赖
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
创建控制器 Controller
package com.mengxuegu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "HelloWorld...";
}
}
创建一个引导类
主要作用是作为启动 Spring Boot 项目的入口
package com.mengxuegu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 用于标识一个主程序类,说明当前是Spring Boot项目
* @Description: com.mengxuegu
* @Auther: www.mengxuegu.com
* @Version: 1.0
*/
@SpringBootApplication
public class HelloMailAppliation {
public static void main(String[] args) {
SpringApplication.run(HelloMailAppliation.class, args);
}
}
运行效果
在浏览器地址栏输入 localhost:8080/hello 即可看到运行结果
简化部署
在 pom.xml 添加如下插件后, 将这个工程打成 jar 包后,可直接通过 java -jar 的命令运行
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如下操作进行打成 jar 包, 从控制台可发现 打成的jar包所在目录