当前位置: 首页 > news >正文

SpringBoot自动配置的原理篇,剖析自动配置原理;实现自定义启动类!附有代码及截图详细讲解

SpringBoot 自动配置

Condition

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

思考:SpringBoot是如何知道要创建哪个Bean的?比如SpringBoot是如何知道要创建RedisTemplate的?

演示1:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1. 没有添加坐标前,发现为空,报错ConfigurableApplicationContext context =SpringApplication.run(SpringbootCondition01Application.class, args);Object redisTemplate = context.getBean("redisTemplate");System.out.println(redisTemplate);
2. 有添加坐标前,发现有对象ConfigurableApplicationContext context =SpringApplication.run(SpringbootCondition01Application.class, args);Object redisTemplate = context.getBean("redisTemplate");System.out.println(redisTemplate);
疑问,他怎么知道的配置哪个类案例1

案例1:

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

代码实现:

  1. POJO实体类:User

    public class User {
    }
    
  2. 现在对User的加载有条件,不能直接用@Component注入,需要写配置类通过@Bean的方式注入

    @Configuration
    public class UserConfig {@Bean@Conditional(value = ClassCondition.class)    // 注入条件public User user(){return  new User();}
    }
    

    @Conditional中的ClassCondition.class的matches方法,返回true执行以下代码,否则反之

  3. 创建ClassCondition类实现Condition接口,重写matches方法

    public class ClassCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {boolean falg = true;try {Class<?> cls = Class.forName("redis.clients.jedis.Jedis");} catch (ClassNotFoundException e) {falg=false;}return falg;}
    }
    
  4. 启动类:

    @SpringBootApplication
    public class SpringbootCondition01Application {public static void main(String[] args) {//启动SpringBoot的应用,返回Spring的IOC容器ConfigurableApplicationContext context = SpringApplication.run(SpringbootCondition01Application.class,args);//获取Bean,redisTemplate//情况1 没有添加坐标前,发现为空//情况2 有添加坐标前,发现有对象Object user = context.getBean("user");System.out.println(user);}
    }
    
  5. 测试,通过pom文件中对Jedis坐标是否注释

    1. Jedi坐标未注释,可以打印出User对象地址

    2. Jedis坐标被注释掉,报错,不打印User对象地址

案例二:

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

实现步骤:

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

代码实现:

  1. POJO实体类User和案例1相同

  2. User的注入条件需要改变,使用@ConditionOnClass

    @Configuration
    public class UserConfig {@Bean
    //    @ConditionOnClass(value = "redis.clients.jedis.Jedis")  有jedis坐标注入User@ConditionOnClass(value = {"redis.clients.jedis.Jedis","com.alibaba.fastjson.JSON"})  //jedis和json都有才注入 public User user(){return new User();}
    }
    
  3. 重写matches方法

    public class ClassCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Map<String, Object> map = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());System.out.println("map:"+map);String[] value = (String[]) map.get("value");boolean isok = true;try {for(String val : value){Class<?> cls = Class.forName(val);}}catch (ClassNotFoundException e){isok = false;}return isok;}
    }
    
  4. 自定义注解

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

    @SpringBootApplication
    public class SpringbootCondition02Application {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(SpringbootCondition02Application.class, args);Object user = context.getBean("user");System.out.println(user);}
    }
    
  6. 测试:通过pom文件中的Jedis和JSON坐标测试

    • Jedis和JSON坐标都有:打印User对象地址
    • 只有一个坐标或者都没有:报错,不打印User对象地址

Condition总结

自定义条件:

  1. 定义条件类:自定义类实现Condition接口,重写 matches 方法,在 matches 方法中进行逻辑判断,返回boolean值 。matches 方法两个参数:
    • context:上下文对象,可以获取属性值,获取类加载器,获取BeanFactory等。
    • metadata:元数据对象,用于获取注解属性。
  2. 判断条件: 在初始化Bean时,使用 @Conditional(条件类.class)注解

SpringBoot提供的常用条件注解:

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

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

@Enable注解

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

思考 SpringBoot 工程是否可以直接获取jar包中定义的Bean?

@Enable的底层核心:@Import

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

  1. 导入Bean
  2. 导入配置类
  3. 导入 ImportSelector 实现类。一般用于加载配置文件中的类
  4. 导入 ImportBeanDefinitionRegistrar 实现类

导入Bean

代码实现:

需要用到多模块编程,项目结构如图:

在这里插入图片描述

springboot_enable_02:

  1. springboot项目,pom文件不需要格外到坐标

  2. User

    public class User {
    }
    
  3. UserConfig

    @Configuration
    public class UserConfig {@Beanpublic User user(){return new User();}
    }
    
  4. EnableUser自定义注解

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Import(UserConfig.class)
    public @interface EnableUser {
    }
    
  5. 启动类

    @SpringBootApplication
    public class SpringbootEnable02Application {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnable02Application.class, args);Object user = context.getBean("user");   // enable_02中肯定能取到UserSystem.out.println(user);}
    }
    

springboot_enable_01:

  1. pom文件导入springboot_enable_02的gav:

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
    </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
    </dependency><!--模块2-->
    <dependency><groupId>com.dong</groupId><artifactId>springboot_enable_02</artifactId><version>0.0.1-SNAPSHOT</version>
    </dependency>
    
  2. 测试:

    //@Import(User.class)
    //@Import(UserConfig.class)
    //@EnableUser
    @ComponentScan("com.dong.springboot_enable_02.config")
    @SpringBootApplication
    public class SpringbootEnable01Application {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnable01Application.class, args);Object user = context.getBean(User.class);System.out.println(user);}
    }
    

    因为enable_01导如了enable_02的坐标,相当是把02的所有代码拷贝在与01的启动类同级目录下,如果包名和包的层级都相同,就可以直接装配User类,但是包的层级不同就必须加注解

    • @Import(User.class):对应第一个使用场景

      ​ 导入02中的User类

    • @Import(UserConfig.class):对应第二个使用场景

      ​ 加载02中的UserConfig配置类,加载配置类中所有的方法和类

    • @EnableUser:

      ​ 自定义的注解,包装了一个@Import(User.class)

    • @ComponentScan(“com.dong.springboot_enable_02.config”):

      ​ 扫描包,加载类和配置类

导入配置类

见上方示例的@Import(UserConfig.class),导入配置类加载配置类中所有的方法和类

导入ImportSelector 实现类

导入 ImportBeanDefinitionRegistrar 实现类

ImportSelector 和 ImportBeanDefinitionRegistrar 一起演示

代码实现:

​ 项目结构
在这里插入图片描述

springboot_enable_04

  1. springboot项目,pom文件不需要额外到坐标

  2. POJO

    实体类:User

    public class User {
    }
    

    Student

    public class Student {
    }
    
  3. UserConfig配置类

    @Configuration
    public class UserConfig {@Beanpublic User user(){return  new User();}
    }
    
  4. MyImportSelector

    public class MyImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{"com.dong.springboot_enable_04.pojo.User","com.dong.springboot_enable_04.pojo.Student"};}
    }
    
  5. MyImportBeanDefinitionRegistrar

    public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();registry.registerBeanDefinition("user1",beanDefinition);}
    }
    

springboot_enable_03

  1. springboot_enable_03pom文件导入springboot_enable_04坐标

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--springboot_enable_04坐标--><dependency><groupId>com.dong</groupId><artifactId>springboot_enable_04</artifactId><version>0.0.1-SNAPSHOT</version></dependency>
    </dependencies>
    
  2. 测试:启动类

    @ComponentScan("com.dong.springboot_enable_04.config")
    @Import(User.class)
    @Import(UserConfig.class)
    @Import(MyImportSelector.class)
    @Import(MyImportBeanDefinitionRegistrar.class)
    @SpringBootApplication
    public class SpringbootEnable03Application {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnable03Application.class, args);User user = context.getBean(User.class);System.out.println(user);Student student = context.getBean(Student.class);System.out.println(student);User user1 = (User) context.getBean("user1");System.out.println(user1);}
    }
    
    • @Import(MyImportSelector.class):对应第三个使用场景

      ​ MyImportSelector实现了ImportSelector接口重写了selectImports方法,在字符出数组中的完全相对路径即注入了容器,因为引入了enable_04,所以03中可以获取到User和Student

    • @Import(MyImportBeanDefinitionRegistrar.class):对应第四个使用场景

      ​ MyImportBeanDefinitionRegistrar实现了ImportBeanDefinitionRegistrar接口,registerBeanDefinitions方法,在此方法中就可以对Bean进行注册,如案例演示,04注入了一个User对象,id是user1,所以03中就可以获取到User这个对象,打印出地址

@EnableAutoConfiguration注解

  • 主启动类

    //@SpringBootApplication 来标注一个主程序类
    //说明这是一个Spring Boot应用
    @SpringBootApplication
    public class SpringbootApplication {public static void main(String[] args) {//以为是启动了一个方法,没想到启动了一个服务SpringApplication.run(SpringbootApplication.class, args);}
    }
    
  • @SpringBootApplication注解内部

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

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

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

    2. @SpringBootConfiguration

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

      //@SpringBootConfiguration注解内部
      //这里的 @Configuration,说明这是一个配置类 ,配置类就是对应Spring的xml 配置文件;
      @Configuration
      public @interface SpringBootConfiguration {}
      //里面的 @Component 这就说明,启动类本身也是Spring中的一个组件而已,负责启动应用
      @Component
      public @interface Configuration {}
      
  • AutoConfigurationPackage :自动配置包

    //AutoConfigurationPackage的子注解
    //Registrar.class 作用:将主启动类的所在包及包下面所有子包里面的所有组件扫描到Spring容器
    @Import({Registrar.class})
    public @interface AutoConfigurationPackage {
    }
    
  • @EnableAutoConfiguration开启自动配置功能

    以前我们需要自己配置的东西,而现在SpringBoot可以自动帮我们配置 ;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能,这样自动配置才能生效;

    @Import({AutoConfigurationImportSelector.class}):给容器导入组件 ;

    AutoConfigurationImportSelector :自动配置导入选择器,给容器中导入一些组件

AutoConfigurationImportSelector.class↓selectImports方法↓
this.getAutoConfigurationEntry(annotationMetadata)方法↓
this.getCandidateConfigurations(annotationMetadata, attributes)方法↓
方法体:List<String> configurations =
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass
(), this.getBeanClassLoader());Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;↓
在所有包名叫做autoConfiguration的包下面都有META-INF/spring.factories文件

总结原理:

  • @EnableAutoConfiguration 注解内部使用 @Import(AutoConfigurationImportSelector.class)

    来加载配置类。

  • 配置文件位置:META-INF/spring.factories,该配置文件中定义了大量的配置类,当 SpringBoot

    应用启动时,会自动加载这些配置类,初始化Bean

  • 并不是所有的Bean都会被初始化,在配置类中使用Condition来加载满足条件的Bean

SpringBoot自动配置原理扒源代码图解

在这里插入图片描述

自定义启动类的实现

案例需求:

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

参考:

可以参考mybatis启动类的应用

在这里插入图片描述

实现步骤:

  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. pom文件

      <dependencies><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--spring-boot-starter-test注释掉了-->
      </deendencies>
      
    2. RedisAutoConfiguration配置类

      @Configuration
      @EnableConfigurationProperties(RedisProperties.class)
      public class RedisAutoConfiguration {@Beanpublic Jedis jedis(RedisProperties redisProperties){return new Jedis(redisProperties.getHost(),redisProperties.getPort());}
      }
      
    3. 动态获取主机号和端口号的类

      @ConfigurationProperties(prefix = "spring.redis")  
      //如果配置文件中有就读取配置文件中的spring.redis下面get、set方法就会对host和port达到动态效果,如果配置文件中没有spring.redis就默认localhost和6379
      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;}
      }
      
    4. 在resources目录下创建META-INF文件夹,下创建spring.factories文件,写入键值

      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.dong.redisspringbootautoconfigure.config.RedisAutoConfiguration
      
  • redis-spring-boot-start模块

    此模块只需要在pom文件中导入上个模块的gav,test文件夹和java下的目录都可以删除

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--引入自定义的redis-spring-boot-autoconfigure--><dependency><groupId>com.dong</groupId><artifactId>redis-spring-boot-autoconfigure</artifactId><version>0.0.1-SNAPSHOT</version></dependency>
    </dependencies>
    
  • 测试模块springboot_enable_05

    1. pom文件导入redis-spring-boot-start模块gav

      <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--导入自定义启动类--><dependency><groupId>com.dong</groupId><artifactId>redis-spring-boot-start</artifactId><version>0.0.1-SNAPSHOT</version></dependency>
      </dependencies>
      
    2. yaml配置文件修改端口

      spring:redis:port: 6060
      
    3. 测试:

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

      输出内容:BinaryJedis{Connection{DefaultJedisSocketFactory{localhost:6060}}}

      说明走了我们自定义的启动器

相关文章:

SpringBoot自动配置的原理篇,剖析自动配置原理;实现自定义启动类!附有代码及截图详细讲解

SpringBoot 自动配置 Condition Condition 是在Spring 4.0 增加的条件判断功能&#xff0c;通过这个可以功能可以实现选择性的创建 Bean 操作 思考&#xff1a;SpringBoot是如何知道要创建哪个Bean的&#xff1f;比如SpringBoot是如何知道要创建RedisTemplate的&#xff1f;…...

苹果Ios系统app应用程序开发者如何获取IPA文件签名证书时需要注意什么?

今天呢想和大家介绍介绍苹果App开发者如何获取IPA文件签名证书的步骤和注意事项。对于苹果应用程序开发者而言&#xff0c;获取IPA文件签名证书是发布应用程序至App Store的重要步骤之一。签名证书能够确保应用程序的安全性和可信度&#xff0c;并使其能够在设备上正确运行。 …...

算法通关村第七关-黄金挑战二叉树迭代遍历

大家好我是苏麟 , 今天带来二叉树的迭代遍历 . 二叉树的迭代遍历 前序编列 描述 : 给你二叉树的根节点 root &#xff0c;返回它节点值的 前序 遍历。 题目 : LeetCode 二叉树的前序遍历 : 144. 二叉树的前序遍历 分析 : 前序遍历是中左右&#xff0c;如果还有左子树就一…...

2023-11-Rust

学习方案&#xff1a;Rust程序设计指南 1、变量和可变性 声明变量&#xff1a;let 变量、const 常量 rust 默认变量一旦声明&#xff0c;就不可变(immutable)。当想改变 加 mut&#xff08;mutable&#xff09; 。 const 不允许用mut &#xff0c;只能声明常量&#xff0c;…...

iOS代码混淆----自动

先大致解释一下“编译"、"反编译": 编译&#xff1a;就是把千千万万行字符串(也叫代码&#xff0c;或者源文件)&#xff0c;变成010101010101(机器码&#xff0c;也叫目标代码) 编译过程&#xff1a;预处理-编译-汇编-链接 我的脚本运行在预处理阶段。 反编…...

对Mysql和应用微服务做TPS压力测试

1.对Mysql 使用工具&#xff1a;mysqlslap工具 使用命令&#xff1a; mysqlslap -uroot pGG8697000!#--auto generate sql -auto generate sql-load typemixed-concurrency100,200 - number of queries1000-iterations10 - number-int-cols7 - number-charcols13auto genera…...

将程序添加至右键菜单

将程序添加至右键菜单 手动导入 如果要将cmder添加至右键菜单。可以通过编写reg注册表方式添加 也可以在路径HKEY_CLASSES_ROOT\Directory\Background\shell中右击添加 创建项commadn 编写reg注册表 [HKEY_CLASSES_ROOT\Directory\Background\shell\cmder]为注册表地址 Wi…...

三板斧的使用、全局配置文件、静态文件的配置、orm介绍

三板斧的使用 【1】HttpResponse 返回字符串类型 【2】render 返回html页面&#xff0c;并且在返回给浏览器之前还可以给html页面传值 【3】redirect 重定向页面 视图函数必须返回一个 HttpResponse 对象 def index(request):print(request)# return HttpResponse("r…...

【编程实践】黑框框里的打字小游戏,但是汇编语言

开始&#xff1a; 在学习王爽的《汇编语言》的过程中&#xff0c;我就真切地体会到编程实践对于理解的帮助。起初我没有安装书中的实验环境&#xff0c;看到100页左右就开始感觉无趣、吃力&#xff0c;看了后面忘前面&#xff0c;差点就要放弃这本书的学习。好在我后来还是装好…...

ElasticSearch的集群、节点、索引、分片和副本

Elasticsearch是面向文档型数据库&#xff0c;一条数据在这里就是一个文档。为了方便大家理解&#xff0c;我们将Elasticsearch里存储文档数据和关系型数据库MySQL存储数据的概念进行一个类比 ES里的Index可以看做一个库&#xff0c;而Types相当于表&#xff0c;Documents则相当…...

std::cout无法打印uint8_t类型的数据

std::cout在处理uint8_t变量类型的时候默认输出字符&#xff0c;刚好数字0-10对应的ascii字符都是不可打印的 解决&#xff1a; 使用static_cast std::cout << static_cast<int>(time) << std::endl;参考文章&#xff1a;https://blog.csdn.net/weixin_459…...

浅谈泛在电力物联网在智能配电系统应用

贾丽丽 安科瑞电气股份有限公司 上海嘉定 201801 摘要&#xff1a;在社会经济和科学技术不断发展中&#xff0c;配电网实现了角色转变&#xff0c;传统的单向供电服务形式已经被双向能流服务形式取代&#xff0c;社会多样化的用电需求也得以有效满足。随着物联网技术的发展&am…...

已解决:云原生领域的超时挂载Bug — Kubernetes深度剖析

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…...

概念解析 | 高光谱图像:揭开自然世界的神秘面纱

注1:本文系“概念解析”系列之一,致力于简洁清晰地解释、辨析复杂而专业的概念。本次辨析的概念是:高光谱图像 高光谱图像:揭开自然世界的神秘面纱 Hyperspectral imaging - Wikipedia 背景介绍 我们生活的世界充满了丰富多彩的颜色。这些颜色来源于各种物体反射或吸收不同波长…...

Java类和对象(1)

&#x1f435;本篇文章将会开始对类和对象的第一部分讲解 一、简单描述类和对象 对象可以理解为一个实体&#xff0c;在现实生活中&#xff0c;比如在创建一个建筑之前&#xff0c;要先有一个蓝图&#xff0c;这个蓝图用来描述这个建筑的各种属性&#xff1b;此时蓝图就是类&a…...

百度上海智能研发中心一面

Prometheus告警机制原理 介绍hashmap和concurrentHashmap concurrentHashmap和hashmap如果线程1在遍历 另一个线程对这个map进行修改操作 会发生什么现象 对线程安全的理解 通过什么方法解决线程安全 除了上锁 CAS等还有其他手段 不用锁的话 &#xff08;集合的类设计成一…...

硝烟后的茶歇 | 中睿天下谈攻防演练之邮件攻击溯源实战分享

近日&#xff0c;由中国信息协会信息安全专业委员会、深圳市CIO协会、PCSA安全能力者联盟主办的《硝烟后的茶歇广东站》主题故事会在深圳成功召开。活动已连续举办四年四期&#xff0c;共性智慧逐步形成《年度红蓝攻防系列全景图》、《三化六防“挂图作战”》等共性研究重要成果…...

Leetcode Hot 100之四:283. 移动零+11. 盛最多水的容器

283.移动零 题目&#xff1a; 给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &#xff0c;必须在不复制数组的情况下原地对数组进行操作。 示例 1: 输入: nums [0,1,0,3,12] 输出: [1,3,12,0,0] …...

景联文科技助力金融机构强化身份验证,提供高质量人像采集服务

随着社会的数字化和智能化进程的加速&#xff0c;人像采集在金融机构身份认证领域中发挥重要作用&#xff0c;为人们的生活带来更多便利和安全保障。 金融机构在身份验证上的痛点主要包括以下方面&#xff1a; 身份盗用和欺诈风险&#xff1a;传统身份验证方式可能存在漏洞&am…...

Spring Cloud LoadBalancer基础知识

LoadBalancer 概念常见的负载均衡策略使用随机选择的负载均衡策略创建随机选择负载均衡器配置 Nacos 权重负载均衡器创建 Nacos 负载均衡器配置 自定义负载均衡器(根据IP哈希策略选择)创建自定义负载均衡器封装自定义负载均衡器配置 缓存 概念 LoadBalancer(负载均衡器)是一种…...

Vim 调用外部命令学习笔记

Vim 外部命令集成完全指南 文章目录 Vim 外部命令集成完全指南核心概念理解命令语法解析语法对比 常用外部命令详解文本排序与去重文本筛选与搜索高级 grep 搜索技巧文本替换与编辑字符处理高级文本处理编程语言处理其他实用命令 范围操作示例指定行范围处理复合命令示例 实用技…...

装饰模式(Decorator Pattern)重构java邮件发奖系统实战

前言 现在我们有个如下的需求&#xff0c;设计一个邮件发奖的小系统&#xff0c; 需求 1.数据验证 → 2. 敏感信息加密 → 3. 日志记录 → 4. 实际发送邮件 装饰器模式&#xff08;Decorator Pattern&#xff09;允许向一个现有的对象添加新的功能&#xff0c;同时又不改变其…...

零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?

一、核心优势&#xff1a;专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发&#xff0c;是一款收费低廉但功能全面的Windows NAS工具&#xff0c;主打“无学习成本部署” 。与其他NAS软件相比&#xff0c;其优势在于&#xff1a; 无需硬件改造&#xff1a;将任意W…...

可靠性+灵活性:电力载波技术在楼宇自控中的核心价值

可靠性灵活性&#xff1a;电力载波技术在楼宇自控中的核心价值 在智能楼宇的自动化控制中&#xff0c;电力载波技术&#xff08;PLC&#xff09;凭借其独特的优势&#xff0c;正成为构建高效、稳定、灵活系统的核心解决方案。它利用现有电力线路传输数据&#xff0c;无需额外布…...

Java多线程实现之Callable接口深度解析

Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...

[10-3]软件I2C读写MPU6050 江协科技学习笔记(16个知识点)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...

SpringCloudGateway 自定义局部过滤器

场景&#xff1a; 将所有请求转化为同一路径请求&#xff08;方便穿网配置&#xff09;在请求头内标识原来路径&#xff0c;然后在将请求分发给不同服务 AllToOneGatewayFilterFactory import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; impor…...

大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计

随着大语言模型&#xff08;LLM&#xff09;参数规模的增长&#xff0c;推理阶段的内存占用和计算复杂度成为核心挑战。传统注意力机制的计算复杂度随序列长度呈二次方增长&#xff0c;而KV缓存的内存消耗可能高达数十GB&#xff08;例如Llama2-7B处理100K token时需50GB内存&a…...

在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?

uni-app 中 Web-view 与 Vue 页面的通讯机制详解 一、Web-view 简介 Web-view 是 uni-app 提供的一个重要组件&#xff0c;用于在原生应用中加载 HTML 页面&#xff1a; 支持加载本地 HTML 文件支持加载远程 HTML 页面实现 Web 与原生的双向通讯可用于嵌入第三方网页或 H5 应…...

[免费]微信小程序问卷调查系统(SpringBoot后端+Vue管理端)【论文+源码+SQL脚本】

大家好&#xff0c;我是java1234_小锋老师&#xff0c;看到一个不错的微信小程序问卷调查系统(SpringBoot后端Vue管理端)【论文源码SQL脚本】&#xff0c;分享下哈。 项目视频演示 【免费】微信小程序问卷调查系统(SpringBoot后端Vue管理端) Java毕业设计_哔哩哔哩_bilibili 项…...