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

公司章程在工商官网哪里下载深圳关键词优化

公司章程在工商官网哪里下载,深圳关键词优化,北京专业企业营销网站建设,开发公司2022年工作计划Spring Boot 源码学习系列 RedisAutoConfiguration 详解 引言往期内容主要内容1. Spring Data Redis2. RedisAutoConfiguration2.1 加载自动配置组件2.2 过滤自动配置组件2.2.1 涉及注解2.2.2 redisTemplate 方法2.2.3 stringRedisTemplate 方法 总结 引言 上篇博文&#xff0…

Spring Boot 源码学习系列

在这里插入图片描述

RedisAutoConfiguration 详解

  • 引言
  • 往期内容
  • 主要内容
    • 1. Spring Data Redis
    • 2. RedisAutoConfiguration
      • 2.1 加载自动配置组件
      • 2.2 过滤自动配置组件
        • 2.2.1 涉及注解
        • 2.2.2 redisTemplate 方法
        • 2.2.3 stringRedisTemplate 方法
  • 总结

引言

上篇博文,Huazie 带大家从源码角度分析了 Spring Boot 内置的 http 编码功能,进一步熟悉了自动配置的装配流程。本篇趁热打铁,继续带大家分析 Spring Boot 内置的有关 Redis 的自动配置类【RedisAutoConfiguration】。

在这里插入图片描述

往期内容

在开始本篇的内容介绍之前,我们先来看看往期的系列文章【有需要的朋友,欢迎关注系列专栏】:

Spring Boot 源码学习
Spring Boot 项目介绍
Spring Boot 核心运行原理介绍
【Spring Boot 源码学习】@EnableAutoConfiguration 注解
【Spring Boot 源码学习】@SpringBootApplication 注解
【Spring Boot 源码学习】走近 AutoConfigurationImportSelector
【Spring Boot 源码学习】自动装配流程源码解析(上)
【Spring Boot 源码学习】自动装配流程源码解析(下)
【Spring Boot 源码学习】深入 FilteringSpringBootCondition
【Spring Boot 源码学习】OnClassCondition 详解
【Spring Boot 源码学习】OnBeanCondition 详解
【Spring Boot 源码学习】OnWebApplicationCondition 详解
【Spring Boot 源码学习】@Conditional 条件注解
【Spring Boot 源码学习】HttpEncodingAutoConfiguration 详解

主要内容

1. Spring Data Redis

Spring Data RedisSpring Data 家族的一部分,它提供了从 Spring 应用程序中轻松配置和访问 Redis 的功能。

我们来看看官方介绍的特性:

  • 连接包作为多个 Redis 驱动程序( LettuceJedis )的低级别抽象。
  • Redis 驱动程序异常转换为 Spring 的可移植数据访问异常层次结构。
  • 提供各种 Redis 操作、异常转换和序列化支持的 RedisTemplate
  • 支持发布订阅(例如用于消息驱动 POJO 的消息监听器容器)。
  • 支持 Redis SentinelRedis Cluster
  • 使用 Lettuce 驱动程序的响应式 API
  • 支持 JDKStringJSONSpring 对象 / XML 映射序列化器。
  • Redis 上实现 JDK 集合。
  • 支持原子计数器类。
  • 支持排序和管道功能。
  • 专用于 SORTSORT/GET模式和支持返回批量值的功能。
  • Spring 缓存抽象提供 Redis 实现。
  • 自动实现 Repository 接口,包括使用 @EnableRedisRepositories 支持自定义查询方法。
  • 对存储库提供 CDI 支持。

Spring Data Redis 中,我们可以直接使用 RedisTemplate 及其相关的类来操作 Redis。虽然 RedisConnection 提供了接受和返回二进制值(字节数组)的低级方法,但 RedisTemplate 负责序列化和连接管理,使用户可以无需处理这些细节。

RedisTemplate 还提供了操作视图(按照 Redis 命令参考进行分组),这些视图提供了丰富、通用的接口,用于针对特定类型或特定键进行操作(通过 KeyBound 接口实现),如下表所示:

接口描述
GeoOperationsRedis地理空间操作,例如GEOADD、GEORADIUS等。
HashOperationsRedis哈希操作
HyperLogLogOperationsRedis键绑定哈希操作
ListOperationsRedis列表操作
SetOperationsRedis集合操作
ValueOperationsRedis字符串(或值)操作
ZSetOperationsRedis有序集合操作
BoundGeoOperationsRedis键绑定地理空间操作
BoundHashOperationsRedis键绑定哈希操作
BoundKeyOperationsRedis键绑定操作
BoundListOperationsRedis键绑定列表操作
BoundSetOperationsRedis键绑定集合操作
BoundValueOperationsRedis键绑定字符串(或值)操作
BoundZSetOperationsRedis键绑定有序集合操作

下面我们来看看相关的 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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true"/><!-- redis 模板定义 --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"/></beans>

一旦配置完成,Redis 模板就是线程安全的,并且可以在多个实例之间重用。

RedisTemplate 使用基于 Java 的序列化器进行大部分操作。也就意味着通过模板写入或读取的任何对象都是通过 Java 进行序列化和反序列化的。

我们也可以更改模板上的序列化机制,可以添加如下配置:

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="redisConnectionFactory"/><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property><property name="hashKeySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="hashValueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property>
</bean>

而 Redis 模块提供了几个序列化器的实现,有关这些实现大家可以查看 org.springframework.data.redis.serializer 包。

在这里插入图片描述

还可以将任何序列化程序设置为 null,并通过设置 enableDefaultSerializer 属性为 false 来使用RedisTemplate 与原始字节数组一起使用。

注意: 模板要求所有键都不为空。但是,只要底层序列化程序接受值,值就可以为空。

下面我们可以注入 RedisTemplate,并调用 RedisTemplate 的方法进行存储、查询、删除等操作。

@Autowired
private RedisTemplate<String, Object> redisTemplate;// 存储数据
redisTemplate.opsForValue().set("key", "value");
// 查询数据
Object value = redisTemplate.opsForValue().get("key");
// 删除数据
redisTemplate.delete("key");

对于需要特定模板视图的情况,声明视图作为依赖项并注入模板。容器会自动执行转换,消除opsFor[X] 调用,如下所示的示例:

public class Example {	// inject the template as ListOperations@Resource(name="redisTemplate")private ListOperations<String, String> listOps;public void addLink(String userId, URL url) {listOps.leftPush(userId, url.toExternalForm());}
}

当然 Spring Data Redis 肯定不止上述这些,有需要深入了解的读者们,请看如下:

参考: Spring Data Redis 官方文档

2. RedisAutoConfiguration

那么 Spring Data Redis 的 RedisTemplate 的自动配置在 Spring Boot 是如何实现的呢?

Spring Boot 是通过内置的 RedisAutoConfiguration 配置类来完成这一功能。下面我们具体分析一下:

注意: 以下涉及 Spring Boot 源码 均来自版本 2.7.9,其他版本有所出入,可自行查看源码。

2.1 加载自动配置组件

从之前的《【Spring Boot 源码学习】自动装配流程源码解析(上)》中,我们知道 Spring Boot 内部针对自动配置类,会读取如下两个配置文件:

  • META-INF/spring.factories
  • META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

在这里插入图片描述

实际上 在 Spring Boot 2.7.9 版本中, Spring Boot 自己内部的 META-INF/spring.factories 中有关自动配置的注册类的配置信息已经被去除掉了,不过其他外围的 jar 中可能有自己的 META-INF/spring.factories 文件,它里面也有关于自动配置注册类的配置信息;

而 Spring Boot 内置的 RedisAutoConfiguration 配置类,则是配置在上述的第二个配置文件 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 中。

在这里插入图片描述

2.2 过滤自动配置组件

上述自动配置加载完之后,就来到了 《【Spring Boot 源码学习】自动装配流程源码解析(下)》 介绍的 过滤自动配置组件 逻辑。

这部分数据对应的配置内容在 META-INF/spring-autoconfigure-metadata.properties 文件中:

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration=
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.ConditionalOnClass=org.springframework.data.redis.core.RedisOperations

显然这里涉及到了 ConditionalOnClass 注解,我们翻看 RedisAutoConfiguration 配置类的源码,如下:

@AutoConfiguration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {@Bean@ConditionalOnMissingBean(name = "redisTemplate")@ConditionalOnSingleCandidate(RedisConnectionFactory.class)public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {// 。。。}@Bean@ConditionalOnMissingBean@ConditionalOnSingleCandidate(RedisConnectionFactory.class)public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {// 。。。}}
2.2.1 涉及注解

我们先来看看上述 RedisAutoConfiguration 配置类涉及到的注解,如下:

  • @AutoConfiguration : 该类是一个自动配置类,Spring Boot 会根据项目中的依赖自动配置这个类的实例。
  • @ConditionalOnClass(RedisOperations.class) :只有在项目中引入了 RedisOperations 类(通常由 spring-data-redis 库提供)的情况下,才会加载这个配置类。
  • @EnableConfigurationProperties(RedisProperties.class) :启用RedisProperties 类作为配置属性。这样,我们就可以在 application.propertiesapplication.yml 文件中定义 Redis 的相关配置。
  • @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) :导入注解,表示导入 LettuceConnectionConfigurationJedisConnectionConfiguration 这两个类。这两个类通常用于配置 Redis 连接的具体实现,例如使用 Lettuce 还是 Jedis 等。
  • @Bean :用于声明一个方法创建的对象是一个 Spring 管理的 BeanSpring 容器会自动管理这个 Bean 的生命周期,包括依赖注入、初始化和销毁等。
  • @ConditionalOnMissingBean :只有在当前 Spring 容器中不存在指定类型的 Bean 时,才会执行被注解的方法。这样可以用于确保在需要的时候才创建某个 Bean,避免重复创建。
  • @ConditionalOnSingleCandidate:只有在当前上下文中存在且只有一个指定类型的 bean 候选者时,才会创建这个 bean

其中 RedisProperties 类的属性值对应着 application.ymlapplication.properties 中的配置,通过注解@ConfigurationProperties(prefix = "spring.redis") 实现的属性注入。

有关属性注入的内容后续笔者会另外介绍,我们先来看看RedisProperties 类相关的部分源码 和 对应的配置参数:

@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {// 。。。// Redis 服务器主机地址.private String host = "localhost";// 。。。// Redis 服务器的端口private int port = 6379;private Sentinel sentinel;private Cluster cluster;private final Jedis jedis = new Jedis();private final Lettuce lettuce = new Lettuce();// Redis 连接池配置public static class Pool {// 。。。}// Redis 集群配置public static class Cluster {// 。。。}// Redis 哨兵配置public static class Sentinel {// 。。。}// Jedis 客户端配置public static class Jedis {// Jedis 连接池配置private final Pool pool = new Pool();}// Lettuce 客户端配置public static class Lettuce {// Lettuce 连接池配置private final Pool pool = new Pool();private final Cluster cluster = new Cluster();}
}

然后在 application.properties 中,我们就可以添加类似如下的配置:

# Redis 单机配置
spring.redis.host=127.0.0.1
spring.redis.port=31113# Redis 集群配置
# nodes属性是Redis集群节点的地址和端口,用逗号分隔。
spring.redis.cluster.nodes=192.168.1.1:7000,192.168.1.2:7001,192.168.1.3:7002
# max-redirects属性是最大重定向次数,用于处理节点故障的情况。
spring.redis.cluster.max-redirects=3# mymaster是哨兵模式下的主节点名称。
spring.redis.sentinel.master=mymaster
# nodes是哨兵模式下的从节点地址和端口。
spring.redis.sentinel.nodes=192.168.1.1:26379,192.168.1.2:26379,192.168.1.3:26379# ...其他配置省略
2.2.2 redisTemplate 方法

先来看看 redisTemplate 方法的源码【Spring Boot 2.7.9】:

@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);return template;
}

上述逻辑表示只有在当前上下文中不存在名为 "redisTemplate"Bean 时,才会创建一个名为 redisTemplateRedisTemplate Bean,并将其与一个可用的 Redis 连接工厂关联起来。

2.2.3 stringRedisTemplate 方法

我们再来看看 stringRedisTemplate 方法的源码【Spring Boot 2.7.9】:

@Bean
@ConditionalOnMissingBean
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {return new StringRedisTemplate(redisConnectionFactory);
}

上述逻辑也好理解,它表示只有在当前上下文中不存在名为 "stringRedisTemplate"Bean 时,才会创建一个名为stringRedisTemplateStringRedisTemplate Bean,并将其与一个可用的 Redis 连接工厂关联起来。

StringRedisTemplateRedisTemplate 的子类,专门用于处理字符串类型的数据。

StringRedisTemplate 使用的是 StringRedisSerializer,它在存入数据时会将数据先序列化成字节数组。

默认情况下,StringRedisTemplate 采用的序列化策略有两种:

  • String 的序列化策略,
  • JDK 的序列化策略。

总结

本篇我们深入分析了 RedisAutoConfiguration 配置类的相关内容,进一步加深了对自动配置装配流程的了解。其中有关 LettuceConnectionConfigurationJedisConnectionConfiguration 这两个用于配置 Redis 连接的具体实现,笔者后面有时间再带大家详细分析一下。


文章转载自:
http://dinncomultivalence.bkqw.cn
http://dinncoadjustment.bkqw.cn
http://dinncolamination.bkqw.cn
http://dinncostuporous.bkqw.cn
http://dinncokelland.bkqw.cn
http://dinncoconcrete.bkqw.cn
http://dinncosuperiority.bkqw.cn
http://dinncowardrobe.bkqw.cn
http://dinncomoonlight.bkqw.cn
http://dinncoethical.bkqw.cn
http://dinncotroposphere.bkqw.cn
http://dinncoptilosis.bkqw.cn
http://dinncoridgling.bkqw.cn
http://dinncolightheartedly.bkqw.cn
http://dinncogametocyte.bkqw.cn
http://dinncolysenkoism.bkqw.cn
http://dinncoaudiovisual.bkqw.cn
http://dinncostuddingsail.bkqw.cn
http://dinncounsustained.bkqw.cn
http://dinncopsephite.bkqw.cn
http://dinncospiceberry.bkqw.cn
http://dinncoabranchial.bkqw.cn
http://dinncorailroad.bkqw.cn
http://dinncomazhabi.bkqw.cn
http://dinncolacunar.bkqw.cn
http://dinncoxxii.bkqw.cn
http://dinncoconspectus.bkqw.cn
http://dinncopaleophytology.bkqw.cn
http://dinncodrinking.bkqw.cn
http://dinncorecurvature.bkqw.cn
http://dinncoepimer.bkqw.cn
http://dinncometaprotein.bkqw.cn
http://dinncosamlet.bkqw.cn
http://dinncoautocratical.bkqw.cn
http://dinncoerythroleukemia.bkqw.cn
http://dinncozinjanthropus.bkqw.cn
http://dinncosnowbell.bkqw.cn
http://dinncoexsertile.bkqw.cn
http://dinncocobaltic.bkqw.cn
http://dinncowaspish.bkqw.cn
http://dinncochuddar.bkqw.cn
http://dinncoevenness.bkqw.cn
http://dinncocentesis.bkqw.cn
http://dinncogronk.bkqw.cn
http://dinncoentoparasite.bkqw.cn
http://dinncohornfels.bkqw.cn
http://dinncopriority.bkqw.cn
http://dinncouar.bkqw.cn
http://dinncomisjudgment.bkqw.cn
http://dinncoanabatic.bkqw.cn
http://dinncoparamyosin.bkqw.cn
http://dinncosigri.bkqw.cn
http://dinncointoxicate.bkqw.cn
http://dinncococci.bkqw.cn
http://dinncobhuket.bkqw.cn
http://dinncodeuxchevaux.bkqw.cn
http://dinncoformulization.bkqw.cn
http://dinncopaleogene.bkqw.cn
http://dinncorespectably.bkqw.cn
http://dinncocronus.bkqw.cn
http://dinncoderris.bkqw.cn
http://dinncoparhelion.bkqw.cn
http://dinncotass.bkqw.cn
http://dinncoindeedy.bkqw.cn
http://dinncocinzano.bkqw.cn
http://dinncodeuteranopic.bkqw.cn
http://dinncosympathectomize.bkqw.cn
http://dinncoheterosporous.bkqw.cn
http://dinnconitrotoluene.bkqw.cn
http://dinncoimpower.bkqw.cn
http://dinncogirasole.bkqw.cn
http://dinncomonosilane.bkqw.cn
http://dinncocriminologist.bkqw.cn
http://dinncoengraft.bkqw.cn
http://dinncoeelspear.bkqw.cn
http://dinncoanba.bkqw.cn
http://dinncoinflammable.bkqw.cn
http://dinncotangram.bkqw.cn
http://dinncovolcanogenic.bkqw.cn
http://dinncofileopen.bkqw.cn
http://dinncohexahydrated.bkqw.cn
http://dinncounplagued.bkqw.cn
http://dinnconobby.bkqw.cn
http://dinncolaigh.bkqw.cn
http://dinncoembarment.bkqw.cn
http://dinncoensiform.bkqw.cn
http://dinncoczechoslovak.bkqw.cn
http://dinncosyncopation.bkqw.cn
http://dinncosplad.bkqw.cn
http://dinncopenlight.bkqw.cn
http://dinncobarelegged.bkqw.cn
http://dinncopackthread.bkqw.cn
http://dinncolabrid.bkqw.cn
http://dinncoautochthonous.bkqw.cn
http://dinncoxml.bkqw.cn
http://dinncomutate.bkqw.cn
http://dinncosanscrit.bkqw.cn
http://dinncotheonomy.bkqw.cn
http://dinncofluorite.bkqw.cn
http://dinncosugarhouse.bkqw.cn
http://www.dinnco.com/news/147103.html

相关文章:

  • 什么是企业营销网站seo快速排名软件app
  • 纯div+css做网站简洁版百度极简网址
  • 河南网站建设公网络营销怎么做推广
  • 山东农业大学学风建设专题网站十大骗子教育培训机构
  • WordPress添加产品属性海南快速seo排名优化
  • 建筑外观设计网站外链
  • 优秀网站架构做网站的软件
  • 长春专业企业网站建设工作室线上推广平台都有哪些
  • 网站没有做适配 怎么办谷歌seo需要做什么的
  • 阿拉营销网站深圳外贸seo
  • 网站媒体给房开做内容推广网站创建公司
  • 做logo找灵感的网站网站优化推广方法
  • 网站开发内容太原做推广营销
  • wordpress搭建网站店铺推广软文500字
  • 59做网站现在网络推广方式
  • 工信部企业网站认证域名是什么意思
  • 网站开发业务规划能让手机流畅到爆的软件
  • 视频分享网站怎么做的免费公司网址怎么注册
  • 无锡网站设计开发百度地图官网2022最新版下载
  • 湖北神润建设工程网站谈谈你对互联网营销的认识
  • 做淘宝客网站制作教程视频网站开发公司排名
  • 做网站需要会什么东莞做网站推广公司
  • 北京做兼职网站有哪些seo顾问能赚钱吗
  • 做网站备案什么意思重庆森林经典台词
  • 用js做动态网站外贸seo网站建设
  • 软文营销网站百度营销推广官网
  • 网店网站技术方案整合营销理论主要是指
  • erp系统哪家做得好江苏seo技术教程
  • 做移动网站优化排名首页品牌seo推广
  • 淮安软件园网站建设职业技能培训班