反射在spring boot自动配置的应用
目录
一,背景
二,知识回顾
2.1 理解使用反射技术,读取配置文件创建目标对象(成员变量,方法,构造方法等)
三,springboot自动配置
3.1 反射在自动配置中的工作流程
3.2 浏览源码,逐步浏览spring boot自动配置
四, 理解 starter
4.1 自定义starter
一,背景
一直以来,我们在完成spring boot项目中,一直遵循 固定的模式:
1,向pom文件引入依赖(starter)
2 ,使用依赖 的注解
3 ,观察引入的注解,是否报错,如果没有报错,编译运行观察结果
4 如果报错结合 自己所学知识+Ai辅助
至于这个注解,是怎么生效的,何时起作用,我们并没有关注,但实际上spring boot已经帮我们做好了。
今天,我就要和大家一起,深挖在spring boot 中,我们使用的注解 是怎么起作用的?----------------spring boot自动配置
二,知识回顾
在学习spring boot自动配置 之前,我们先要复习以下:通过配置文件动态创建对象
2.1 理解使用反射技术,读取配置文件创建目标对象(成员变量,方法,构造方法等)
配置文件+反射技术 应用场景:
实际开发中,当我们项目部署到服务器,一般情况下,不允许重新部署,但我们想要开发新的功能,可以使用配置文件+反射相结合完成新功能的添加与修改。
案例
背景:原本创建 🐺,🐏 对象实现动物接口,并在测试类中运行方法。但在运行后,才发现:还没有创建 狼对象,于是使用反射+配置文件读取的方式,实现对狼的创建
util工具类 :读取 配置文件内容
package com.it.heima;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class util {public static String readProperties() {Properties properties = new Properties();String path = null;try(// 获取配置文件流InputStream resourceAsStream = test.class.getClassLoader().getResourceAsStream("data.properties")){properties.load(resourceAsStream);path = properties.getProperty("wolf");
// System.out.println(path);} catch (IOException e) {e.printStackTrace();}return path;}
}
data.properties配置文件
test测试类
package com.it.heima;import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;public class test {public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {Sheep sheep = new Sheep();sheep.run();sheep.eat();sheep.sleep();System.out.println("--------------------------");String path = util.readProperties();Wolf wolf = (Wolf)Class.forName(path).newInstance();wolf.run();wolf.eat();}
}
测试结果:
绵羊跑跑~~~~~~~~
绵羊吃吃~~~~~~~~
绵羊睡觉~~~~~~~~
--------------------------
com.it.heima.Wolf
狼跑~~~~~~~~~~~
狼吃羊~~~~~~~
"上面这个案例展示了反射的基本用法 —— 通过配置文件动态创建对象。而 Spring Boot 的自动配置,本质上就是把这个过程标准化、规模化:框架通过反射读取类路径、注解和配置文件,自动为我们创建和配置各种 Bean,这就是我们接下来要深入探讨的内容。"
三,springboot自动配置
3.1 反射在自动配置中的工作流程
1,类路径扫描:Spring Boot 会扫描类路径下的所有类,找出带有特定注解的类。@compoentscan注解
2,条件评估:通过反射检查类、方法或者字段的存在情况,以此确定是否应用某项配置。@Condition衍生注解+@Bean
3,实例化 Bean:利用反射调用构造函数或者工厂方法来创建 Bean 实例。
4,依赖注入:借助反射设置 Bean 的字段值或者调用 setter 方法。
案例
@Configuration
@ConditionalOnClass(RestTemplate.class) // 反射检查类是否存在
@EnableConfigurationProperties(MyAutoConfigProperties.class)
public class MyAutoConfiguration {
private final MyAutoConfigProperties properties;public MyAutoConfiguration(MyAutoConfigProperties properties) {this.properties = properties;
}@Bean
@ConditionalOnMissingBean // 反射检查Bean是否已存在
public RestTemplate restTemplate() {RestTemplate template = new RestTemplate();// 基于配置属性进行定制if (properties.isLoggingEnabled()) {template.getInterceptors().add(new LoggingInterceptor());}return template;
}
3.2 浏览源码,逐步浏览spring boot自动配置
这里,我将拿web起步依赖举例:
以下图片是我截取 来自博客链接 :学习springboot 的自动配置原理_spring-boot-autoconfigure-CSDN博客
核心:看到 selectImport方法
11进入DispatcherServletAutoConfiguration 自动配置类
关于条件注解的详情可以浏览 :学习springboot-条件化配置@Conditional(条件注解)_springboot条件化配置-CSDN博客
四, 理解 starter
starter:一组功能模块的坐标,当我们引用坐标时,可以使用它提供的功能
starter本质:接口+接口实现类/接口实现类
4.1 自定义starter
可以结合:springboot学习(自定义starter)_springboot3.4.3 引入 定义 springboot starter-CSDN博客