Spring-Boot入门(雷丰阳)

spring启动原理分析

一、spring-Boot启动依赖

1、pom文件分析

1️⃣ maven插件

<!-- 这个插件,可以将应用打包成一个可执行的jar包-->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

将这个应用打成jar包,直接使用java -jar的命令进行执行;
为什么java -jar 能运行jar文件
java命令 : java -jar 和 java -cp

2️⃣ Spring-Boot父项目

    <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	他的父项目
	 <parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-dependencies</artifactId>
	    <version>2.3.1.RELEASE</version>
	  </parent>
	他来真正管理Spring Boot应用里面所有依赖版本;	  

Spring Boot的版本仲裁中心;
以后我们导入依赖默认是不需要写版本;(没有在dependencies里面管理的依赖自然需要申明版本号)

3️⃣ spring-boot-starter启动器

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

spring-boot-starter-web
spring-boot-starter:spring-boot场景启动器;帮助我们导入web模块正常运行所依赖的组件;
Spring Boot将所有的功能场景都提取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景所有依赖都会倒入进来。要用什么功能就导入什么场景的启动器。

2、主程序类,主入口类

/**
 * @SpringBootApplication 来标注一个主程序类,说明这是一个spring Boot应用
 */
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		//Spring 应用启动起来
		SpringApplication.run(DemoApplication.class, args);
	}
}

@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

@SpringBootConfiguration:spring Boot的配置类;
标注在某个类上,表示这是一个Spring Boot的配置类;
@Configuration:配置类上来标注这个注解;
配置类-------配置文件;配置类也是容器中的一个组件;@Component

@EnableAutoConfiguration:开启自动配置功能;
以前我们需要配置的东西,Spring Boot帮我们自动配置;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生效;

@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

***************************************************************

@Import({Registrar.class})
public @interface AutoConfigurationPackage {

@AutoConfigurationPackage:自动配置包
@Import({AutoConfigurationPackages.Registrar.class});
Spring的底层注解@Import,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class

将主配置类(@SpringBootApplication)的类所在包及下面所有的子包里面的组件扫描到Spring容器;

@Import({AutoConfigurationImportSelector.class});
给容器中导入组件?
AutoConfigurationImportSelector:导入那些组件的选择器;
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
会给容器导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;
在这里插入图片描述
有了自动配置类,免去了我们手动编写配置注入功能组件等工作;
List configurations = SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, beanClassLoader);
Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮助我们进行自动配置工作;以前我们需要自己配置的东西,自动配置类都帮我们;
J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-2.3.0.RELEASE.jar

3、使用Spring Initializer快速创建Spring Boot项目

默认生成的Spring Boot项目;

  • 主程序已经生成好了,我们只需要我们自己的逻辑
  • resouces文件夹中目录结构
    • static:保存所有的静态资源;js css images;
    • templates:保存所有的模板页面;(Spring Boot默认使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
    • application.properties:Spring Boot应用配置文件;可以修改一些默认值;

二、spring-Boot配置文件

1、配置文件

SpringBoot使用一个全局的配置文件,配置名称是固定的;

  • application.properties
  • application.yml(.yaml)

配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;
标记语言:
以前的配置文件,打多都是使用xxx.xml文件;
YAML:以 数据为中心,比json、xml更加适合做配置文件
YAML:配置例子

server:
  port: 8081

XML:

<server>
<port>8081<port>
</server>

2、YAML语法:

1️⃣ 基本语法

k:(空格)v :表示一对键值对(空格必须有);
空格的缩减来控制层级关系;只要是左对齐的一列数据,都是同一个层级

server:
  port: 8081
  path: /hello

属性和值大小写敏感;

2️⃣ 值的写法

字面量:普通的值(数字,字符串,布尔)
name: "zhangsan \n lisi"
************************
name: 'zhangsan \n lisi'

k: v:字面直接写;
  字符串默认不用上单引号或者双引号;
 " ":双引号;不会转义字符串里面的特殊字符;特殊字符作为本身想表示的意思;输出:zhangsan 换行 lisi
 ’ ':单引号;会转义字符串里面的特殊字符;特殊字符最终只是一个普通的字符串数据;输出:zhangsan \n lisi

对象、Map(属性和值)(键值对);

k: v 在下一行写对象的属性和值关系;注意缩进

friends:
	lastName: zhangsan
	age: 20

行内写法:

friends: {lastName: zhangsan,age: 18}
数组(List、Set);

用- 值表示数组中的一个元素

pet:
 - cat
 - dog
 - pig

行内写法

pet: [cat,dog,pig]

3、 配置文件值注入

1️⃣properties配置文件在idea中默认utf-8可能会乱码

在这里插入图片描述

2️⃣ @Value获取值和@ConfigurationProperties获取值比较

@ConfigurationProperties@Value
功能批量注入配置文件中的属性一个一个指定
松散绑定(松散语法)支持不支持
SpEL不支持支持
JSR303数据校验支持不支持

配置文件properties还是yaml他们都能获取到值;
如果说,我们只在业务逻辑中获取一下配置文件的某项值,使用@Value

 * @Controller
 * @ResponseBody (如果是对象转为json数据)
 * 上面两个相加等同于 @RestController
 */
@RestController
//@Controller
public class HelloworldController {

    @Value( "${person.last-name}" )
    private String name;

    @RequestMapping("/hello")
//    @ResponseBody //@ResponseBody的作用其实是
//	  将java对象转为json格式的数据。
    public String hello(){
        return "helloworld, "+name;
    }
}

如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就可以直接使用@ConfigurationProperties

3️⃣ 配置文件注入数据校验

@Component
@ConfigurationProperties(prefix = "person")
@Data
@Validated
public class Person {

//    在@ConfigurationProperties下lastName必须是邮箱格式
    @Email
    private String  lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String,Object> maps;
    private List<Object>       lists;
    private Dog                dog;

}

@Validated, @Email

4️⃣ @PropertySource&@ImportResource

@PropertySource: 加载指定的配置文件;

/**
 * @author Created by huyh
 * @create on 2020/6/19.
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties 告诉SpringBoot将本类中的所有属性和配置文件中相关的属性进行配置绑定;
 * prefix = "person" 配置文件中那个下面的所有属性进行一一映射
 *
 * 只有这个组件是容器中的组件(@Component),才能容器提供的@ConfigurationProperties功能;
 * @ConfigurationProperties(prefix = "person")默认从全局配置文件周年获取值;
 *
 *
 */
@PropertySource( value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@Data
//@Validated
public class Person {
    /**
     * <bean class="Person" >
     *  <property name="lastName" value="字面量/${key}从环境变量、配置文		 	件中获取/#{SpEL}" ></property>
     * </bean>
     */
//    在@ConfigurationProperties下lastName必须是邮箱格式
//    @Email
//    @Value( "${person.last-name}" )
    private String  lastName;
    //    @Value( "#{1*2}" )
    private Integer age;
    //    @Value( "true" )
    private Boolean boss;
    //    @Value( "2020/6/19" )
    private Date birth;
    
    private Map<String,Object> maps;
    private List<Object>       lists;
    private Dog                dog;
}

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,不会自动识别;想让Spring配置文件生效,加载进来;@ImportResource标注在一个配置类上

@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效

不来编写Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="com.example.demo.service.HelloService"></bean>
</beans>

SpringBoot推荐给容器中添加组件的方式,推荐使用全注解方式;
1、配置类====Spring配置文件
2、使用@Bean给容器中添加组件

/**
 * @author Created by huyh
 * @create on 2020/6/19.
 *
 * @Configuration 指明当前类是一个配置类;就是来替代之前的spring配置文件
 *
 * 在配置文件中用<bean></bean>标签添加组件
 */

@Configuration
@Component
public class MyAppConfig {
//    将方法的返回值添加到容器中;容器中这个组件默认的Id就是方法名
    @Bean
    public HelloService helloService(){
        System.out.println("@Bean给容器中添加组件了");
        return new HelloService();
    }
}

4、配置文件占位符

1、随机数

${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,66665]}

2、占位符获取之前配置的值,如果没有可以是用:指定默认值

person.last-name=张三${random.uuid}
#person.age=19
person.age=${random.int}
person.birth=2020/6/19
person.boss=false
person.dog.name=${person.hello:hello}_小狗
person.dog.age=2

5、Profile

1️⃣ 多Profile文件

我们在主配置文件编写的时候,文件名可以是application-{proifile}.properties/yml
默认使用application.properties的配置;

2️⃣ yml支持多文档块方式

server:
  port: 8081
#spring:
#  profiles:
#    active: dev
---
server:
  port: 8082
spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: prod #指定配置那个环境
---

3️⃣ 激活指定profile

1、在配置文件中指定 spring.profiles.active=dev
2、命令行:
java -jar spring-demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
可以直接在测试的时候,配合传入命令行参数
在这里插入图片描述
3、虚拟机参数;
-Dspring.profiles.active=dev
在这里插入图片描述

6、配置文件加载位置

springboot启动会扫描以下位置的application.properties或application.yml文件作为Spring Boot的默认配置文件
-file:./config/
-file:./
-classpath:/config/
-classpath:/
优先级由高到底,高优先级的配置会覆盖低优先级的配置;
SpringBoot会从这四个位置全部加载主配置文件;互不配置
我们还可以通过spring.config.location来改变默认的配置文件位置
项目打包以后,我们可以使用命令参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=D:/application.properties --server.servlet.context-path=/abc

7、外部配置加载顺序

SpringBoot也可以从以下位置加载顺序;优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置
1.命令行参数
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.servlet.context-path=/abc
多个配置用空格分开;–配置项=值
2.来自java:comp/env的JNDI属性
3.Java系统属性(System.getProperties())
4.操作系统环境变量
5.RandomValuePropertySource配置的random.*属性值

由jar包外向jar包内进行寻找;
优先加载带profile

6.jar包外部的application-{profile}.properties或application.yml{带spring.profile}配置文件
7.jar包内部的application-{profile}.properties或application.yml{带spring.profile}配置文件

再来加载不带profile
8.jar包外部的application.properties或application.yml{不带spring.profile}
9.jar内外部的application.properties或application.yml{不带spring.profile}

10.@Configuration注解上的@PropertySource
11.通过SpringApplication.setDefaultProperties指定的默认属性
所有支持的配置加载来源;
参考官方文档

8、自动配置原理

配置文件到底能写什么?怎么写?自动配置原理;
配置文件能配置的属性参照

1️⃣ 自动配置原理:

1)、SpringBoot启动的时候加载主配置类,开启自动配置功能==@EnableAutoConfiguration==
2)、@EnableAutoConfiguration作用

  • 利用AutoConfigurationImportSelector给容器中导入一些组件?
  • 可以利用selectImports()方法的内容;
  • List configurations = getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置
SpringFactoriesLoader.loadFactoryNames();
扫描所有jar包类路径下,META-INF/spring.factories
把扫描的这些文件的内容包装成properties
从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们加入到容器中
 EnableAutoConfiguration.class

将类路径下META-INF/spring.factories里面配置的所有EnableAutoConfiguration的值加入到容器中;

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration

每个这样的xxxAutoConfiguration类都是容器中的一个组件,都加入到容器中;用他们来做自动配置;
3)、每个一个自动配置类进行自动配置功能;
4)、以HttpEncodingAutoConfiguration为例解释自动配置原理;

@Configuration(proxyBeanMethods = false)//表示这是一个配置类,以前编写的配置文件一样,也可以给容器中添加组件
@EnableConfigurationProperties(ServerProperties.class)//启动指定的类@ConfigurationProperties功能;将配置文件中对应的值和ServerProperties绑定起来;并把ServerProperties加入到Ioc容器中
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)//Spring底层@Conditional注解(Spring注解版),根据不同的条件,如果满足指定的条件,整个配置类里面就会生效;判断当前应用是否是web应用,如果是,当前配置类生效
@ConditionalOnClass(CharacterEncodingFilter.class)//判断当前项目有没有这个类 CharacterEncodingFilter;SpringMVC中解决乱码的过滤器;
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)
//判断配置文件中是否存在某个配置 server.servlet.encoding.enabled;如果不存在,判断也是成立的;
//即使我们配置文件中不配置server.servlet.encoding.enabled=true,也是默认生效的;
public class HttpEncodingAutoConfiguration {
	//他已经和Springboot的配置文件映射了
	private final Encoding properties;
	//只有一个有参构造器的情况下,参数的值会从容器中拿
	public HttpEncodingAutoConfiguration(ServerProperties properties) {
		this.properties = properties.getServlet().getEncoding();
	}

	@Bean //给容器中添加一个组件,这个组件的某些值需要从properties中获取
	@ConditionalOnMissingBean //判断容器中没有这个组件
	public CharacterEncodingFilter characterEncodingFilter() {
		CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
		filter.setEncoding(this.properties.getCharset().name());
		filter.setForceRequestEncoding(this.properties.shouldForce(Encoding.Type.REQUEST));
		filter.setForceResponseEncoding(this.properties.shouldForce(Encoding.Type.RESPONSE));
		return filter;
	}

根据当前不同的条件判断,决定这个配置类是否生效?
一旦这个配置类生效;这个配置类就会给容器中添加各种组件;这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的;

5)、所有在配置文件中能配置的属性都是xxxProperties类中封闭着;配置文件能配置什么就可以参照某个功能对应的这个属性类

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)//从配置文件中获取指定的值和bean的属性进行绑定
public class ServerProperties {

精髓:
1)、SpringBoot启动会加载大量的自动配置类
2)、我们看我们需要的功能有没有SpringBoot默认写好的自动配置类;
3)、我们再来看这个自动配置类中到底配置了那些组件;(只要我们要用的组件有,我们就不需要再来配置了)
4)、给容器自动配置类添加组件的时候,会从properties类中获取某些属性,我们就可以在配置文件中指定这些属性的值;

xxxxAutoConfiguration:自动配置类;
给容器添加组件
xxxxProperties:封装配置文件中相关属性;

2️⃣ 细节

1、@Conditional派生注解(Spring注解版原生的@Conditional作用)

作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配置里面的所有内容才生效;

@Conditional扩展注解作用(判断是否满足当前指定条件)
@ConditionalOnJava系统的java版本是否符合要求
@ConditionalOnBean容器中存在指定Bean
@ConditionalOnMissingBean容器中不存在指定Bean
@ConditionalOnMissingBean容器中不存在指定Bean;
@ConditionalOnExpression满足SpEL表达式指定
@ConditionalOnClass系统中有指定的类
@ConditionalOnMissingClass系统中没有指定的类
@ConditionalOnSingleCandidate容器中只有一个指定的Bean,或者这个Bean是首选Bean
@ConditionalOnProperty系统中指定的属性是否有指定的值
@ConditionalOnResource类路径下是否存在指定资源文件
@ConditionalOnWebApplication当前是web环境
@ConditionalOnNotWebApplication当前不是web环境
@ConditionalOnJndiJNDI存在指定项

自动配置类必须在一定的条件下才能生效;
我们怎么知道那些配置类生效;
我们可以通过启用debug=true属性;来让控制台打印自动配置报告;这样我们就可以很方便的知道那些自动配置类生效;

============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:(自动配置类启动的)
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   AopAutoConfiguration.ClassProxyingConfiguration matched:
      - @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)

   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
      - found 'session' scope (OnWebApplicationCondition)

Negative matches:(没有启动的,没有匹配成功的自动配置类)
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   AopAutoConfiguration.AspectJAutoProxyingConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

三、日志

1、日志框架

小张;开发一个大型系统;
1、System.out.println("");将关键数据打印在控制台;去掉?写在一个文件?
2、框架来记录系统的一些运行时信息;日志框架;zhanglogging.jar;
3、高大上的几个功能?异步模式?自动归档?xxxx?zhanglogging-good.jar;
4、将以前框架卸下来?换上新的框架,重新修改之前🇭🇰的API;zhanglogging-prefect.jar
5、JDBC ---- 数据库驱动;
写了一个的接口层;日志门面(日志的一个抽象层);logging-abstract.jar;给项目中导入具体的日志实现就行了;我们之前的日志框架都是实现的抽象层;

市面上的日志框架;
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j…

日志门面(日志的抽象层)日志实现
JCL(Jakarta Commons Logging)SLF4j(Simple Logging Facade for Java)jboss-loggingLog4j JUL(java.util.logging) Log4j2 Logback

左边选一个门面(抽象层)、右边来选择一个实现;
日志门面:SLF4j
日志实现:Logback;

SpringBoot:底层是Spring框架,Spring框架默认是用JCL;
SpringBoot选用:SLF4j和Logback;

2、SLF4j使用

1、如何在系统中使用SLF4j

以后开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法;给系统里面导入slf4j的jar和logback的实现jar

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

图示:
在这里插入图片描述
每一个日志的实现框架都有自己的配置文件。使用slf4j以后,*配置文件还是做成日志实现框架自己本身的配置文件;

2、遗留问题

在这里插入图片描述
如何让系统中所有的日志都统一到slf4j;
1、将系统中其他日志先排除出去;
2、用中间包来替换原有的日志框架;
3、我们导入slf4j其他的实现;

3、SpringBoot日志关系

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
      </dependency>

SpringBoot使用它来做日志功能;

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>

底层依赖关系
在这里插入图片描述
总结:
1)、SpringBoot底层也是使用slf4j+logback的方式进行日志记录
2)、SpringBoot也把其他的日志都替换成了slf4j;
3)、中间替换包?

在这里插入代码片

4)、如果我们要引入其他框架?一定要把这个框架的默认日志依赖移除掉?
Spring框架用的是commons-logging;

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <exclusions>
      	<exclusion>
      	   <groupId>commons-logging</groupId>
    	  <artifactId>commons-logging</artifactId>
      	</exclusion>
      </exclusions>
 </dependency>

SpringBoot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉;

4、日志使用

1、默认配置
	Logger logger=LoggerFactory.getLogger(getClass());
	
	//日志的级别;
	//由低到高 trace<debug<info<warn<error
	//可以调整输出的日志级别;日志就只会在这个级别及以后的高级别生效	
	@Test
	public void contextLoads() {
		logger.trace("这个是teace日志");
		logger.debug("这个是debug日志");
//		SpringBoot默认给我们使用的是Info级别的,没有指定级别的就用SpringBoot默认规定的级别;root级别
		logger.info("这个是info日志");
		logger.warn("这个是warn日志");
		logger.error("这个是error日志");
	}
日志输出格式:
%d表示日期时间,
%thread表示线程名
%-5level:级别从左显示5个字符宽度
%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
%msg :日志消息,
%n是换行符
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n

SpringBoot修改日志的默认配置

#SpringBootdebug模式
#debug=true
logging.level.com.example=trace

#logging.file.path=
#不指定路径在当前项目下生成springboot.log日志
#可以指定完整的路径;
#logging.file.name=H:/springboot.log
#logging.file.name=springboot.log

logging.file.path=/spring/log

#在控制台输出的日志的格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} -%msg%n

logging.pattern.file=%d{yyyy-MM-dd} === [%thread] %-5level === %logger{50} -%msg%n

logback.xml

logging.file.namelogging.file.pathExampleDescription
(none)(none)只在控制台输出
指定文件名(none)my.log输出日志到my.log文件
(none)指定目录/var/log输出到指定目录的spring.log文件中
2、指定配置

给类路径下放上每个日志框架自己的配置文件即可;SpringBoot就不使用他默认配置的了
Depending on your logging system, the following files are loaded:

Logging SystemCustomization
Logbacklogback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
Log4j2log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging)logging.properties

如果可能,我们建议您使用-spring日志配置的变体(例如,logback-spring.xml而不是logback.xml)。如果使用标准配置位置,Spring无法完全控制日志初始化
logback.xml:直接就被日志框架识别了;
logback-spring.xml:日志框架就不直接加载日志的配置项,由SpringBoot解析日志配置,可以使用SpringBoot的高级Profile功能

 <springProfile name="staging">
 <!-- configuration to be enabled when the "staging" profile is active-->
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] %-5level %logger{50} - %msg%n</pattern>
 </springProfile>

否则

no applicable action for [springProfile]
<!--控制台输出appender-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!--设置输出格式-->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
      <!--       <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> -->
      <springProfile name="!dev">
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} +=+ [%thread] %-5level %logger{50} - %msg%n</pattern>
      </springProfile>
       <springProfile name="dev">
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] %-5level %logger{50} - %msg%n</pattern>
      </springProfile>
            <!--设置编码-->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

5、切换日志框架

可以按照slf4j的日志适配图,进行相关的切换;
slf4j+log4j的方式;

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>

切换为log4j2

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

四、Web开发

1、简介

使用SpringBoot;
1)、创建SpringBoot应用,选中我们需要的模块;
2)、SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来
3)、自己编写业务代码;

自动配置原理?
这个场景SpringBoot帮我们配置了什么?都不能修改?能修改哪些配置?能不能扩展?xxx

XXXXAutoConfigruation:帮我们给容器自动配置组件;
xxxxProperties:配置类来封装配置文件的内容;

2、SpringBoot对静态资源的映射规则;

*/
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {

	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
			"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
			//可以设置和静态资源有关的参数,缓存时间等
@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
			CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
			if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			if (!registry.hasMappingForPattern(staticPathPattern)) {
				customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
						.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
		}
		
		//配置欢迎页映射
		@Bean
		public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
				FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
			WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
					new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
					this.mvcProperties.getStaticPathPattern());
			welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
			welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
			return welcomePageHandlerMapping;
		}



1)、所有/webjars/**,都会去classpath:/META-INF/resources/webjars/找资源
添加链接描述

在这里插入图片描述
localhost:8080/webjars/jquery/3.5.1/jquery.js
2)、“/**” 访问当前项目路径的任何资源,(静态资源文件夹)

"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"
"/":当前项目的根路径

localhost:8080/abc 去静态资源文件夹下里面找abc
3)、欢迎页;静态资源文件夹·下面的所有index.html页面;被"/**"映射;
localhost:8080

	private Optional<Resource> getWelcomePage() {
			String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
			return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
		}

4)、所有的**/favicon.ico都是在静态文件下找;

3、模板引擎

JSP、Velocity、FreeMaker、Thymeleaf;
在这里插入图片描述
SpringBoot推荐的Thymeleaf;
语法更简单,功能更强大;

1、引入Thymeleaf
<dependency>
    <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、Thymeleaf使用&语法
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

	public static final String DEFAULT_PREFIX = "classpath:/templates/";

	public static final String DEFAULT_SUFFIX = ".html";
	//只要我们吧Html页面放在classpath:/templates/,thymeleaf就能自动渲染;

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;
使用:
1、导入thymeleaf命名空间

<html xmlns:th="http://www.thymeleaf.org">

2、使用thymeleaf语法,

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
success
<h1>成功!</h1>
<h1>[[${hello}]]</h1>
<!--th:text 将div里面的文本内容设置为-->
<div th:text="${hello}">这里显示欢迎信息</div>
</body>
</html>
3、语法规则

1)、th:text;改变当前元素里面的文本内容;
th:任意html属性;
在这里插入图片描述
2)、表达式

Simple expressions:(表达式语法)
		 Variable Expressions: ${...} :获取变量值;OGNL;
		 						1)、获取对象的属性
		 						2)、使用内置的基本对象
						#ctx : the context object. #vars: the context variables. #locale : the context locale.
						#request : (only in Web Contexts) the HttpServletRequest object.
						#response : (only in Web Contexts) the HttpServletResponse object. 
						#session : (only in Web Contexts) the HttpSession object. #servletContext : (only in 		   Web Contexts) the ServletContext object.
		
		 Selection Variable Expressions: *{...} :选择表达式;和${}在功能上是一样;补充配合 <div th:object="${session.user}">
 <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
 <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
 <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p> 
</div>
			
		 Message Expressions: #{...}  获取国际化内容
		 Link URL Expressions: @{...} 定义URL;
		 Fragment Expressions: ~{...} 判断引入表达式
		 ${session.foo}
		 						3)、内置的一些工具对象
Literals(字面量)
 Text literals: 'one text' , 'Another one!' ,… 
 Number literals: 0 , 34 , 3.0 , 12.3 ,…
 Boolean literals: true , false 
 Null literal: null
  Literal tokens: one , sometext , main ,… 
  Text operations: (文本操作)
  String concatenation: +
   Literal substitutions: |The name is ${name}|
   Arithmetic operations: (数学运算)
   Binary operators: + , - , * , / , % 
   Minus sign (unary operator): -
 Boolean operations:(布尔运算)
  Binary operators: and , or
   Boolean negation (unary operator): ! , not 
Comparisons and equality:(比较运算)
   Comparators: > , < , >= , <= ( gt , lt , ge , le ) 
   Equality operators: == , != ( eq , ne ) 
Conditional operators: (条件运算)
	If-then: (if) ? (then) 
	If-then-else: (if) ? (then) : (else) 
	Default: (value) ?: (defaultvalue)
Special tokens:(特殊操作)
	No-Operation: _ 
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
 <a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>

<div th:insert="~{commons :: main}">...</div>

4、SpringMVC自动配置

spring自动配置

7.1.1. Spring MVC Auto-configuration

1)、SpringMVC auto Configruation

Spring Boot 自动配置好了SpringMVC
以下是SpringBoot对SpringMVC的默认配置;

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
    • 自动配置 了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View)视图对象决定如何渲染(转发?重定向?))
    • ContentNegotiatingViewResolver:组合所有的视图解析器;
    • 如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来
  • Support for serving static resources, including support for WebJars (covered later in this document)).(静态属性)
  • Automatic registration of Converter, GenericConverter, and Formatter beans.
    • Converter:转换器;public String hello(User user);类型转换使用
    • Formatter:格式化器;2020-06-28===Date;
		@Bean
		@Override
		public FormattingConversionService mvcConversionService() {
			Format format = this.mvcProperties.getFormat();
			WebConversionService conversionService = new WebConversionService(new DateTimeFormatters()
					.dateFormat(format.getDate()).timeFormat(format.getTime()).dateTimeFormat(format.getDateTime()));
			addFormatters(conversionService);
			return conversionService;
		}

自己添加的格式化转换器,我们只需要放在容器中即可

 `
  • Support for HttpMessageConverters (covered later in this document).
    • HttpMessageConverters:SpringMVC用来转换Http请求和响应的;User—json;
    • HttpMessageConverters:是从容器中确定;获取所有的HttpMessageConverters;
      自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(@Bean,@Component)
  • Automatic registration of MessageCodesResolver (covered later in this document).定义错误代码生成规则
  • Static index.html support.
  • Custom Favicon support (covered later in this document).
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).
    我们可以配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器)
初始化WebDataBinder;
请求数据=====JavaBean;

org.springframework.boot.test.autoconfigure.web:web的所有自动场景

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configurationclass of type WebMvcConfigurer but without @EnableWebMvc.
If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

2)、扩展SpringMVC
<mvc:view-controller path="/hello" view-name="success"/>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/hello"/>
            <bean></bean>
        </mvc:interceptor>
    </mvc:interceptors>

编写一个配置类(@Configruation),是WebMvcConfigurer类型,不能标注@EnableWebMvc;

//使用WebMvcConfigurer可以来扩展SpringMVC的功能
//WebMvcConfigurerAdapter因为1.8接口的默认方法特性被弃用
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        super.addViewControllers(registry);
        registry.addViewController( "/atguigu" ).setViewName( "success" );
    }
}

原理:
1)、WebMvcAutoConfiguration是SpringMVC的自动配置类
2)、在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class)

	/**
	 * Configuration equivalent to {@code @EnableWebMvc}.
	 */
	@Configuration(proxyBeanMethods = false)
	public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
    private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

    public DelegatingWebMvcConfiguration() {
    }

    @Autowired(required = false)
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }

//一个参考实现;将所有的WebMvcConfigurer相关配置都来一起调用;
// public void addWebMvcConfigurers(List<WebMvcConfigurer> configurers) {
//      if (!CollectionUtils.isEmpty(configurers)) {
//            this.delegates.addAll(configurers);
//        }
//    }	

    }

3)、容器中所有的WebMvcConfigurer都来一起起做用;
4)、我们的配置类也会被调用;
效果:SpringMVC的自动配置和我们的扩展配置都会起作用;所有的SpringMVC的自动配置都失效;
我们需要在配置类中添加@EnableWebMvc即可;

//使用WebMvcConfigurer可以来扩展SpringMVC的功能
//WebMvcConfigurerAdapter过期因为1.8接口的特性默认方法特性被弃用
@Configuration
@EnableWebMvc
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        super.addViewControllers(registry);
        registry.addViewController( "/atguigu" ).setViewName( "success" );
    }
}

原理:
为什么@EnableWebMvc自动配置就失效了;
1)、@EnableWebMvc的核心

@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}

2)、

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3)、

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
//容器中没有这个组件的时候,这个自动配置类才生效
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

4)、@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;
5)、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;

5、如何修改SpringBoot的默认配置

模式:

1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以多个(ViewResolver)将用户配置的和自己的默认的组合起来;
2)、在SpringBoot中会有非常多的xxxxConfigurer帮助我们配置;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值