SpringBoot自动配置--原理探究

news2024/9/21 22:33:28

什么是自动配置?

SpringBoot自动配置是指在SpringBoot应用启动时,可以把一些配置类自动注入到Spring的IOC容器中,项目运行时可以直接使用这些配置类的属性。简单来说就是用注解来对一些常规的配置做默认配置,简化xml配置内容,使项目快速运行。

一.Condition

Condition 是在Spring 4.0 增加的条件判断功能,通过这个可以功能可以实现选择性的创建 Bean 操作。

案例:需求1

在 Spring 的 IOC 容器中有一个 User 的 Bean,现要求:导入Jedis坐标后,加载该Bean,没导入,则不加载。

实现步骤:

① 定义条件类:自定义类实现Condition接口,重写 matches 方法,在 matches 方法中进行逻辑判断,返回 boolean值 。 matches 方法两个参数:

• context:上下文对象,可以获取属性值,获取类加载器,获取BeanFactory等。

• metadata:元数据对象,用于获取注解属性。

② 判断条件: 在初始化Bean时,使用 @Conditional(条件类.class)注解

SpringBoot 提供的常用条件注解:

一下注解在springBoot-autoconfigure的condition包下

ConditionalOnProperty:判断配置文件中是否有对应属性和值才初始化Bean ConditionalOnClass:判断环境中是否有对应字节码文件才初始化Bean ConditionalOnMissingBean:判断环境中没有对应Bean才初始化Bean ConditionalOnBean:判断环境中有对应Bean才初始化Bean

代码实现如下: 

public class ClassCondition implements Condition {
    
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //1.需求: 导入Jedis坐标后创建Bean
        //思路:判断redis.clients.jedis.Jedis.class文件是否存在

        boolean flag = true;
        try {
            Class<?> cls = Class.forName("redis.clients.jedis.Jedis");
        } catch (ClassNotFoundException e) {
            flag = false;
        }
        return flag;

    }
}

配置类:

@Configuration
public class UserConfig {
    //@Conditional中的ClassCondition.class的matches方法,返回true执行以下代码,否则反之
    @Bean
    @Conditional(value= ClassCondition.class)
    public User user(){
        return new User();
    }
}

 测试:

@SpringBootApplication
public class SpringbootCondition01Application {
    public static void main(String[] args) {

        //启动SpringBoot的应用,返回Spring的IOC容器
        ConfigurableApplicationContext context =  SpringApplication.run(SpringbootCondition01Application.class, args);
      
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

案例:需求2

在 Spring 的 IOC 容器中有一个 User 的Bean,现要求: 将类的判断定义为动态的。判断哪个字节码文件存在可以动态指定

实现步骤: 不使用@Conditional(ClassCondition.class)注解,自定义注解@ConditionOnClass,因为他和之前@Conditional注解功能一致,所以直接复制编写ClassCondition中的matches方法

 自定义注解@ConditionOnClass:

@Target({ElementType.TYPE, ElementType.METHOD})//可以修饰在类与方法上
@Retention(RetentionPolicy.RUNTIME)//注解生效节点runtime
@Documented//生成文档
@Conditional(value=ClassCondition.class)
public @interface ConditionOnClass {
    String[] value();//设置此注解的属性redis.clients.jedis.Jedis
}

 ClassCondition类:

public class ClassCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //2.需求: 导入通过注解属性值value指定坐标后创建Bean
        //获取注解属性值  value
        Map<String, Object> map = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        System.out.println(map);
        String[] value = (String[]) map.get("value");
        boolean flag = true;
        try {
            for (String className : value) {
                Class<?> cls = Class.forName(className);
            }
        } catch (ClassNotFoundException e) {
            flag = false;
        }
        return flag;

    }
}

配置类:

@Configuration
public class UserConfig {
    //情况1
    @Bean
//    @Conditional(ClassCondition.class)
//    @ConditionOnClass(value="redis.clients.jedis.Jedis")
    @ConditionOnClass(value={"com.alibaba.fastjson.JSON","redis.clients.jedis.Jedis"})
    public User user(){
        return new User();
    }

    //情况2
    @Bean
    //当容器中有一个key=k1且value=v1的时候user2才会注入
    //在application.properties文件中添加k1=v1
    @ConditionalOnProperty(name = "k1",havingValue = "v1")
    public User user2(){
        return new User();
    }

}

 测试:

@SpringBootApplication
public class SpringbootCondition02Application {
    public static void main(String[] args) {
        //启动SpringBoot的应用,返回Spring的IOC容器
        ConfigurableApplicationContext context =  SpringApplication.run(SpringbootCondition02Application.class, args);
        Object user2 = context.getBean("user2");
        System.out.println(user2);
    }
}

二.@Enable注解

这里引入多模块的概念:

比起传统复杂的单体工程,使用Maven的多模块配置,可以帮助项目划分模块,鼓励重用,防止pom变得过于庞大,方便某个模块的构建,而不用每次都构建整个项目。

1.首先使用 Spring Initializr 来快速创建好一个Maven项目。这是子项目,在子项目里写相关功能类。

2.然后在父项目的 pom.xml 里面声明该父项目包含的子模块。(其它信息就不逐一讲述了,诸如继承SpringBoot官方父项目以及统一依赖管理)

实例如下:

1.子项目构建好实现类

2.父项目导入子项目的模块坐标,然后进行使用

    <!--导入坐标-->
    <dependency>
        <groupId>com.wei</groupId>
        <artifactId>enable02</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

 回归正题——————————————————————————————————————————

SpringBoot中提供了很多Enable开头的注解,这些注解都是用于动态启用某些功能的。而其底层原理是使用@Import注解导入一些配置类,实现Bean的动态加载

@Enable底层依赖于@Import注解导入一些类,使用@Import导入的类会被Spring加载到IOC容器中。

而@Import提供4种用法:

接下来的实例使用了上述所说的多模块,子项目写相关功能类及自定义注解,父项目导入子项目坐标,完成重用

① 导入Bean

直接写好实体类,使用@Import注解导入即可

@SpringBootApplication
@Import(User.class)
public class Enable01Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Enable01Application.class, args);

        //获取Bean
        User user = context.getBean(User.class);
        System.out.println(user);
    }
}

② 导入配置类

配置类:

@Configuration
public class UserConfig {

    @Bean
    public User user(){
        return new User();
    }
}

导入配置类并测试: 

@SpringBootApplication
@Import(UserConfig.class)
public class Enable01Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Enable01Application.class, args);

        //获取Bean
        User user = context.getBean(User.class);
        System.out.println(user);
   }
}

③ 导入 ImportSelector 实现类

一般用于加载配置文件中的类:

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        //目前字符串数组的内容是写死的,未来可以设置在配置文件中动态加载
        return new String[]{"com.wei.enable02.domain.User", "com.wei.enable02.domain.Student"};
    }
}

导入ImportSelector的实现类并测试:

@SpringBootApplication
@Import(MyImportSelector.class)
public class Enable01Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Enable01Application.class, args);
      
        //获取Bean
        User user = context.getBean(User.class);
        System.out.println(user);
    }
}

④ 导入 ImportBeanDefinitionRegistrar 实现类

ImportBeanDefinitionRegistrar的实现类:

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //1.获取user的definition对象
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();

        //2.通过beanDefinition属性信息,向spring容器中注册id为user的对象
        registry.registerBeanDefinition("user", beanDefinition);

    }
}

导入ImportBeanDefinitionRegistrar的实现类并测试:

@SpringBootApplication
@Import(MyImportBeanDefinitionRegistrar.class)
public class Enable01Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Enable01Application.class, args);

        //获取Bean
        User user = context.getBean(User.class);
        System.out.println(user);
    }
}

三、@EnableAutoConfiguration 注解

主启动类:springboot启动类上有一个@SpringBootApplication注解,这是springboot项目一个必不可少的注解。

@SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用

@SpringBootApplication
 public class SpringbootApplication {
 public static void main(String[] args) {
 //以为是启动了一个方法,没想到启动了一个服务
    SpringApplication.run(SpringbootApplication.class, args);
 }
 }

@SpringBootApplication注解内部:

@SpringBootApplication是一个复合注解,其中包含了@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan注解

下面我们分别展开看看:

1.@ComponentScan

这个注解在Spring中很重要 ,它对应XML配置中的元素。

作用:自动扫描并加载符合条件的组件或者bean , 将这个bean定义加载到IOC容器中

2.@SpringBootConfiguration

作用:SpringBoot的配置类 ,标注在某个类上 , 表示这是一个SpringBoot的配置类;

3.@EnableAutoConfiguration开启自动配置功能

以前我们需要自己配置的东西,而现在SpringBoot可以自动帮我们配置 ;

@EnableAutoConfiguration 告诉SpringBoot开启自动配置功能,这样自动配置才能生效;

接下来@EnableAutoConfiguration注解如何开启自动配置,@EnableAutoConfiguration也是一个复合注解,关键部分在它导入的AutoConfigurationImportSelector类中。

@Import({AutoConfigurationImportSelector.class}) :给容器导入组件 ; AutoConfigurationImportSelector :自动配置导入选择器,给容器中导入一些组件 ;

 在AutoConfigurationImportSelector类中有一个selectImports()方法,通过SpringFactoriesLoader.loadFactoryNames()扫描所有具有META-INF/spring.factories的jar包,获取获取spring.factories中key为org.springframework.boot.autoconfigure.EnableAutoConfiguration的值(配置类名),然后把这些类导入到spring的ioc容器中。

总结原理:

  1. @EnableAutoConfiguration 注解内部使用 @Import(AutoConfigurationImportSelector.class) 来加载配置类。
  2. 配置文件位置:META-INF/spring.factories,该配置文件中定义了大量的配置类,当 SpringBoot 应用启动时,会自动加载这些配置类,初始化Bean
  3. 并不是所有的Bean都会被初始化,在配置类中使用Condition来加载满足条件的Bean 

四、自定义启动器

需求: 自定义redis-starter,要求当导入redis坐标时,SpringBoot自动创建Jedis的Bean

实现步骤:

  1. 创建redis-spring-boot-autoconfigure模块
  2. 创建redis-spring-boot-starter模块,依赖redis-spring-boot-autoconfigure的模块
  3. 在redis-spring-boot-autoconfigure模块中初始化Jedis的Bean,并定义META INF/spring.factories文件
  4. 在测试模块中引入自定义的redis-starter依赖,测试获取Jedis的Bean,操作redis

创建redis-spring-boot-autoconfigure模块 

1、编写一个配置类,这个配置类使用@ConfigurationProperties注解与全局配置文件中的以spring.redis开头的一组配置文件绑定。

@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
    private String host="localhost";
    private int port = 6379;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}

2、编写一个自动配置加载类,这个类将使用@EnableConfigurationProperties注解将配置类加载到spring的ioc容器中。

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoconfiguration {
    //注入jedis
    @Bean
    public Jedis jedis(RedisProperties redisProperties){
        return new Jedis(redisProperties.getHost(),redisProperties.getPort());
    }
}

3、将编写的自动配置类添加到resources/META-INF/spring.factories(新建一个即可)中EnableAutoConfiguration的值中。

 

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.wei.redisspringbootautoconfigure.RedisAutoconfiguration

创建redis-spring-boot-starter模块,依赖redis-spring-boot-autoconfigure的模块

<!--引入自定义的redis-spring-boot-autoconfigure-->
<dependency>
    <groupId>com.wei</groupId>
    <artifactId>redis-spring-boot-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

在测试模块中引入自定义的redis-starter依赖,测试获取Jedis的Bean,操作redis

1.导入redis-spring-boot-starter坐标

<!--导入坐标-->
<dependency>
    <groupId>com.wei</groupId>
    <artifactId>redis-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2.测试获取Jedis的Bean,操作redis

@SpringBootApplication
public class SpringbootStarterTestApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootStarterTestApplication.class, args);
        Jedis bean = context.getBean(Jedis.class);
        System.out.println(bean);
    }

}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2051592.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【三维目标检测】H3DNet(三)

【版权声明】本文为博主原创文章&#xff0c;未经博主允许严禁转载&#xff0c;我们会定期进行侵权检索。 参考书籍&#xff1a;《人工智能点云处理及深度学习算法》 H3DNet数据和源码配置调试过程以及主干网络介绍请参考上一篇博文&#xff1a;【三维目标检测】H3DNet&am…

[Java]面向对象-内部类

类的五大成员&#xff1a;属性、方法、构造方法、代码块、内部类 内部类 在一个类里边再定义一个类。可以在外部其他类中创建内部类对象并调用它的方法 public class Outer {//外部类public class inner{//内部类} } 内部类使用时机&#xff1a; 内部类表示的事物是外部类…

打造高可用集群的基石:深度解析Keepalived实践与优化

高可用集群 集群类型 集群类型主要分为负载均衡集群&#xff08;LB&#xff09;、高可用集群&#xff08;HA&#xff09;和高性能计算集群&#xff08;HPC&#xff09;三大类。每种集群类型都有其特定的应用场景和优势。 1. 负载均衡集群&#xff08;LB&#xff09; 负载均衡集…

drawio的问题

drawio的问题 先给出drawio的链接https://app.diagrams.net/ 我在用overleaf写论文的过程中&#xff0c;发现了一个问题&#xff0c;就是使用drawio画好图之后&#xff0c;只能保存以下几个选项&#xff1a; 但是不管是什么类型&#xff0c;在overleaf上面图片都不显示。如果…

SpringBoot如何做自动配置

目录 一、什么是springboot的自动配置&#xff1f; 二、Enable注解 三、springboot自动配置流程 ComponentScan SpringBootConfiguration EnableAutoConfiguration注解 condition的几个注解&#xff1a; 四、自定义启动类 需求&#xff1a; 参考&#xff1a; 实现步…

使用JQUERY请求数据出现500报错

我在写项目的时候遇到了一个问题&#xff0c;就是在存商品id的时候我将它使用了JSON.stringify的格式转换了&#xff01;&#xff01;&#xff01;于是便爆出了500这个错误&#xff01;&#xff01;&#xff01; 我将JSON.stringify的格式去除之后&#xff0c;它就正常显示了&a…

【数学建模】趣味数模问题——四人追逐问题

问题描述&#xff1a; 如图所示&#xff0c;在正方形ABCD的四个顶点各有一个人。在初始时刻 t0 时&#xff0c;四人同时出发&#xff0c;以匀速 v 沿顺时针方向朝下一个人移动。如果他们始终对准下一个人为目标行进&#xff0c;最终结果会如何&#xff1f;需要作出各自的运动轨…

路径规划 | 灰狼算法+B样条曲线优化无人机三维路径规划(Matlab)

目录 效果一览基本介绍程序设计参考文献 效果一览 基本介绍 灰狼算法B样条曲线优化无人机三维路径规划&#xff08;Matlab&#xff09; 群智能路径规划算法。三维灰狼算法&#xff08;GWO&#xff09;加B样条曲线优化的matlab代码。无人机&#xff08;UAV&#xff09;路径规划…

QT:安装软件

QT 介绍 QT是一个跨平台的C应用程序开发框架&#xff0c;具有广泛的应用领域和强大的功能。 定义&#xff1a;QT是一个跨平台的C图形用户界面应用程序框架&#xff0c;为开发者提供了建立艺术级图形界面所需的所有功能。 特点&#xff1a;QT具有短平快的优秀特质&#xff0c;即…

csrf漏洞(二)

本文仅作为学习参考使用&#xff0c;本文作者对任何使用本文进行渗透攻击破坏不负任何责任。 前言&#xff1a; 本文依靠phpstudy以及dvwa靶场进行操作&#xff0c;具体搭建流程参考&#xff1a;xss漏洞&#xff08;二&#xff0c;xss靶场搭建以及简单利用&#xff09; 前篇…

Godot关于3d射线投射的一些问题

首先你得把刚体模式激活如图否则将是空对象 为了区分其他坐标颜色园哥把射线设置成紫色以示区别 另外运行模式中貌似射线不可见只在调试模式中可见。最后查看调试器 成功碰撞一个名为主角的物体&#xff0c;也许都命中了但是方法只返回第一个命中的物体&#xff0c;吐槽一下&a…

场外个股期权如何发出行权指令?

场外期权行权指令也就是平仓指令的意思&#xff0c;一般场外个股期权交易有三种方式开仓和行权平仓指令&#xff0c;分别是市价&#xff0c;限价和半小时询价&#xff0c;跟普通股票的买卖和交易方式类似&#xff0c;唯一区别是手动发出场外个股期权的行权指令&#xff0c;下文…

Linux系统中的高级系统资源管理技术:systemd资源控制

在当今信息技术的快速发展中&#xff0c;服务器的性能管理和资源分配变得尤为重要。Linux操作系统以其灵活性和可定制性在服务器领域广受欢迎。而在Linux系统中&#xff0c;systemd资源控制作为一项重要的高级系统资源管理技术&#xff0c;为管理员提供了精细化控制和调整系统服…

STM32标准库学习笔记-2.GPIO

参考教程&#xff1a;【STM32入门教程-2023版 细致讲解 中文字幕】 标准库开发新建工程准备工作&#xff1a; 建立工程文件夹&#xff0c;Keil中新建工程&#xff0c;选择型号STM32F103C8T6工程文件夹里建立Start、Library、User等文件夹&#xff0c;复制固件库里面的文件到工…

【leetcode详解】实现一个魔法字典(思路详解 错误反思)

关于输入的解释&#xff1a; ‘ 输入 ’下方第一个列表是“调用函数”&#xff0c;第二个列表是“提供的字符串”&#xff0c;二者一一对应 如示例中buildDict对应[[“hello”, "leetcode"]]&#xff0c;即构建的字典包含 “hello” 和 "leetcode" 两个字…

基于laravel开发的开源交易所源码|BTC交易所/ETH交易所/交易所/交易平台/撮合交易引擎

开源交易所&#xff0c;基于Laravel开发的交易所 | BTC交易所 | ETH交易所 | 交易所 | 交易平台 | 撮合交易引擎。本项目有完整的撮合交易引擎源码、后台管理&#xff08;后端前端&#xff09;、前台&#xff08;交易页面、活动页面、个人中心等&#xff09; 特色&#xff1a;…

smbms

框架 数据库 项目如何搭建 考虑使不使用maven&#xff0c;依赖与jar包 项目搭建准备工作 搭建一个maven web项目配置tomcat测试项目是否能够跑起来导入项目中会遇到的jar包 //jsp、servlet、mysql驱动、jstl、stand.... <dependency><groupId>junit</groupI…

链表的删除 203、237、19 链表的遍历 430

203. 移除链表元素&#xff08;简单&#xff09; 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 解法一、递归 见代码 链表的定义具有递归的性质&#xff0c;所以遍历也可以以递归的方…

百度AI智能云依赖库OpenSSL库和Curl库及jsoncpp库安装

开发百度AI项目时&#xff0c;需要用到https协议&#xff0c;因此需要安装OpenSSl和curl库。 若只安装curl库&#xff0c;只支持http协议&#xff0c;不支持https协议。此外&#xff0c;还需要jsoncpp库&#xff0c;用以组包及解析与百度AI通信的json格式协议。 1.Ubuntu上安装…

修饰者模式

文章目录 1.简单介绍2.修饰器的优缺点3.模式结构3.案例分析4.总结5.装饰器模式和代理模式对比 1.简单介绍 装饰器模式&#xff08;Decorator Pattern&#xff09;也叫包装模式(Wrapper Pattern),是指在不改变原有对象的基础之上&#xff0c;将功能附加到对象上&#xff0c;提供…