Spring Boot 入门项目
SpringBoot 知识点目录: SpringBoot 核心知识点整理!
springboot 介绍
springboot 引言
Spring Boot 是由 Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的 初始搭建以及开发过程。该框架使用了 特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot 致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
springboot(微框架) = springmvc(控制器) + spring core(项目管理)
框架的演变过程:
- SSH: Spring — Struts — Hibernate
- SSM: Spring — Struts2 | Struts1 — Mybatis
- SSM: Spring — SpringMVC — Mybatis
springboot 特点
- 创建独立的 Spring 应用程序
- 嵌入的 Tomcat,无需部署 WAR 文件
- 简化 Maven 配置
- 自动配置 Spring,没有 XML 配置!
springboot 约定大于配置
项目目录结构:
springboot 项目中必须在 src/main/resources
中 放入 application.yml
(application.properties
)核心配置文件,名字必须为:application
;
springboot 项目中必须在 src/main/java
中所有子包之外构建全局入口类型 xxApplication
,入口类一个 springboot 项目只能有一个。
springboot 入门项目
项目整体结构:
1、引入项目依赖
<!--继承springboot的父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<dependencies>
<!--引入springboot的web支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2、引入配置文件
配置文件位于:src/main/resources/application.yml
;
可以通过修改端口号解决 tomcat 端口占用问题:
server:
port: 8989 #用来指定内嵌服务器端口号
context-path: /springboot #用来指定项目的访问路径
yml 基础语法
person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists: # 数组
- lisi
- zhaoliu
dog:
name: 小狗
age: 12
3、 建包并创建控制器 HelloController
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/hello")
public String hello() {
System.out.println("hello springboot!!!");
return "hello springboot";
}
}
4、编写入口类 Application
// @EnableAutoConfiguration // 作用: 开启自动配置 初始化spring环境 springmvc环境
// @ComponentScan // 作用: 用来扫描相关注解 扫描范围 当前入口类所在的包及子包(com.yusal及其子包)
// 下面的注解的功能是上面两个注解功能之和
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// springApplication: spring应用类 作用: 用来启动springboot应用
// 参数1: 传入入口类 类对象 参数2: main函数的参数
SpringApplication.run(Application.class, args);
}
}
5、启动项目并访问
运行 Application 中的 main 来启动整个项目:如下则启动成功。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-06-19 21:56:45.166 INFO 8888 --- [ main] com.yusael.Application : Starting Application on DESKTOP-PNEMUCC with PID 8888 (D:\CodePro\IdeaPro\SpringBoot\hello_springboot\target\classes started by yusael in D:\CodePro\IdeaPro\SpringBoot)
2020-06-19 21:56:45.171 INFO 8888 --- [ main] com.yusael.Application : No active profile set, falling back to default profiles: default
2020-06-19 21:56:46.716 INFO 8888 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8989 (http)
2020-06-19 21:56:46.730 INFO 8888 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-19 21:56:46.731 INFO 8888 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-06-19 21:56:46.855 INFO 8888 --- [ main] o.a.c.c.C.[.[.[/hello_springboot] : Initializing Spring embedded WebApplicationContext
2020-06-19 21:56:46.856 INFO 8888 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1613 ms
2020-06-19 21:56:47.244 INFO 8888 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-19 21:56:47.473 INFO 8888 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8989 (http) with context path '/hello_springboot'
2020-06-19 21:56:47.487 INFO 8888 --- [ main] com.yusael.Application : Started Application in 3.253 seconds (JVM running for 4.806)
2020-06-19 21:57:28.156 INFO 8888 --- [nio-8989-exec-3] o.a.c.c.C.[.[.[/hello_springboot] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-19 21:57:28.157 INFO 8888 --- [nio-8989-exec-3] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-06-19 21:57:28.165 INFO 8888 --- [nio-8989-exec-3] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
注意:springboot 的项目默认没有项目名,项目名要在 application.yml
中配置;
如果不在配置文件中配置,路径应该是:
http://localhost:8080/hello/hello
由于我们在配置文件中进行了配置:
server:
port: 8989 #用来指定内嵌服务器端口号
context-path: /springboot #用来指定项目的访问路径
所以访问路径变成了:
http://localhost:8989/springboot/hello/hello
相关注解
@EnableAutoConfiguration
- 作用:开启自动配置 ,根据 pom.xml 文件中依赖自动判断并构建环境
如在第一个环境之中引入了 spring-boot-starter-web, 会自动根据引入的这个依赖构建相关环境(springmvc环境、web容器环境) - 修饰范围:只能用在类上
@ComponentScan
- 作用:用来开启注解扫描
- 修饰范围:只能用在类上
- 扫描注解范围:默认当前包以及当前包下的子包
@SpringBootApplication
:@EnableAutoConfiguration
+ @ComponentScan
@RestController
- 作用:用来实例化当前对象为一个控制器对象,并将类上的所有方法的返回值转为 json,响应给浏览器。
- 修饰范围: 用在类上
- 功能相当于:
@Controller
+@ResponseBody
@Controller
:实例化当前类为一个控制器@ResponseBody
:用来将当前方法返回值转为 json, 相应给浏览器
@RequestMapping
- 作用:用来加入访问路径
- 修饰范围:类(加入命名空间)、方法上(指定具体路径)
- 同类注解:
@GetMapping
:作用: 限定请求方式只能是GET,并指定路径;修饰范围:方法上
@PostMapping
@DeleteMapping
@PutMapping
main
方法的作用:指定通过标准 java 入口方式委托给 SpringApplication
,并告知当前 springboot
主应用类是谁,从而启动 springboot 中 tomcat 容器;
args
作用:可以在启动时使用外部参数
starter
:启动器,springboot-starter-xxx Starters 是一组方便的依赖关系描述符。