SpringBoot-快速入门-01
该文章参考:
1.需求
搭建SpringBoot工程,定义HelloController.hello()方法,返回”Hello SpringBoot!”。
2.实现步骤
2.1 创建Maven项目
直接创建即可,不需要选择什么。
2.2 导入SpringBoot起步依赖
pom.xml文件导入如下依赖
<?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.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.study.springboot</groupId>
<artifactId>helloword</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloword</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>
2.3 定义Controller
package com.itheima.controller;
@RestController
public class HelloController {<!-- -->
@RequestMapping("/hello")
public String hello() {<!-- -->
return "Hello SpringBoot!";
}
}
2.4 编写引导类
package com.itheima;
/**
* 引导类。SpringBoot项目的入口
*/
@SpringBootApplication
public class HelloApplication {<!-- -->
public static void main(String[] args) {<!-- -->
SpringApplication.run(HelloApplication.class, args);
}
}
2.5 启动测试
直接运行main方法即可
浏览器访问
3.Spring Initializr创建SpringBoot工程
(1)直接选中Spring Initializr
(2)我们这里创建web工程,选中web即可
(3)创建完毕,SpringBoot会自动配置好需要的依赖和创建相关目录
(4)自己创建controller测试即可
4.SpringBoot起步依赖原理分析
- 在spring-boot-starter-parent中定义了各种技术的版本信息,组合了一套最优搭配的技术版本。- 在各种starter中,定义了完成该功能需要的坐标合集,其中大部分版本信息来自于父工程。- 我们的工程继承parent,引入starter后,通过依赖传递,就可以简单方便获得需要的jar包,并且不会存在版本冲突等问题
5.总结
- SpringBoot在创建项目时,使用jar的打包方式。- SpringBoot的引导类,是项目入口,运行main方法就可以启动项目。