1.搭建Spring Boot
创建maven工程(不需要指定web-app框架)
创建完工程后在pom.xml中指定父级依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--Spring Boot版本 -->
<version>2.0.1.RELEASE</version>
</parent>
然后添加spring-web启动器依赖:
<dependencies>
<!--添加spring-web启动器依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
创建SpringBoot引导类:在java下新建一个SpringBoot引导启动类,并使用SpringApplication的静态方法run(启动类的class文件)启动Spring Boot,记得给引导启动类添加@SpringBootApplication注解
package com.leon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
启动成功:
测试:创建一个Controller控制器
package com.leon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/quick")
public class QuickController {
@RequestMapping("/test1")
@ResponseBody
public String test1(){
return "hello Spring Boot";
}
}
// @responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的
//body区,通常用来返回JSON数据或者是XML数据,需要注意的呢,在使用此注解之后不会再走视图处理器,而是直接将数据写入到输
//入流中,他的效果等同于通过response对象输出指定格式的数据。
重新运行项目后在浏览器地址栏输入 http://localhost:8080/quick/test1
页面出现
说明Spring Boot环境搭建成功,Spring Boot帮我们实现了Spring MVC配置的一些功能
2.热部署
添加依赖
<!--热部署配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
添加完依赖后对Idea进行一些配置,使Idea支持自动编译
修改方式 https://www.jianshu.com/p/f658fed35786