一.SpringBoot简介
1.SpringBoot是什么?
产生背景:Spring开发变得越来越笨重,大量的XML文件,繁琐的配置,复杂的部署的流程,整合第三方框架难度大
二.SpringBoot项目创建
1、官网创建项目
https://spring.io/projects/spring-boot
https://start.spring.io/
2、IDEA脚手架创建 SpringBoot Initializr
3、通过IDEA的Maven创建
三.springboot项目结构介绍
1.配置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.2.0.RELEASE</version> <!-- springboot 版本 --!>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootdemo</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.1继承
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version> <!-- springboot 版本 --!>
<relativePath/> <!-- lookup parent from repository -->
</parent>
SpringBoot 的父级依赖,只有继承他项目才是SpingBoot项目
spring-boot-parent是一个特殊的starter,它是用来提供相关的Maven依赖。使用它之后,常用的包依赖可以省去version标签
1.2依赖
启动器依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1.3插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Spring Boot 的启动类的作用是启动Spring Boot项目,是基于main方法来运行的。
注意:启动类在启动时会注解扫描@Controller、@Service、@Repository....),扫描位置为同包或者子包下的注解,所以启动类的位置应放于包的根下。
2.启动类
2.1启动类与启动器的区别:
* 启动类表示项目的启动入口
* 启动器表示jar包的坐标
2.2创建启动类
/**
*spring boot 启动类
*/
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
3.启动器
Spirng Boot 将所有的功能场景都抽取出来,戳成一个个starter(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来,要用到什么功能就导入什么场景,在jar包管理上非常方便,最总实现一站式开发
Spring Boot 提供了多达 44 个启动器。
- spring-boot-starter 这是 Spring Boot 的核心启动器,包含了自动配置、日志和 YAML。
- spring-boot-starter-actuator 帮助监控和管理应用。
- spring-boot-starter-web 支持全栈式 Web 开发,包括 Tomcat 和 spring-webmvc。
4.配置文件
Spring Boot 提供一个名称为application的全局配置文件,支持两种格式properteis格式和YAML格式
4.1Properties格式
配置TOmcat监听端口
server.port=8080