一.SpringBoot简介
简介:
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
SpringBoot优点:
- 1.实现约定大于配置,不用像以前开发应用那样需要大量的配置,SpringBoot不用配置或者使用极少的配置就能使用Spring的大部分功能
- 2.提供了内置的Servlet容器(Tomcat,Jetty)
- 3.支持热加载,开发体验好
SpringBoot文档
https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#getting-started-introducing-spring-boot
二.SpringBoot环境
必须环境
- JDK8及以上
- Maven3及以上
给Maven的setting.xml文件默认添加如下配置
<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>
使用IDEA作为主要编辑器,设置编码环境为UTF-8编码
三.从零开始SpringBoot HelloWorld
例子:发送请求,打印HelloWorld
1.使用IDEA,新建Maven工程
总体结构
2.pom.xml添加SpringBoot相关依赖
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.编写主程序,作为SpringBoot启动器
package com.fjut;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication
* 用来标识这是一个SpringBoot应用
*/
@SpringBootApplication
public class SpringBootinitApplication {
/**
* SpringBoot的主入口
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(SpringBootinitApplication.class,args);
}
}
4.编写Controller,用来处理请求
package com.fjut.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello")
public @ResponseBody String hello(){
return "Hello World SpringBoot";
}
}
5.测试
启动main程序
打印:
浏览器访问:http://localhost:8080/hello
四.Spring常用注解介绍
- @Controller:声明此类是一个MVC控制类,通常与@RequestMapping(“/xxx”)一起使用
- @Service:声明此类是一个业务处理类,通常与@Transactional一起使用
- @Repository:声明此类是一个数据库或者其他NoSQL访问类
- @RestController:同@Controller用于处理REST请求
- @Component:用于平常类
- @Configuration:声明此类是一个配置类,通常与注解@Bean使用
- @PostConstruct:声明容器初始化后调用的方法
- @PreDestory:声明容器销毁之前调用的方法
- @Autowired:根据类型自动注入,通常与@Qualifier一起使用
- GetMapping:用来处理get请求
- @PostMapping:用来处理post请求
- @PutMapping:用来处理put请求
- @DeleteMapping:用来处理delete请求
- @PatchMapping:用来处理patch请求
- @ResponseBody:将返回值以文本方式返回
- @PathVariable:用于提取URL中的参数
- @RequestParam:用于提取request中的参数
五.使用Spring Initializr快速创建SpringBoot应用
图示步骤: