SprintBoot简单入门
1、什么是SpringBoot
SpringBoot
是基于Spring
的基础上提供了一套全新的框架,其目的是为了在开发时简化Spring
的相关配置及开发过程。在SpringBoot
未出来之前,准备搭建一个Spring
的开发环境需要配置一堆的XML
文件,而SpringBoot
就是去除了大量的XML
配置文件,简化了复杂的依赖管理。
Spring Boot
集成了大量常用的第三方库配置,Spring Boot
应用中这些第三方库几乎可以是零配置的开箱即用(out-of-the-box
),大部分的Spring Boot
应用都只需要非常少量的配置代码(基于Java
的配置),开发者能够更加专注于业务逻辑。
2、SpringBoot特征
- 独立运行的
Spring
项目,使用jar
包的形式独立运行,只需通过命令java -jar xx.jar
即可运行。 - 内嵌
Servlet
容器(例如Tomcat
、Jetty
或者Undertow
等),应用无需打成WAR
包 。 - 提供
starter
简化Maven
配置,提供了一系列的starter
项目对象模型(POMS
)来简化Maven
配置。 - 提供了大量的默认自动配置,来简化项目的开发,开发人员也通过配置文件修改默认配置。
- 自带应用监控(如指标、健康检查和外部化配置)。
- 没有代码生成和
XML
配置。
3、快速搭建SpringBoot
- 引入
maven
依赖
首先引入parent
依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
</parent>
因为是要开发一个web
项目,因此需要引入web
依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 创建启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- 创建
controller
类
@RestController
@RequestMapping(path = "/v1/index")
public class IndexController {
@GetMapping(path = "")
public String index(){
return "Hello Spring Boot!";
}
}
- 启动项目
Connected to the target VM, address: '127.0.0.1:54766', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.3)
2021-08-04 23:16:55.694 INFO 15528 --- [ main] com.tenghu.sb.Application : Starting Application using Java 1.8.0_301 on Arvin with PID 15528 (E:\project\java\spring-boot-study\target\classes started by admin in E:\project\java\spring-boot-study)
2021-08-04 23:16:55.696 INFO 15528 --- [ main] com.tenghu.sb.Application : No active profile set, falling back to default profiles: default
2021-08-04 23:16:56.231 INFO 15528 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-08-04 23:16:56.237 INFO 15528 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-08-04 23:16:56.237 INFO 15528 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.50]
2021-08-04 23:16:56.292 INFO 15528 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-08-04 23:16:56.292 INFO 15528 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 567 ms
2021-08-04 23:16:56.503 INFO 15528 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-08-04 23:16:56.509 INFO 15528 --- [ main] com.tenghu.sb.Application : Started Application in 1.065 seconds (JVM running for 1.607)
2021-08-04 23:17:07.919 INFO 15528 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-08-04 23:17:07.919 INFO 15528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-08-04 23:17:07.920 INFO 15528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
看到这个结果,表示SpringBoot
项目就已经启动成功了,现在使用浏览器访问路径http://localhost:8080/v1/index
就可以看到浏览器输出结果:Hello Spring Boot!
。
以上是手动建立一个空的maven
项目,手动添加依赖的方式创建一个简单的spring boot
项目,另外还有两种方式可以快速生成spring boot
项目:
- 使用IDEA,里面已经集成了SpringBoot插件
- 使用官网提供的创建SpringBoot插件。https://start.spring.io/