1. 回顾
1.JDK8的新特性:
Stream流,对数组或集合进行操作。提供了很多的操作API方法。
日期时间安全类: LocalDate LocalTime DateTimeFormatter
JDK8新增了哪些特点:
Lambda表达式 函数式接口 方法引用 Stream流 日期时间安全类
接口中增加default和static方法。
2. 大纲–springboot框架
1. 什么是Springboot?
2. Springboot特点有哪些?
3. 快速搭建一个Springboot工程?
4. Springboot配置文件的介绍?
5. 读取springboot中配置文件的内容?
6. Springboot注册web三大组件。
7. springboot自动装配原理--->[面试]
3.什么是Springboot?
springboot可以帮你简化spring的搭建,并且快速创建一个spring的应用程序。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置
4.Springboot特点有哪些?
4.1springboot-starter场景启动器
只要引入starter,这个场景的所有常规需要的依赖springboot就会自动引入(依赖传递原则,一个依赖依赖于其他的依赖,引入这个依赖就会引入它所依赖的其他依赖)
4.2版本仲裁(依赖不写版本号的原因)
springboot中配置的starter不需要添加版本的原因为,父依赖中(spring-boot-starter-parent)声明了依赖的版本号,所以不需要添加版本号,不过可以手动声明依赖的版本号,
4.3修改依赖版本号
1、引入依赖默认都可以不写版本
2、引入非版本仲裁的jar(也就是springboot 的父依赖中没有声明的依赖的版本号),要写版本号
(1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;
(2)内嵌Tomcat或Jetty等Servlet容器;
(3)提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
(4)尽可能自动配置Spring容器;
(5)提供准备好的特性,如指标、健康检查和外部化配置;
(6)绝对没有代码生成,不需要XML配置。
5. 创建springboot工程
1.JDK必须为1.8以上
2.spring的jar必须5.0以上
3.maven必须3.3以上
创建一个controller类
配置yml文件
yml的文件格式:
YAML语法格式:
1,大小写敏感
2,使用缩进表示层级关系
3,不支持Tab键制表符缩进,只使用空格缩进
4,缩进的空格数目不重要,只要相同层级的元素左侧对齐即可,通常开头缩进两个空格
5,字符后缩进一个空格,如冒号,逗号,短横杆(-)等
6,"—“表示YAML格式,一个文件的开始,用于分隔文件间
7,”#”表示注释
8,值必须与:中间有个空格(切记不要忘记)
启动springboot工程并浏览器访问:
5.1 介绍springboot中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: 继承
如果你的maven项目想成为springboot工程必须继承SpringBoot父依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ykq</groupId>
<artifactId>qy151-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>qy151-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web的启动依赖 把spring中常见的jar都集成到该依赖中,集成了springmvc的启动配置类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意:
默认springboot扫描的包为主启动类所在的包以及子包。
5.2 聊一下springboot的配置文件
有两种格式的配置文件:
第一种: properties属性文件
# 修改springboot中tomcat端口号. server.port=8888
第二种: yml文件
server: port: 6666
不管是哪种,他们的名字必须以application开始。
如果两个配置文件同时存在,而且有些内容一样。按照properties的优先级高。如果有些不一样,两个配置文件不一样的会合并在一起。
6. 读取springboot配置文件中的内容
OSS文件上传
密钥和bucket名称等—密钥和bucket都写死在java代码中。如果后期修改密钥和bucket的值,你必须修改源码代码。 我们要写在配置文件。然后通过代码在读取配置文件中的密钥和bucket.
如何读取springboot配置文件的内容呢?
通过@PropertiesConfiguration或者@Value注解。
6.1 使用@PropertiesConfiguration
@PropertiesConfiguration该注解使用在类上。
#自定义的配置信息
student.name=ldh
student.age=15
student.hobby[0]=sing
student.hobby[1]=swing
@Data
@Component //该类对象的创建和销毁都有spring容器来管理
@ConfigurationProperties(prefix = "student") //读取springboot中的配置内容
public class Student {
private String name;
private Integer age;
private String[] hobby;
}
@Autowired //spring容器帮你注入该对象
private Student student;
@GetMapping("/index")
public Student index(){
return student;
}
6.2 使用@Value
@Value 只能放在我们的类属性上。而且它只能读取基本类型和字符串类型。
7. 配置不同的配置文件
思考: 我们在实际开发中,环境有哪些?
开发环境---->测试环境---->线上环境 由于环境的不同,那么就会有不同的配置内容。
难道我们不断的修改配置内容。----不会
实际工作中,针对不同的环境配置不同的配置文件,然后再总的配置文件中激活相应的配置文件。
7.1 配置开发配置.yml
spring:
profiles:
active: test
7.2 配置路径的前置路径
server:
servlet:
context-path: /springboot02
8. Springboot注册web三大组件。
什么是web的三个组件?
Servlet和Filter以及Linstener监听器。
为什么要注册这三个组件呢?
因为后面springboot有可能要集成第三方框架,而第三方框架的底层可能就依赖于过滤器或者servlet.
如何注册呢?
思考: 早期:
<1>Servlet类
<2>注册到Tomcat容器web.xml
<servlet> <servlet-name></servlet-name> <servlet-class>Servlet类</servlet-class> </servlet> <servlet-mapping> <servlet-name></servlet-name> </url-parterrn>/</url-parterrn> </servlet-mapping>
现在:都没有web.xml
创建一个配置类:
@Configuration //该类为配置类 xml文件 public class MyConfig { @Bean //理解为配置文件中<bean > public ServletRegistrationBean<Servlet> registrationBean(){ //创建一个Servlet注册器. ServletRegistrationBean<Servlet> registrationBean=new ServletRegistrationBean<>(); registrationBean.setName("my"); registrationBean.setServlet(new MyServlet()); registrationBean.addUrlMappings("/my"); return registrationBean; } }
以前如何注册过滤器: web.xml
现在:
@Bean public FilterRegistrationBean<Filter> filterRegistrationBean(){ FilterRegistrationBean<Filter> filterRegistrationBean=new FilterRegistrationBean<>(); filterRegistrationBean.setName("myfilter"); filterRegistrationBean.setFilter(new MyFilter()); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; }
9.springboot自动装配原理—>[面试]
9.1 springboot包扫描的原理
为什么包扫描时,扫描的为主类所在的包以及子包。
10 解决小bug
问题背景:
进行Spring Boot配置文件部署时,发出警告Spring Boot Configuration Annotation Processor not configured,但是不影响运行。
解决方案
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
问题分析:
它的意思是“Spring Boot配置注解执行器没有配置”,配置注解执行器的好处是什么。
配置注解执行器配置完成后,当执行类中已经定义了对象和该对象的字段后,在配置文件中对该类赋值时,便会非常方便的弹出提示信息。
例子如下: