SpringBoot 自动装配原理解析

本文深入解读SpringBoot中的自动装配机制,重点讲解了@SpringBootApplication注解背后的‘三体’结构,以及@EnableAutoConfiguration如何配合SpringFactoriesLoader实现智能配置。以Redis配置为例,阐述了自动装配的过程,包括寻找配置类、依赖检查和默认配置。

自动装配是 Spring Boot 的核心部分,也是 Spring Boot 功能的基础,正是由于自动装配,才 将我们从 Bean 的繁复配置中解脱出来。那么 Spring Boot 中的自动装配指的是什么?我们继 续以 Spring MVC 为例,不使用 Spring Boot 时,我们可能需要配置视图解析器,文件解析器, 请求适配器等等各种 Bean,如果在使用数据库,redis,还需要配置数据库、redis 相关 Bean。

1、从@SpringBoot Application(啊不雷可雷神) 启动注 解入手

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
 @Filter(type = FilterType.CUSTOM, classes =
TypeExcludeFilter.class),
 @Filter(type = FilterType.CUSTOM, classes =
AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
 @AliasFor(annotation = EnableAutoConfiguration.class)
 Class<?>[] exclude() default {};
 @AliasFor(annotation = EnableAutoConfiguration.class)
 String[] excludeName() default {};
//根据包路径扫描
 @AliasFor(annotation = ComponentScan.class, attribute =
"basePackages")
 String[] scanBasePackages() default {};
//直接根据 class 类扫描
 @AliasFor(annotation = ComponentScan.class, attribute =
"basePackageClasses")
 Class<?>[] scanBasePackageClasses() default {};
}
初看@SpringBootApplication 有很多的注解组成,其实归纳就是一个"三体"
构,重要的只有三个 Annotation:

@Configuration(康飞的累神)(@SpringBootConfiguration 实质就是一个@Configuration)

@EnableAutoConfiguration(昂买的度 康飞的累神)

@ComponentScan(康抛闹的死改)

 也就是说我们在开发的时候,加上上面的上个注解会等同于加上
@SpringBootApplication 注解
(1)@Configuration 注解

 这个注解实际上就是代表了一个配置类,相当于一个 beans.xml 文件
(2)@ComponentScan(康抛闹的死改)

 @ComponentScan 的功能其实就是自动扫描并加载符合条件的组件或 bean
定义,最终将这些 bean 定义加载到容器中
(3)@EnableAutoConfiguration(昂买的度 康飞的累神)

 在 spring 中有关于@Enablexxx 的注解是开启某一项功能的注解,比如
@EnableScheduling 表示开启 spring 的定时任务。其原理是借助@Import(in抛的)
的帮助,将所有符合自动配置条件的 bean 定义加载到 Ioc 容器。
 EnableAutoConfiguration 代表开启 springboot 的自动装配

源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
 String ENABLED_OVERRIDE_PROPERTY =
"spring.boot.enableautoconfiguration";
//按类型排序不需要自动装配的类
 Class<?>[] exclude() default {};
//按名称排除不需要自动装配的类
 String[] excludeName() default {};
}
从源码中可以知道,最关键的要属
@Import(EnableAutoConfigurationImportSelector.class),借助
EnableAutoConfigurationImportSelector(昂买的度 康飞的累神 in抛可来的),@EnableAutoConfiguration(昂买的度 康飞的累神) 可以帮
助 SpringBoot 应用将所有符合条件的@Configuration(康飞的累神) 配置都加载到当前
SpringBoot 创建并使用的 IoC 容器。同时借助于 Spring 框架原有的一个工具类:
SpringFactoriesLoader(s斌坏的瑞猫都),@EnableAutoConfiguration 

(昂买的度 康飞的累神)就可以实现智能的自动配
置。
//从这里可以看出该类实现很多的 xxxAware 和 DeferredImportSelector,所有
的 aware 都优先于 selectImports(s来的抛人的)
//方法执行,也就是说 selectImports 方法最后执行,那么在它执行的时候所有
需要的资源都已经获取到了

AutoConfigurationImportSelector(奥都 康非的累神 in抛的 s来挺)源码

public class AutoConfigurationImportSelector implements 
DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware, Ordered {

public String[] selectImports(AnnotationMetadata annotationMetadata) {
 if (!this.isEnabled(annotationMetadata)) {
 return NO_IMPORTS;
 } else {
1 加载 META-INF/spring-autoconfigure-metadata.properties 文件
 AutoConfigurationMetadata autoConfigurationMetadata =
AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
2 获取注解的属性及其值(PS:注解指的是@EnableAutoConfiguration 注解)
 AnnotationAttributes attributes =
this.getAttributes(annotationMetadata);
3.在 classpath 下所有的 META-INF/spring.factories(坏的瑞死) 文件中查找
org.springframework.boot.autoconfigure.EnableAutoConfiguration 的值,
并将其封装到一个 List 中返回
 List<String> configurations =
this.getCandidateConfigurations(annotationMetadata, attributes);
//4.对上一步返回的 List 中的元素去重、排序
 configurations = this.removeDuplicates(configurations);
//5.依据第 2 步中获取的属性值排除一些特定的类
 Set<String> exclusions =
this.getExclusions(annotationMetadata, attributes);
//6 对上一步中所得到的 List 进行过滤,过滤的依据是条件匹配。这里用到的
过滤器是
//org.springframework.boot.autoconfigure.condition.OnClassCondition
最终返回的是一个 ConditionOutcome[](k的死out 上)
//数组。(PS:很多类都是依赖于其它的类的,当有某个类时才会装配,所以这
次过滤的就是根据是否有某个
//class 进而决定是否装配的。这些类所依赖的类都写在
META-INF/spring-autoconfigure-metadata.properties 文件里)
 this.checkExcludedClasses(configurations, exclusions);
 configurations.removeAll(exclusions);
 configurations = this.filter(configurations,
autoConfigurationMetadata);
 this.fireAutoConfigurationImportEvents(configurations,
exclusions);
 return StringUtils.toStringArray(configurations);
 }
 }
 protected List<String> getCandidateConfigurations(AnnotationMetadata 
metadata, AnnotationAttributes attributes) {
 List<String> configurations =
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderF
actoryClass(), 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;
 }

}

SpringFactoriesLoader(spring坏的瑞猫都) 中加载配置,SpringFactoriesLoader 属于 Spring 框架私有的一种扩展方案,其主要功能就是从指定的配置文件 META-INF/spring.factories(坏的瑞死) 加载配置,即根据 @EnableAutoConfiguration(昂买的度 康飞的累神) 的完整类名 org.springframework.boot.autoconfigure.EnableAutoConfiguration 作为查找的 Key,获取对应的一组@Configuration(康飞的累神) 类

SpringFactoriesLoader(spring坏的瑞猫都)源码

public abstract class SpringFactoriesLoader {
public static final String FACTORIES_RESOURCE_LOCATION =
"META-INF/spring.factories";
private static Map<String, List<String>> loadSpringFactories(@Nullable
ClassLoader classLoader) {
 MultiValueMap<String, String> result = cache.get(classLoader);
 if (result != null)
 return result;
 try {
 Enumeration<URL> urls = (classLoader != null ?
 
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
 
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
 result = new LinkedMultiValueMap<>();
 while (urls.hasMoreElements()) {
 URL url = urls.nextElement();
 UrlResource resource = new UrlResource(url);
 Properties properties =
PropertiesLoaderUtils.loadProperties(resource);
 for (Map.Entry<?, ?> entry : properties.entrySet()) {
 List<String> factoryClassNames = Arrays.asList(
 
StringUtils.commaDelimitedListToStringArray((String)
entry.getValue()));
 result.addAll((String) entry.getKey(),
factoryClassNames);
 }
 }
 cache.put(classLoader, result);
 return result;
 }
 catch (IOException ex) {
 throw new IllegalArgumentException("Unable to load 
factories from location ["
 +
 FACTORIES_RESOURCE_LOCATION + "]", ex);
 }
 }

@EnableAutoConfiguration(昂买的度 康飞的累神) 作用就是从 classpath 中搜寻所有的 META-INF/spring.factories(坏的瑞死) 配置文件,并将其中 org.springframework.boot.autoconfigure.EnableutoConfiguration 对应的 配置项通过反射(Java Refletion)实例化为对应的标注了@Configuration 的 JavaConfig(康非的) 形式的 IoC 容器配置类,然后汇总为一个并加载到 IoC 容器。这些 功能配置类要生效的话,会去 classpath 中找是否有该类的依赖类(也就是 pom.xml 必须有对应功能的 jar 包才行)并且配置类里面注入了默认属性值类, 功能类可以引用并赋默认值。生成功能类的原则是自定义优先,没有自定义时才 会使用自动装配类。

所以功能类能生效需要的条件:

(1)spring.factories 里面有这个类的 配置类(一个配置类可以创建多个围绕该功能的依赖类)

(2)pom.xml 里面需要有对应的 jar 包

自动装配案例说明以 Redis 为例

从 spring-boot-autoconfigure.jar/META-INF/spring.factories 中获取
redis 的相关配置类全限定名(有 120 多个的配置类)RedisAutoConfiguration,
一般一个功能配置类围绕该功能,负责管理创建多个相关的功能类,比如
RedisAutoConfiguration 负责:JedisConnectionFactory、RedisTemplate、
StringRedisTemplate 这 3 个功能类的创建
spring.factories 中的 redis 配置类
2、RedisAutoConfiguration 配置类生效的一个条件是在 classpath 路径下有
RedisOperations 类存在,因此 springboot 的自动装配机制会会去 classpath
下去查找对应的 class 文件。
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class,
JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {


}
3.如果 pom.xml 有对应的 jar 包,就能匹配到对应依赖 class,
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
 
 4、匹配成功,这个功能配置类才会生效,同时会注入默认的属性配置类
@EnableConfigurationProperties(RedisProperties.class)
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
 private int database = 0;
 private String url;
 private String host = "localhost";
 private String password;
 private int port = 6379;
 5.Redis 功能配置里面会根据条件生成最终的 JedisConnectionFactory、
RedisTemplate,并提供了默认的配置形式@ConditionalOnMissingBean(name = 
"redisTemplate")
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class,
JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
 @Bean
//用户没定义就使用默认的
 @ConditionalOnMissingBean(name = "redisTemplate")
 public RedisTemplate<Object, Object> redisTemplate(
 RedisConnectionFactory redisConnectionFactory) throws 
UnknownHostException {
 RedisTemplate<Object, Object> template = new RedisTemplate<>();
 template.setConnectionFactory(redisConnectionFactory);
 return template;
 }
 @Bean
 @ConditionalOnMissingBean(StringRedisTemplate.class)
 public StringRedisTemplate stringRedisTemplate(
 RedisConnectionFactory redisConnectionFactory) throws 
UnknownHostException {
 StringRedisTemplate template = new StringRedisTemplate();
 template.setConnectionFactory(redisConnectionFactory);
 return template;
 }
}

6.最终创建好的默认装配类,会通过功能配置类里面的 @Bean 注解,注入到 IOC
当中
7.用户使用,当用户在配置文件中自定义时候就会覆盖默认的配置
@ConditionalOnMissingBean(name = "redisTemplate")

自动依赖过程总结

1.通过各种注解实现了类与类之间的依赖关系,容器在启动的时候 Application(啊不瑞k神).run,会调用 EnableAutoConfigurationImportSelector((昂买的度 康飞的累神 in抛可来的)).class 的 selectImports(s来的抛人的) 方法(其实是其父类的方法)--这里需要注意,调用这个方法之 前发生了什么和是在哪里调用这个方法需要进一步的探讨

2.selectImports (s来的抛人的)方法最终会调用 SpringFactoriesLoader(spring坏的瑞猫都).loadFactoryNames(奥楼 坏的瑞) 方法来获取一个全面的常用 BeanConfiguration 列表

3.loadFactoryNames(奥楼 坏的瑞) 方法会读取 FACTORIES_RESOURCE_LOCATION(也就是 spring-boot-autoconfigure.jar 下面的 spring.factories),获取到所有的 Spring 相关的 Bean 的全限定名 ClassName,大概 120 多个

4.selectImports(s来的抛人的) 方法继续调用 filter(飞有的)(configurations, autoConfigurationMetadata);这个时候会根据这些 BeanConfiguration 里面的 条件,来一一筛选,最关键的是 @ConditionalOnClass(k的醒热康的坏死),这个条件注解会去 classpath 下查找,jar 包里面是否 有这个条件依赖类,所以必须有了相应的 jar 包,才有这些依赖类,才会生成 IOC 环境需要的一些默认配置 Bean

5.最后把符合条件的 BeanConfiguration 注入默认的 EnableConfigurationPropertie(in的博客 飞的雷神 抛的in) 类里面的属性值,并且注入到 IOC 环境当使用

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、pandas是什么?

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、使用步骤

1.引入库

代码如下(示例):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context

2.读入数据

代码如下(示例):

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

该处使用的url网络请求的数据。


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
`

<think>我们正在讨论Gemini GenAI SDK中的聊天历史功能。由于这是一个特定的技术问题,我将根据一般性的知识和常见的SDK设计模式来回答,因为Gemini GenAI SDK的具体实现细节可能随时间变化,且我没有实时的访问权限。但我会尽量提供通用且可靠的指导。 聊天历史功能通常是指在一个持续对话中,模型能够记住之前的对话内容,从而在后续的回复中保持上下文。在Gemini GenAI SDK中,实现聊天历史管理通常涉及以下几个关键点: 1. **消息传递结构**:通常,每次调用模型时,我们需要传递整个对话历史(包括用户的消息和模型的回复)作为上下文。这样模型才能基于整个对话历史生成回复。 2. **消息格式**:每条消息通常包含角色(如"user"或"model")和内容(文本)。例如: ```json [ {"role": "user", "content": "你好"}, {"role": "model", "content": "你好!有什么可以帮助你的吗?"}, {"role": "user", "content": "告诉我Gemini是什么?"} ] ``` 3. **管理对话历史**:开发者需要维护一个消息列表,每次用户发送新消息时,将新消息添加到历史列表中,然后调用模型生成回复,再将模型的回复也添加到历史列表中。注意,历史记录的长度不能超过模型的最大上下文长度(通常有token限制)。 4. **截断历史记录**:当对话历史很长时,需要截断以避免超过token限制。常见的策略是保留最近的几条消息,或者删除中间的一些消息但保留开头(如系统指令)和最近的对话。 5. **Gemini SDK中的具体实现**:在Gemini SDK中,可能会有一个`ChatSession`类来管理对话状态。例如: ```python import google.generativeai as genai genai.configure(api_key="YOUR_API_KEY") model = genai.GenerativeModel(&#39;gemini-pro&#39;) chat = model.start_chat(history=[]) # 初始历史为空 response = chat.send_message("你好") print(response.text) # 模型的回复 # 继续对话 response = chat.send_message("告诉我Gemini是什么?") print(response.text) # 查看历史 for message in chat.history: print(message.role, message.parts[0].text) ``` 在上面的例子中,`chat`对象会自动维护历史记录。每次调用`send_message`后,用户的消息和模型的回复都会被添加到`chat.history`中。 6. **常见问题**: - **上下文丢失**:如果发现模型没有记住之前的对话,请检查是否在每次发送消息时都使用了同一个`ChatSession`实例,并且历史记录是否正确传递。 - **token限制**:当历史记录很长时,可能会超过模型的最大token限制(例如,gemini-pro模型的最大上下文长度是30720个token)。此时,需要手动截断历史记录(如保留最近的几条消息)或者使用更高级的摘要技术。 - **历史记录存储**:如果需要长期存储对话,可以将历史记录保存到数据库,并在下次对话时重新加载。但要注意,重新加载后,需要确保整个历史记录不超过token限制。 7. **重置历史**:如果需要开始一个新的对话,可以创建一个新的`ChatSession`实例。 8. **系统指令**:在对话开始时,可以设置一个系统指令(system instruction)来指导模型的行为。例如: ```python model = genai.GenerativeModel(&#39;gemini-pro&#39;, system_instruction="你是一个专业的客服助手。") ``` 以上是关于Gemini GenAI SDK中聊天历史功能的基本实现和常见问题处理。具体细节请参考官方文档。 由于这是一个技术性较强的问题,我生成的相关问题将围绕使用中的常见场景和问题: §§相关问题§§ 1. 如何在Gemini GenAI SDK中重置聊天历史? 2. 当对话历史很长时,如何避免超过模型的token限制? 3. 如何将聊天历史保存到数据库并在下次对话时恢复? 4. Gemini模型支持的最大上下文长度是多少? 5. 在Gemini SDK中,如何设置系统指令来指导模型的行为?</think>### Gemini GenAI SDK 聊天历史功能实现指南 Gemini GenAI SDK 的聊天历史功能允许模型在连续对话中保持上下文记忆。以下是关键实现步骤和常见问题解决方案: #### 一、核心实现方法 1. **历史记录传递** 每次请求需将完整对话历史作为上下文传入: ```python from google.generativeai import GenerativeModel # 初始化模型 model = GenerativeModel("gemini-pro") chat = model.start_chat(history=[]) # 初始历史为空 # 首次交互 response = chat.send_message("解释量子力学") print(response.text) # 模型回复 # 后续交互(自动携带历史) response = chat.send_message("用简单例子说明") print(response.text) # 基于上下文的回复 ``` 2. **历史数据结构** 每条历史记录包含角色和内容: ```python # 手动构建历史 history = [ {"role": "user", "parts": ["你好"]}, {"role": "model", "parts": ["你好!有什么可以帮助你?"]} ] ``` 3. **历史管理技巧** - **上下文长度限制**:Gemini 1.5 Pro 支持最大 1M token 上下文[^1] - **历史截断策略**: ```python # 保留最近10轮对话 if len(chat.history) > 20: # 10轮对话=20条消息 chat.history = chat.history[-20:] ``` #### 二、常见问题解决 1. **上下文丢失问题** **症状**:模型忘记之前的对话 **解决方案**: - 检查是否每次调用都传递了完整 `chat.history` - 验证 token 计数是否超限: ```python import tiktoken encoder = tiktoken.encoding_for_model("gpt-4") tokens = encoder.encode(str(chat.history)) print(len(tokens)) # 需小于模型上限 ``` 2. **历史记录污染** **场景**:用户输入包含敏感数据 **清理方法**: ```python def clean_history(history): return [msg for msg in history if not is_sensitive(msg["parts"][0])] ``` 3. **性能优化** - 使用 `stream=True` 流式传输减少延迟 - 对长历史启用摘要功能: ```python summary = model.generate_content("总结对话要点: " + str(history[-5:])) new_history = [{"role": "system", "parts": [summary.text]}] ``` #### 三、最佳实践 1. **混合历史策略** ```mermaid graph LR A[新对话] --> B{历史长度>阈值?} B -->|是| C[摘要前5轮+完整后5轮] B -->|否| D[使用完整历史] ``` 2. **元数据存储** 建议将历史记录与会话元数据(如 session_id)共同存储: ```python # Firebase 存储示例 firestore.collection("chats").document(session_id).set({ "history": chat.history, "last_updated": firestore.SERVER_TIMESTAMP }) ``` > **重要提示**:Gemini 1.5 支持原生文件处理(PDF/Word),可将文档作为历史上下文传入[^1]: > ```python > response = chat.send_message( > "总结此文档", > file=open("report.pdf", "rb") > ) > ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值