SpringBoot
1 概述
Spring Boot是一个框架,一种全新的编程规范,简化了框架的使用,所谓简化是指简化了Spring 众多框架中所需的大量且繁琐的配置文件,所以Spring Boot是一个服务于框架的框架,服务范围是简化配置文件。所以从本质上来说,SpringBoot其实就是Spring框架的另一种表现形式。
2 版本
3 创建SpringBoot项目
3.1 方式一:在SpringBoot官网创建
地址:SpringBoot官网
将压缩包导入Idea即可。
3.2 方式二:由Idea脚手架创建
打开idea,选择创建项目,选择Spring Initializr,然后根据步骤完成即可。内容和官网类似,本质上就是从官网下载来的。
3.3 方式三:由Idea的Maven创建
利用maven就比较的常规,也相对比前两种复杂一些。首先正常的创建maven项目,在创建完成之后需要在pom中添加依赖,使得此项目成为一个SpringBoot的项目。
4 SpringBoot的项目结构
4.1 pom.xml
[注]:3.3节中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>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ghy</groupId>
<artifactId>springboothello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboothello</name>
<description>Demo project for Spring Boot</description>
<!--当前使用的jdk-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web启动器-->
<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>
<!--SpringBoot的打包插件:jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2 启动类
SpringBoot启动类的作用:启动SpringBoot项目,是基于Main方法来运行的。这个启动类在启动的时候会进行相关的注解扫描(@Controller…),扫描的位置为同包或者子包下的注解。故启动类的位置应放在包的根下。
-启动类和启动器的区别:
- 启动类表示项目的启动入口
- 启动器表示Jar包的坐标
创建启动类:(3.3节也需要启动类来启动项目)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类:必须加上SpringBootApplication
*/
@SpringBootApplication
public class SpringBootHelloApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloApplication.class,args);
}
}
4.3 启动器
只需要在项目里面引入启动器,相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景。
spring-boot-starter:核心启动器;
spring-boot-starter-web:web启动器,即导入了web模块正常运行的一些组件。
所有启动器查看地址:启动器
4.4 配置文件
SpringBoot中提供一个名为application的全局配置文件,支持properteis格式和yaml( yml)格式。一般这些配置文件放在resources目录下面,在SpringBoot启动后就解析该配置文件(核心启动器进行解析)。
.yaml文件
server:
port: 8080
4.4.1 存放位置
- 当前项目的根目录下
- 当前项目根目录下的config目录中
- 项目的resources目录下,即classpath根路径中
- 项目resources目录的config目录中
4.4.2 加载顺序
1.不同格式的加载顺序:
.properties优先于.yml。如果同一个配置属性在多个配置文件都配置了,默认使用第1个读取到的,后面读取的并不会覆盖前面读取到的。
2.不同存放位置的加载顺序:
当前项目根目录/config/ > 当前项目根目录/ > resources/config/ > resources/
一般我们把配置文件放在resources目录下。
4.4.3 配置文件中的占位符 ${}
1.占位符可以获取框架提供的方法中的值:random.int;
eg.${random.int}:随机取整型范围内的一个值
2.占位符还可以获取配置文件中键的值赋值给另一个键作为值。
server:
port: ${
random.int(8000,8888)} #随机生成8000和8888中的一个数
user:
name: ghy
name2: ${
user.name} #这个就是name2的值就是name的值
4.4.4 bootstrap配置文件(了解)
SpringBoot中有两种上下文对象,一种是 bootstrap, 另外一种是application。
bootstrap是应用程序的父上下文,即bootstrap 加载优先于applicaton的加载。 bootstrap 主要用于从额外的资源来加载配置信息,也可以在本地外部配置文件中解密属性。这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。
4.5 核心注解
@SpringBootApplication
@SpringBootConfiguration 标注该类为一个配置类
@Conofiguration 通过对bean对象的操作代替spring中xml文件
@EnableAutoConfiguration 自动配置,@AutoConfigurationPackage和@Import({AutoConfigurationImportSelector.class})的结合
@AutoConfigurationPackage 自动注入主类下所在包下所有的加了注解的类以及配置类
@Import({AutoConfigurationImportSelector.class})
@ComponentScan 组件扫描,可自动发现和装配一些bean
@ConfigurationPropertiesScan 扫描配置属性
4.6 Spring Boot在Controller中常用的注解
@RestController 相当于是@controller和@ResponseBody,使用@RestCotoller注解Controller中的方法不返回页面,返回的内容就是return里的内容(字符串)。
@GetMapping 相当于@RequestMapping(method = RequestMethod.GET)
@PostMapping
@PutMapping
@DeleteMapping
5 SpringBoot整合web层技术
5.1 整合Servlet
5.1.1 通过注解扫描完成Servlet组件的注册
1.创建Servlet:继承Servlet;重写doGet方法;添加@webServlet
/**
*通过注解扫描完成Servlet组件整合
*/
@WebServlet(name="FirstServlet",urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("通过注解扫描完成");
}
}
2.修改启动类:添加@ServletComponentScan,springboot启动时,会扫描@webServlet,并将该类实例化。
@SpringBootApplication
@ServletComponentScan //springboot启动时,会扫描@webServlet-@WebFilter-@WebListener注解,并将该类实例化
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class,args);
}
}
5.1.2 通过方法完成Servlet组件的扫描
1.创建Servlet:继承Servlet;重写doGet方法
/**
*通过方法完成Servlet组件整合
*/
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("通过方法完成");
}
}
2.创建Servlet配置类
@Configuration
public class ServletConfig {
/**
* 完成Servlet组件的注册
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
// 指定访问url
bean.addUrlMappings("/second");
return bean;
}
}
5.2 整合Filter
5.2.1 通过注解扫描完成Filter组件的注册
1.创建Filter:实现Filter接口;重写方法;添加@webFilter
/**
* 通过注解扫描完成Filter组件整合
*/
@WebFilter(filterName = "FirstFilter",urlPatterns = "/first")
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("---filter start---");
// 手动放行
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("---filter end---");
}
@Override
public void destroy() {
}
}
2.修改启动类:添加@ServletComponentScan,springboot启动时,会扫描@webFilter,并将该类实例化。
@SpringBootApplication
@ServletComponentScan //springboot启动时,会扫描@webServlet-@WebFilter-@WebListener注解,并将该类实例化
public class SpringBootWebApplication {
public static void main