“Springboot”的版本间的差异
(→启动类) |
|||
第155行: | 第155行: | ||
===编写 Controller=== | ===编写 Controller=== | ||
<blockquote> | |||
package com.kayak.springboot.demo.controller; | |||
import org.springframework.web.bind.annotation.GetMapping; | |||
import org.springframework.web.bind.annotation.RestController; | |||
/** | |||
* HelloController. | |||
* | |||
* 这是一个简单的Controller Demo | |||
* | |||
* @author younge | |||
* @date 2020/09/16 | |||
*/ | |||
@RestController | |||
public class HelloController { | |||
@GetMapping("/hello") | |||
public String hello() { | |||
return "Hello Spring Boot!"; | |||
} | |||
} | |||
</blockquote> | |||
===使用启动类启动运行=== | ===使用启动类启动运行=== | ||
===使用浏览器访问测试=== | ===使用浏览器访问测试=== |
2021年8月26日 (四) 07:40的版本
Spring Boot 概述
Spring Boot 是什么
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。Spring Boot采用约定优于配置的方式,大量的减少了配置文件的使用。
一般我们把Spring Boot称为搭建程序的 脚手架 或者说是 便捷搭建基于Spring的工程脚手架 。主要的作用就是帮助开发人员快速构建Spring项目,并且尽可能的减少一切xml配置,做到开箱即用,快速上手,让开发人员关注业务而非配置。Spring Boot简化了基于Spring的应用开发,只需要“run” 就能创建一个独立的、生产级别的Spring应用。Spring Boot为Spring平台及第三方库提供了开箱即用的设置,这样我们就可以简单的开始。多数Spring Boot应用只需要很少的Spring配置。
Spring Boot 的特性
- 创建独立的Spring应用
- 直接嵌入 Tomcat、Jetty 或Undertow(无需部署 WAR 文件)
- 提供固定"启动器"依赖关系,以简化构建配置
- 尽可能自动配置Spring和第三方库
- 提供生产就绪型功能,如指标、运行状况检查和外部配置
- 绝对没有代码生成,也没有 XML 配置要求
为什么要用Spring Boot
Java(Spring)一直被人诟病的一点就是臃肿、麻烦,主要原因是以下两点:
- 复杂的配置
- 混乱的依赖管理
通过上小节Spring Boot的特性,我们可以看到,Spring Boot就是用来解决这些问题的。自动配置,可以节省我们大量的配置问题,Spring Boot为Spring和其他有需要的第三方依赖提供了默认配置,我们甚至可以不做任何配置就可以启动和运行项目。依赖关系,Spring Boot通过starter引入第三方依赖,其中的所有依赖关系都是经过测试和验证的,再加上Maven等依赖管理工具避免了依赖冲突。
Spring Boot 快速启动
启动一个提供Http请求的Restful的服务接口,请求地址为http://localhost:8080/hello 返回 Hello Spring Boot!
环境准备:
- JDK1.8.X
- Maven 3.6.X
- Eclipse 4.x/STS 4.x(Spring Tools Suite)/Idea
- Spring Boot 2.3.X
- Spring 5
创建项目工程
1.创建一个Spring Starter Project
2.填写相关信息
Name: HelloSpringBoot Java Version:8 Group: com.kayak Package: com.kayak.springboot.demo
3.选择版本,配置starter(启动器)
Spring Boot Version: 2.3.X Dependencies: Spring Web
完整的pom文件
<?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.3.3.RELEASE</version>
<relativePath />
</parent>
<groupId>com.example</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloSpringBoot</name>
<description>Demo project for Spring Boot</description>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
Spring Boot 项目通过main函数即可启动
package com.kayak.springboot.demo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class HelloSpringBootApplication {
public static void main(String[] args) { SpringApplication.run(HelloSpringBootApplication.class, args); }
}
编写 Controller
package com.kayak.springboot.demo.controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
/** * HelloController. * * 这是一个简单的Controller Demo * * @author younge * @date 2020/09/16 */ @RestController public class HelloController {
@GetMapping("/hello") public String hello() { return "Hello Spring Boot!"; }
}