SpringBoot SpringBoot 原理篇 1 自动配置 1.17 自动配置原理【3】
创始人
2024-02-21 08:27:37
0

SpringBoot

【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】

SpringBoot 原理篇

文章目录

      • SpringBoot
      • SpringBoot 原理篇
      • 1 自动配置
        • 1.17 自动配置原理【3】
          • 1.17.1 看源码了
          • 1.17.2 小结

1 自动配置

1.17 自动配置原理【3】

1.17.1 看源码了

【笔者用的2.7.4 ,感觉和老师的2.5.4 都已经差别很大了】

换个版本

在这里插入图片描述

在这里插入图片描述

OK, 前面我们已经说到

它在这个文件中默认加载了很多的自动配置的功能

在这里插入图片描述

比如说这儿的第一个就是它默认要加载的一个项

找出来看看这个配置的代码

在这里插入图片描述

现在在我们猫和老鼠的案例中 ,加入redis

没加之前的运行效果

package com.dingjiaxiong;import com.dingjiaxiong.bean.CartoonCatAndMouse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Indexed;/*** ClassName: App* date: 2022/10/25 13:03** @author DingJiaxiong*/@SpringBootApplication//@SpringBootConfiguration
//        @Configuration
//                @Component
//        @Indexed
//@EnableAutoConfiguration
//        @AutoConfigurationPackage
//                @Import({AutoConfigurationPackages.Registrar.class})
//        @Import({AutoConfigurationImportSelector.class})
//@ComponentScan(excludeFilters = {
//                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}),
//                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class}
//        )}//@Import({AutoConfigurationPackages.Registrar.class})
//@Import({AutoConfigurationImportSelector.class})@Import(CartoonCatAndMouse.class)
public class App {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(App.class);String[] names = context.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}CartoonCatAndMouse bean = context.getBean(CartoonCatAndMouse.class);bean.play();}}

运行结果

在这里插入图片描述

这一堆bean ,是没有redis的,现在我加个依赖

org.springframework.bootspring-boot-starter-data-redis2.7.1

在这里插入图片描述

OK,现在再试一次

在这里插入图片描述

效果超级明显,这就说明它已经加载了所有与redis 有关的bean 了

在这里插入图片描述

就是这句话生效了。

在这里插入图片描述

再看看这个,好家伙,和我们之前的案例有原理相像啊

点进去看看

在这里插入图片描述

嗯,它也没说它是个bean

在这里插入图片描述

我的这个也没说,看看它的默认值

在这里插入图片描述

这也是我们不配就能用的原因

在这里插入图片描述

接着后面它又导入了两组客户端的实现【这就是一个对象里面包另一个对象了,想想我们写配置的时候】

再接着看

在这里插入图片描述

如果我没有找到redisTemplate 这样一个bean,我就给你一个,如果你自己定义了,那我就不加载了,因为它怕加载重了

太细了

下面的StringRedisTemplate 也是一样的道理

在这里插入图片描述

OK,

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

1.17.2 小结
  1. 先开发若干种技术的标准实现
  2. SpringBoot启动时加载所有的技术实现对应的自动配置类
  3. 检测每个配置类的加载条件是否满足并进行对应的初始化
  4. 切记是先加载所有的外部资源,然后根据外部资源进行条件比对

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...