Spring boot快速入门
Spring boot快速入门
一、什么是spring boot
官网原文:Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
翻译:Spring Boot可以很容易地创建独立的、基于Spring的生产级应用程序,您只需“运行”即可。
特性:
- 创建独立的Spring应用程序
- 直接嵌入Tomcat、Jetty或Undertow(不需要部署WAR文件)
- 提供快捷的“启动器”依赖项来简化构建配置
- 尽可能地自动配置Spring和第三方库
- 提供可用于生产的特性,如度量、运行状况检查和外部化配置
- 不需要生成代码,也不需要XML配置
二、搭建一个spring boot程序
本案例采用IDEA搭建一个spring boot工程
需要稍等片刻自动加载依赖,就创建好了一个spring boot工程。
这里我们可以把mybatis依赖注释掉,防止测试没有数据库连接信息报错。具体代码如下:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.1
com.it
springboot-demo
0.0.1-SNAPSHOT
springboot-demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-web-services
2.4.1
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
其中有个Application类,是程序的入口:
在工程目录下有一个resource文件,其中有一个application.properties文件,默认为空,需要自行写配置信息,重命名为application.yml。
(此步骤不操作也可以实现快速入门)
server:
port: 8080
servlet:
context-path: /springboot
写一个开始类
package com.it.springbootdemo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = {"/hello"},method = RequestMethod.GET)
public String hello(){
return "Hello you!";
}
}
运行Application的main(),启动项目,因为springboot自动内置了servlet容器,故不需要传统方式,先部署到容器在启动容器。只需要运行入口方法即可,启动后打开浏览器输入网址:localhost:8080/springboot/hello,就可以看到信息。