PropertyResolver源码分析

master
xuchengsheng 2023-11-30 16:06:43 +08:00
parent 63686a77e5
commit 2977de7fcf
10 changed files with 431 additions and 372 deletions

View File

@ -1,370 +0,0 @@
<div align="center">
<img alt="logo" src="image/banner.png" style="height: 80px">
</div>
<div align="center">
<h2>深入Spring从源码开始</h2>
<h4>探索Java最受欢迎的框架理解它的内部机制带大家从入门到精通。</h4>
</div>
<p align="center">
<a href="https://github.com/xuchengsheng/spring-reading/stargazers"><img src="https://img.shields.io/github/stars/xuchengsheng/spring-reading?logo=github&logoColor=%23EF2D5E&label=Stars&labelColor=%23000000&color=%23EF2D5E&cacheSeconds=3600" alt="Stars Badge"/></a>
<a href="https://github.com/xuchengsheng"><img src="https://img.shields.io/github/followers/xuchengsheng?label=Followers&logo=github&logoColor=%23FC521F&labelColor=%231A2477&color=%23FC521F&cacheSeconds=3600" alt="Follow Badge"></a>
<a href="https://github.com/xuchengsheng/spring-reading/fork"><img src="https://img.shields.io/github/forks/xuchengsheng/spring-reading?label=Forks&logo=github&logoColor=%23F2BB13&labelColor=%23BE2323&color=%23F2BB13" alt="Fork Badge"></a>
<a href="https://github.com/xuchengsheng/spring-reading/watchers"><img src="https://img.shields.io/github/watchers/xuchengsheng/spring-reading?label=Watchers&logo=github&logoColor=%23FF4655&labelColor=%234169E1&color=%23FF4655&cacheSeconds=3600" alt="Watchers Badge"></a>
</p>
<p align="center">
<img src="https://visitor-badge.lithub.cc/badge?page_id=github.com/xuchengsheng&left_text=Visitors" alt="Visitor Badge"/>
<img src="https://img.shields.io/badge/WeChat-xcs19930428-%2307C160?logo=wechat" alt="Wechat Badge"/>
<a href="https://blog.csdn.net/duzhuang2399"><img src="https://img.shields.io/badge/dynamic/xml?url=https%3A%2F%2Fblog.csdn.net%2Fduzhuang2399&query=%2F%2F*%5B%40id%3D%22userSkin%22%5D%2Fdiv%5B1%5D%2Fdiv%5B2%5D%2Fdiv%5B1%5D%2Fdiv%2Fdiv%5B2%5D%2Fdiv%5B1%5D%2Fdiv%5B1%5D%2Fdiv%5B2%5D%2Fspan&logo=C&logoColor=red&label=CSDN&color=red&cacheSeconds=3600" alt="CSDN Badge"></a>
</p>
<p align="center">
<a href="#技术">技术</a>
|
👋 <a href="#简介">简介</a>
|
🍵 <a href="#为何做Spring源码分析">Why</a>
|
🙏 <a href="#顺手点个星">点个星</a>
|
🌱 <a href="#spring-源码阅读系列">Spring源码</a>
|
💬 <a href="#与我联系">联系我</a>
|
<a href="#欢迎贡献">贡献</a>
|
🔄 <a href="#持续更新中">更新</a>
|
💻 <a href="#我的-github-统计">统计</a>
</p>
---
## ⚡技术
<div align="left">
<img src="https://img.shields.io/badge/Java-1.8%2B-%23437291?logo=openjdk&logoColor=%23437291"/>
<img src="https://img.shields.io/badge/Spring-5.3.10-%23437291?logo=Spring&logoColor=%236DB33F&color=%236DB33F"/>
<img src="https://img.shields.io/badge/SpringBoot-2.5.5-%23437291?logo=SpringBoot&logoColor=%236DB33F&color=%236DB33F"/>
<img src="https://img.shields.io/badge/Maven-3.6.3-%23437291?logo=Apache%20Maven&logoColor=%23C71A36&color=%23C71A36"/>
<img src="https://img.shields.io/badge/JSR-330-%2366CCFF?logo=OpenJDK&logoColor=%2366CCFF&color=%2366CCFF"/>
<img src="https://img.shields.io/badge/JSR-250-%23FF9900?logo=OpenJDK&logoColor=%23FF9900&color=%23FF9900"/>
</div>
## 👋简介
大家好呀我是Lex👨💻。我是一名拥有8年经验的Java 后端开发人员👨‍💼,也是一个对 Spring 框架充满热情❤️的程序员。为了帮助那些希望深入了解 Spring 框架的程序员们🧑‍💻,我创建了这个 “Spring 源码阅读系列”📖。通过这个系列,我希望能够与你们共同探索 Spring 的内部工作机制⚙️。如果您有同样的兴趣或问题🤔,请联系我📩!
## 🍵**为何做Spring源码分析**
在我作为框架研发的开发者👨🔬的工作中我经常遇到需要深入理解和调整框架行为的情况🔧。这些工作不只是简单地使用框架的API更多地是需要对框架的内部工作方式有详细的了解🔍。虽然Github上有关于Spring的简化版本📦这些对于入门学习确实有很大的帮助✅但当涉及到真实的项目应用时与真正的Spring框架还是有很大的差异❌。因此我开始深入研究Spring的源码希望能够更透彻地理解其内部的工作机制以便更好地应用到我的实际工作中🧰。分享我的源码分析📝也是为了给那些希望真正理解Spring而不仅仅是使用它的开发者提供一些参考和帮助🙌。
## 🙏顺手点个星
亲爱的朋友们👥我真的花了很多心思💭去研究和整理这个“Spring 源码阅读系列”📘。如果你觉得这东西还不错👍,或者给你带来了一点点帮助🤗,麻烦点一下星星吧🌟。这真的对我意义重大🎖,每一颗星✨都能让我觉得所有的努力都是值得的💪。我知道这是小事一桩,但你的那一下点击🖱,对我来说就是最好的鼓励🎉。无论如何,都要感谢你抽时间🕰阅读我的内容,真的很感激🙏!
## 🌱Spring 源码阅读系列
#### Spring IOC
- 资源加载与访问
- [`Resource`](spring-resources/spring-resource/README.md):抽象接口,表示文件、类路径等,用于访问不同来源的资源。
- [`ResourceLoader`](spring-resources/spring-resource-resourceLoader/README.md):资源获取核心接口,实现统一加载不同位置资源的策略。
- [`DocumentLoader`](spring-resources/spring-resource-documentLoader/README.md)XML文档加载解析核心接口支持后台自动配置Spring应用。
- 元数据与过滤
- [`MetadataReader`](spring-metadata/spring-metadata-metadataReader/README.md)类元数据获取核心支持组件扫描、条件化注解、AOP等高级功能。
- [`AnnotationMetadata`](spring-metadata/spring-metadata-annotationMetadata/README.md):动态获取和操作运行时类注解信息
- [`TypeFilter`](spring-metadata/spring-metadata-typeFilter/README.md):组件扫描时自定义类筛选,支持复杂条件和精确过滤。
- [`Condition`](spring-metadata/spring-metadata-condition/README.md)条件判断决定Bean创建和配置的灵活机制。
- Bean定义与注册
- [`BeanDefinition`](spring-beans/spring-bean-beanDefinition/README.md)详细描述Bean支持依赖注入、AOP、作用域控制等核心功能。
- [`BeanDefinitionHolder`](spring-beans/spring-bean-beanDefinitionHolder/README.md)管理和操作BeanDefinition的关键类。
- [`BeanDefinitionRegistry`](spring-beans/spring-bean-beanDefinitionRegistry/README.md)Bean定义注册管理关键接口处理Bean元数据。
- Bean定义读取与扫描
- [`XmlBeanDefinitionReader`](spring-beans/spring-bean-xmlBeanDefinitionReader/README.md)加载解析XML配置构建IOC容器注册Bean定义。
- [`PropertiesBeanDefinitionReader`](spring-beans/spring-bean-propertiesBeanDefinitionReader/README.md)属性文件加载解析为Bean定义提升配置灵活性和可扩展性。
- [`GroovyBeanDefinitionReader`](spring-beans/spring-bean-groovyBeanDefinitionReader/README.md)Groovy脚本解析为Bean定义支持应用程序上下文配置。
- [`AnnotatedBeanDefinitionReader`](spring-beans/spring-bean-annotatedBeanDefinitionReader/README.md)注解配置自动扫描注册Spring组件简化Bean定义配置。
- [`ClassPathBeanDefinitionScanner`](spring-beans/spring-bean-classPathBeanDefinitionScanner/README.md)类路径扫描注册Spring Bean支持自动装配提高可维护性和扩展性。
- Bean定义导入与组合
- `ImportBeanDefinitionRegistrar`:运行时动态注册 Bean实现灵活配置扩展配置类功能。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `ImportSelector`:运行时动态导入配置类,实现条件选择和灵活配置。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `DeferredImportSelector`:运行时动态导入配置,支持条件选择和按组别延迟加载。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- Bean工厂
- `BeanFactory`Spring的核心接口提供对Bean的配置、创建、管理的基本功能。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `ListableBeanFactory`支持按类型获取Bean的集合。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `HierarchicalBeanFactory`支持父子容器关系实现Bean定义的层次结构。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `ConfigurableBeanFactory`提供对BeanFactory配置的扩展如属性编辑器、作用域等。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- 基于Java的配置
- `ConfigurationClassPostProcessor`:处理@Configuration注解关键容器启动后置处理器。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `ConfigurationClassParser`:解析@Configuration提取Config信息支持@Bean和条件化配置。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- 容器上下文
- `ClassPathXmlApplicationContext`类路径classpath加载 XML 配置文件的上下文。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `FileSystemXmlApplicationContext`:文件系统加载 XML 配置文件的上下文。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `AnnotationConfigApplicationContext`:注解配置类中加载配置信息的上下文。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `GenericApplicationContext`支持多种配置方式XML、注解、手动注册的上下文。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- Bean生命周期
- `Bean的定义解析`加载与解析配置文件注册解析Bean定义类名、作用域、属性等。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- [`Bean的初始化过程`](spring-core/spring-core-getBean/README.md)实例化、属性注入、Aware回调、后置处理器、初始化方法调用、Bean就绪。
- [`Bean的依赖解析过程`](spring-core/spring-core-resolveDependency/README.md):声明依赖,查找依赖,注入依赖,处理循环依赖,延迟依赖解析。
- `Bean的销毁过程`销毁方法调用接口回调后处理清理通知触发GC回收资源。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- Bean初始化与扩展点
- [`InitializingBean`](spring-interface/spring-interface-initializingBean/README.md)提供Bean初始化时执行自定义逻辑的接口。
- [`DisposableBean`](spring-interface/spring-interface-disposableBean/README.md)定义Bean销毁前执行清理操作的接口。
- [`BeanDefinitionRegistryPostProcessor`](spring-interface/spring-interface-beanDefinitionRegistryPostProcessor/README.md)在容器启动时对BeanDefinition进行动态修改或添加。
- [`BeanFactoryPostProcessor`](spring-interface/spring-interface-beanFactoryPostProcessor/README.md)在Bean实例化前对BeanFactory进行全局修改或配置。
- [`BeanPostProcessor`](spring-interface/spring-interface-beanPostProcessor/README.md)在Bean初始化前后进行自定义处理可影响所有Bean。
- [`InstantiationAwareBeanPostProcessor`](spring-interface/spring-interface-instantiationAwareBeanPostProcessor/README.md):提供更深层次的实例化和属性注入控制。
- [`DestructionAwareBeanPostProcessor`](spring-interface/spring-interface-destructionAwareBeanPostProcessor/README.md) 允许在Bean销毁前进行额外的清理操作。
- [`MergedBeanDefinitionPostProcessor`](spring-interface/spring-interface-mergedBeanDefinitionPostProcessor/README.md)在合并Bean定义时对BeanDefinition进行进一步处理。
- [`SmartInstantiationAwareBeanPostProcessor`](spring-interface/spring-interface-smartInstantiationAwareBeanPostProcessor/README.md):提供更智能的实例化控制。
- [`SmartInitializingSingleton`](spring-interface/spring-interface-smartInitializingSingleton/README.md)在所有单例Bean初始化完成后执行自定义逻辑。
- Aware接口系列
- [`BeanNameAware`](spring-aware/spring-aware-beanNameAware/README.md)让Bean获取自身在容器中的名字实现`setBeanName`方法。
- [`BeanClassLoaderAware`](spring-aware/spring-aware-beanClassLoaderAware/README.md)允许Bean获取其类加载器实现`setBeanClassLoader`方法。
- [`BeanFactoryAware`](spring-aware/spring-aware-beanFactoryAware/README.md)提供Bean获取所属的BeanFactory实现`setBeanFactory`方法。
- [`EnvironmentAware`](spring-aware/spring-aware-environmentAware/README.md)允许Bean获取应用程序环境配置实现`setEnvironment`方法。
- [`EmbeddedValueResolverAware`](spring-aware/spring-aware-embeddedValueResolverAware/README.md)允许Bean解析嵌入式值占位符实现`setEmbeddedValueResolver`方法。
- [`ResourceLoaderAware`](spring-aware/spring-aware-beanClassLoaderAware/README.md)允许Bean获取资源加载器实现`setResourceLoader`方法。
- [`ApplicationEventPublisherAware`](spring-aware/spring-aware-applicationEventPublisherAware/README.md)允许Bean发布应用程序事件实现`setApplicationEventPublisher`方法。
- [`MessageSourceAware`](spring-aware/spring-aware-messageSourceAware/README.md)允许Bean获取消息源实现`setMessageSource`方法。
- [`ApplicationStartupAware`](spring-aware/spring-aware-applicationStartupAware/README.md)允许Bean获取应用程序启动信息实现`setApplicationStartup`方法。
- [`ApplicationContextAware`](spring-aware/spring-aware-applicationContextAware/README.md)允许Bean获取应用程序上下文实现`setApplicationContext`方法。
- [`ImportAware`](spring-aware/spring-aware-importAware/README.md):允许被导入的配置类获取导入它的类的信息,实现`setImportMetadata`方法。
- 核心注解
- [`@Configuration`](spring-annotation/spring-annotation-configuration/README.md)声明类为配置类定义Bean和Bean之间的依赖关系。
- [`@ComponentScan`](spring-annotation/spring-annotation-componentScan/README.md):启用组件扫描,自动发现并注册标记为组件的类。
- [`@Bean`](spring-annotation/spring-annotation-bean/README.md)在配置类中声明方法返回Bean实例。
- [`@Import`](spring-annotation/spring-annotation-import/README.md)引入其他配置类将其Bean定义合并到当前容器。
- [`@PropertySource`](spring-annotation/spring-annotation-propertySource/README.md):指定属性文件,加载外部配置到环境中。
- [`@DependsOn`](spring-annotation/spring-annotation-dependsOn/README.md)指定Bean的依赖顺序确保特定Bean在其他Bean之前初始化。
- [`@Conditional`](spring-annotation/spring-annotation-conditional/README.md)根据条件决定是否创建Bean。
- [`@Lazy`](spring-annotation/spring-annotation-lazy/README.md)指定Bean的延迟初始化只有在首次使用时才创建。
- [`@Value`](spring-annotation/spring-annotation-value/README.md)注入简单值或表达式到Bean的字段或方法参数。
- [`@Autowired`](spring-annotation/spring-annotation-autowired/README.md)自动装配Bean依赖。
- `@Primary`指定在多个候选Bean中优先选择的首选Bean。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `@Description`为Bean提供描述性信息。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `@Role`为Bean提供角色提示用于区分相似类型的Bean。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `@Indexed` 标记Bean用于索引。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- `@Order`指定Bean的加载顺序。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- JSR规范
- [`@Inject`](spring-jsr/spring-jsr330-inject/README.md)JSR-330标准的依赖注入注解。
- [`@Named`](spring-jsr/spring-jsr330-named/README.md)JSR-330标准的命名注解。
- [`@Resource`](spring-jsr/spring-jsr250-resource/README.md)Java EE标准的资源注入注解。
- [`@Qualifier`](spring-jsr/spring-jsr330-qualifier/README.md)用于限定注入的Bean。
- [`@Scope`](spring-jsr/spring-jsr330-scope/README.md)指定Bean的作用域。
- [`@Singleton`](spring-jsr/spring-jsr330-singleton/README.md)指定Bean为单例。
- [`@PostConstruct`](spring-jsr/spring-jsr250-postConstruct/README.md):指定初始化方法。
- [`@PreDestroy`](spring-jsr/spring-jsr250-preDestroy/README.md):指定销毁方法。
- [`Provider`](spring-jsr/spring-jsr330-provider/README.md)ava标准库提供的通用Bean工厂接口。
#### Spring AOP
+ AOP 术语Aspect、Join point、Advice、Pointcut 等
+ Spring AOP 实现原理
+ 动态代理JDK 与 CGLIB
+ @AspectJ 支持与使用
+ 切点表达式解析
#### Spring 事件机制
+ 事件的发布与监听
+ 自定义事件
#### Spring 事务管理
+ Spring 事务管理介绍
+ 编程式与声明式事务
+ @Transactional 注解解析
+ 事务传播行为
+ 事务隔离级别
+ 事务管理器实现原理
#### Spring MVC
+ Spring MVC 流程
+ DispatcherServlet 的角色与工作原理
+ 控制器Controller的工作机制
+ 视图解析与渲染
+ 异常处理
+ RESTful 支持
#### Spring Boot
+ Spring Boot 与 Spring 的区别
+ 自动配置原理
+ Spring Boot starter 介绍
+ Spring Boot Actuator
#### Spring Cloud
- `@EnableDiscoveryClient`:启用服务发现客户端,用于将服务注册到服务注册中心(例如 Eureka
- `@EnableEurekaServer`:启用 Eureka 服务端,用于搭建服务注册中心。
- `@LoadBalanced`:启用负载均衡,通常用于 RestTemplate 和 WebClient使其具备负载均衡的能力。
- `@FeignClient`:声明一个声明式的 HTTP 客户端,简化了服务调用的过程。
- `@EnableCircuitBreaker`:启用断路器,用于防止分布式系统中的雪崩效应。
- `@HystrixCommand`:定义一个熔断器命令。
- `@EnableZuulProxy`:启用 Zuul API 网关代理。
- `@ZuulRoute`:用于配置 Zuul 路由。
- `@EnableConfigServer`:启用配置中心服务端。
- `@RefreshScope`:用于刷新配置,通常与 Spring Cloud Config 配合使用。
- `@EnableZipkinServer`:启用 Zipkin 服务器,用于分布式链路追踪。
- `@EnableBinding`:绑定消息通道,与 Spring Cloud Stream 配合使用。
- `@GlobalTransactional`:全局事务注解,与 Seata 等分布式事务框架配合使用。
- `@SentinelResource`Sentinel 限流和熔断注解。
- `@DubboTransported`:用于 Dubbo 服务的注解。
- `@NacosInjected`:用于注入 Nacos 相关的实例。
#### Spring 编程风格与设计模式
+ 设计模式在 Spring 源码中的应用
+ Spring 源码阅读技巧
## 💬与我联系
✉️ [Email](xuchengshengsuper@163.com) | 💬 [Issue](https://github.com/xuchengsheng/spring-reading/issues) | 🌐 [CSDN](https://blog.csdn.net/duzhuang2399?type=blog) Me about everything!
## ⛵欢迎贡献!
如果你发现任何错误🔍或者有改进建议🛠️,欢迎提交 issue 或者 pull request。你的反馈📢对于我非常宝贵💎
## 🔄持续更新中
为了给大家提供最新🌱、最有价值的内容💼,我会坚持每天更新这个仓库⏳。每一天,你都可以期待看到一些新的内容或者对已有内容的改进✨。如果你有任何建议或反馈📣,欢迎随时联系我📞。我非常珍视每一个反馈💌,因为这是我持续改进的动力🚀。
## 💻我的 GitHub 统计
[![Star History Chart](https://api.star-history.com/svg?repos=xuchengsheng/spring-reading&type=Date)](https://star-history.com/#xuchengsheng/spring-reading&Date)
## 🍱请我吃盒饭?
作者晚上还要写博客✍️,平时还需要工作💼,如果帮到了你可以请作者吃个盒饭🥡
<div>
<img alt="logo" src="image/WeChatPay.png" style="width: 370px;height: 400px">
<img alt="logo" src="image/Alipay.png" style="width: 370px;height: 400px">
</div>
## 👥加入我们
欢迎加入我们的群聊!一起探讨、分享和学习吧! 🌐
<div>
<img alt="logo" src="image/wechat-group.jpg" style="width: 344px;height: 483px">
</div>

View File

@ -104,7 +104,7 @@
- 属性解析和环境配置
- `PropertyResolver`:通用属性解析,获取配置值,处理属性缺失,简便灵活<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
- [`PropertyResolver`](spring-env/spring-env-propertyResolver/README.md):通用属性解析,获取配置值,处理属性缺失,简便灵活<img src="https://img.shields.io/badge/Level-%E7%AE%80%E5%8D%95-0099ff"></img>
- `Environment`:应用环境表示,提供属性访问,支持配置文件,实现动态配置。<img src="https://img.shields.io/badge/%E5%8D%B3%E5%B0%86%E6%9B%B4%E6%96%B0-339933"></img>
@ -253,7 +253,7 @@
- [`@PreDestroy`](spring-jsr/spring-jsr250-preDestroy/README.md):指定销毁方法。<img src="https://img.shields.io/badge/Level-%E4%B8%80%E8%88%AC-%23FF8C69"></img>
- [`Provider`](spring-jsr/spring-jsr330-provider/README.md)ava标准库提供的通用Bean工厂接口。<img src="https://img.shields.io/badge/Level-%E4%B8%80%E8%88%AC-%23FF8C69"></img>
- [`Provider`](spring-jsr/spring-jsr330-provider/README.md)Java标准库提供的通用Bean工厂接口。<img src="https://img.shields.io/badge/Level-%E4%B8%80%E8%88%AC-%23FF8C69"></img>
#### Spring AOP

View File

@ -30,6 +30,7 @@
<module>spring-beans</module>
<module>spring-context</module>
<module>spring-factory</module>
<module>spring-env</module>
</modules>
<dependencies>

22
spring-env/pom.xml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-reading</artifactId>
<groupId>com.xcs.spring</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-env</artifactId>
<packaging>pom</packaging>
<modules>
<module>spring-env-propertyResolver</module>
<module>spring-env-environment</module>
<module>spring-env-configurableEnvironment</module>
<module>spring-env-configurablePropertyResolver</module>
</modules>
</project>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-env</artifactId>
<groupId>com.xcs.spring</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-env-configurableEnvironment</artifactId>
</project>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-env</artifactId>
<groupId>com.xcs.spring</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-env-configurablePropertyResolver</artifactId>
</project>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-env</artifactId>
<groupId>com.xcs.spring</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-env-environment</artifactId>
</project>

View File

@ -0,0 +1,291 @@
## PropertyResolver
- [PropertyResolver](#propertyresolver)
- [一、基本信息](#一基本信息)
- [二、知识储备](#二知识储备)
- [三、基本描述](#三基本描述)
- [四、主要功能](#四主要功能)
- [五、接口源码](#五接口源码)
- [六、主要实现](#六主要实现)
- [七、最佳实践](#七最佳实践)
- [八、与其他组件的关系](#八与其他组件的关系)
- [九、常见问题](#九常见问题)
### 一、基本信息
✒️ **作者** - Lex 📝 **博客** - [掘金](https://juejin.cn/user/4251135018533068/posts) 📚 **源码地址** - [github](https://github.com/xuchengsheng/spring-reading)
### 二、知识储备
1. **Spring 属性占位符解析**
+ 理解 `${...}` 占位符的使用,这是 Spring 配置中的一个关键特性,允许从外部源(如配置文件、环境变量)动态注入属性值。
2. **Spring Environment 抽象**
+ 熟悉 `Environment` 接口及其如何与 `PropertyResolver` 协作,用于处理配置和属性值。这是理解属性解析和占位符处理的基础。
3. **属性文件和配置管理**
+ 了解如何在 Java 和 Spring 应用程序中使用和管理配置文件(例如 `.properties``.yml` 文件),以及如何在这些文件中使用占位符。
4. **Spring 表达式语言SpEL**
+ 虽然与 `PropertyResolver` 直接关系不大,但对 Spring 表达式语言的理解有助于深入理解在 Spring 中处理动态属性值的各种方式。
5. **环境配置和 Profile**
+ 理解 Spring Profiles 的概念和如何在不同环境(如开发、测试、生产)中使用不同的配置。
6. **泛型和类型转换**
+ 对 Java 泛型的了解对于理解 `PropertyResolver` 中的类型安全属性访问方法(如 `<T> T getProperty(String key, Class<T> targetType)`)很重要。
### 三、基本描述
`PropertyResolver` 接口是 Spring 框架的一个核心组件专注于提供一套灵活且强大的机制来处理应用程序配置属性。它定义了一系列方法用于访问和操纵来自各种源例如属性文件、环境变量、JVM 参数)的属性值。
### 四、主要功能
1. **获取属性值**
+ 通过 `getProperty(String key)` 方法可以获取给定键名的属性值。这是处理配置数据时最常用的功能。
2. **带默认值的属性获取**
+ 如果指定的属性键不存在,`getProperty(String key, String defaultValue)` 方法允许返回一个默认值。
3. **属性值类型转换**
+ `getProperty(String key, Class<T> targetType)``getProperty(String key, Class<T> targetType, T defaultValue)` 方法使得可以将属性值转换成指定的数据类型,如从字符串转换为整数或布尔值。
4. **检查属性存在性**
+ `containsProperty(String key)` 方法用于判断是否存在特定的属性键。
5. **获取必需属性**
+ `getRequiredProperty(String key)``getRequiredProperty(String key, Class<T> targetType)` 方法用于获取必须存在的属性值。如果属性不存在,这些方法会抛出异常。
6. **解析占位符**
+ `resolvePlaceholders(String text)` 方法支持解析字符串中的占位符,并用相应的属性值替换它们。这对于处理包含动态内容的配置文件非常有用。
### 五、接口源码
`PropertyResolver` 接口提供了一系列方法来处理属性值的解析,包括检查属性是否存在,获取属性值,支持默认值,以及类型转换,另外它还包含用于解析字符串中的占位符的方法,允许动态替换配置值。
```java
/**
* 用于针对任何底层源解析属性的接口。
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
* @see Environment
* @see PropertySourcesPropertyResolver
*/
public interface PropertyResolver {
/**
* 返回给定属性键是否可用于解析,即给定键的值是否不为 {@code null}。
*/
boolean containsProperty(String key);
/**
* 返回与给定键关联的属性值,或者如果无法解析该键,则返回 {@code null}。
* @param key 要解析的属性名称
*/
@Nullable
String getProperty(String key);
/**
* 返回与给定键关联的属性值,或者如果无法解析该键,则返回 {@code defaultValue}。
* @param key 要解析的属性名称
* @param defaultValue 如果找不到值,则返回的默认值
*/
String getProperty(String key, String defaultValue);
/**
* 返回与给定键关联的属性值,或者如果无法解析该键,则返回 {@code null}。
* @param key 要解析的属性名称
* @param targetType 期望的属性值类型
*/
@Nullable
<T> T getProperty(String key, Class<T> targetType);
/**
* 返回与给定键关联的属性值,或者如果无法解析该键,则返回 {@code defaultValue}。
* @param key 要解析的属性名称
* @param targetType 期望的属性值类型
* @param defaultValue 如果找不到值,则返回的默认值
*/
<T> T getProperty(String key, Class<T> targetType, T defaultValue);
/**
* 返回与给定键关联的属性值(永远不会是 {@code null})。
* @throws IllegalStateException 如果无法解析该键
*/
String getRequiredProperty(String key) throws IllegalStateException;
/**
* 返回与给定键关联的属性值,转换为给定的 targetType永远不会是 {@code null})。
* @throws IllegalStateException 如果无法解析给定键
*/
<T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException;
/**
* 解析给定文本中的 ${...} 占位符,将它们替换为 {@link #getProperty} 解析的相应属性值。
* 无法解析且没有默认值的占位符将被忽略并保持不变。
* @param text 要解析的字符串
* @return 解析后的字符串(永远不会是 {@code null}
* @throws IllegalArgumentException 如果给定的文本是 {@code null}
*/
String resolvePlaceholders(String text);
/**
* 解析给定文本中的 ${...} 占位符,将它们替换为 {@link #getProperty} 解析的相应属性值。
* 无法解析且没有默认值的占位符将导致抛出 IllegalArgumentException。
* @return 解析后的字符串(永远不会是 {@code null}
* @throws IllegalArgumentException 如果给定文本是 {@code null} 或任何占位符无法解析
*/
String resolveRequiredPlaceholders(String text) throws IllegalArgumentException;
}
```
### 六、主要实现
1. **`PropertySourcesPropertyResolver`**
+ 这是最常用的 `PropertyResolver` 实现之一。它使用 `PropertySource` 对象可能包含多个如环境变量、JVM 属性、配置文件等)来解析属性。
2. **`AbstractEnvironment`**
+ 虽然 `AbstractEnvironment` 本身不直接实现 `PropertyResolver` 接口,但它提供了访问 `PropertyResolver` 功能的接口。`AbstractEnvironment` 通过内部持有的 `PropertySourcesPropertyResolver` 实例来提供属性解析服务。
3. **`StandardEnvironment``ConfigurableEnvironment`**
+ 这些类扩展了 `AbstractEnvironment`,提供了更具体的环境配置。它们通过继承 `AbstractEnvironment` 间接提供 `PropertyResolver` 的功能。
### 七、最佳实践
下面代码演示了关于`PropertyResolver`接口 ,包括了获取属性值、检查属性存在性、处理默认值、获取必需属性以及解析占位符等多个方面。
```java
public class SimplePropertyResolverDemo {
public static void main(String[] args) {
// 创建属性源
Map<String, Object> properties = new HashMap<>();
properties.put("app.name", "Spring-Reading");
properties.put("app.version", "1.0.0");
properties.put("app.description", "This is a ${app.name} with version ${app.version}");
MapPropertySource propertySource = new MapPropertySource("myPropertySource", properties);
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(propertySource);
// 使用 PropertySourcesPropertyResolver
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
// 获取属性
String appName = propertyResolver.getProperty("app.name");
String appVersion = propertyResolver.getProperty("app.version");
System.out.println("获取属性 app.name: " + appName);
System.out.println("获取属性 app.version: " + appVersion);
// 检查属性是否存在
boolean containsDescription = propertyResolver.containsProperty("app.description");
boolean containsReleaseDate = propertyResolver.containsProperty("app.releaseDate");
System.out.println("是否包含 'app.description' 属性: " + containsDescription);
System.out.println("是否包含 'app.releaseDate' 属性: " + containsReleaseDate);
// 带默认值的属性获取
String appReleaseDate = propertyResolver.getProperty("app.releaseDate", "2023-11-30");
System.out.println("带默认值的属性获取 app.releaseDate : " + appReleaseDate);
// 获取必需属性
String requiredAppName = propertyResolver.getRequiredProperty("app.name");
System.out.println("获取必需属性 app.name: " + requiredAppName);
// 解析占位符
String appDescription = propertyResolver.resolvePlaceholders(properties.get("app.description").toString());
System.out.println("解析占位符 app.description: " + appDescription);
// 解析必需的占位符
String requiredAppDescription = propertyResolver.resolveRequiredPlaceholders("App Description: ${app.description}");
System.out.println("解析必需的占位符 : " + requiredAppDescription);
}
}
```
运行结果发现,可以看到如何在实际场景中使用 `PropertyResolver` 接口来灵活处理配置属性。
```java
获取属性 app.name: Spring-Reading
获取属性 app.version: 1.0.0
是否包含 'app.description' 属性: true
是否包含 'app.releaseDate' 属性: false
带默认值的属性获取 app.releaseDate : 2023-11-30
获取必需属性 app.name: Spring-Reading
解析占位符 app.description: This is a Spring-Reading with version 1.0.0
解析必需的占位符 : App Description: This is a Spring-Reading with version 1.0.0
```
### 八、与其他组件的关系
1. **`Environment` 抽象**
- `PropertyResolver``Environment` 接口的一部分,而 `Environment` 提供了一个更为全面的配置和属性管理接口。`Environment` 抽象了属性源Property Sources如系统环境变量、JVM 属性、配置文件等。
- 在实际使用中,当我们操作 `Environment` 对象时,我们也在使用 `PropertyResolver` 的功能,因为 `Environment` 继承了 `PropertyResolver` 接口。
2. **`PropertySource(s)``PropertySourcesPropertyResolver`**
- `PropertySource` 代表了一个属性的源头,比如一个 `.properties` 文件或者环境变量。`MutablePropertySources` 是一个包含多个 `PropertySource` 的容器。
- `PropertySourcesPropertyResolver``PropertyResolver` 的具体实现,它可以解析由一个或多个 `PropertySource` 提供的属性。
3. **占位符解析**
- `PropertyResolver` 提供了解析占位符(如 `${property.name}`)的能力,这在处理配置文件时特别有用。
- 它与 Spring 的 `PropertyPlaceholderConfigurer` 或者 `@Value` 注解协同工作,用于将配置文件中的属性值注入到 Spring 管理的 bean 中。
4. **配置类和注解**
- 在使用基于注解的配置类(如使用 `@Configuration` 注解的类)时,`PropertyResolver` 可以用来动态解析和注入属性值,特别是结合 `@PropertySource` 注解使用时。
- 例如,可以使用 `@Value("${property.name}")` 注解来将属性值注入到配置类的字段或方法中。
5. **Profile 管理**
- `PropertyResolver` 与 Spring Profiles 的概念紧密相连。通过 `PropertyResolver`,可以方便地访问和检查当前激活的 Profiles这对于根据不同环境开发、测试、生产等来加载特定的配置非常有用。
### 九、常见问题
1. **属性值未找到**
+ 如果尝试解析不存在的属性,可以通过提前使用 `containsProperty` 方法来检查属性是否存在,以避免问题。同时,确认属性名是否正确,并确保属性源已经包含了对应的属性。
2. **属性类型转换错误**
+ 在将属性值转换为不同的类型时,确保属性值的格式与目标类型兼容。如果格式不匹配,可以使用类型转换服务(如 Spring 的 `ConversionService`)进行显式转换。
3. **占位符未解析**
+ 如果属性值中的 `${...}` 占位符没有被正确解析,确保使用了合适的 `PropertyResolver` 实现(如 `PropertySourcesPropertyResolver`),并且相关的属性源已经包含了占位符引用的属性。
4. **环境相关属性处理**
+ 在处理不同环境(开发、测试、生产)下的属性时,可以使用 Spring Profiles 来定义环境特定的属性。启动应用时,确保激活了正确的 Profile。
5. **配置文件加载问题**
+ 如果属性文件没有被正确加载,检查属性文件的路径和格式是否正确,并确保在 Spring 配置类中使用了 `@PropertySource` 注解来指定属性文件。
6. **使用 `@Value` 注解注入属性时出错**
+ 在使用 `@Value` 注解注入属性值时,确保 `PropertyPlaceholderConfigurer``PropertySourcesPlaceholderConfigurer` 被正确配置并加载到 Spring 容器中。

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-env</artifactId>
<groupId>com.xcs.spring</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-env-propertyResolver</artifactId>
</project>

View File

@ -0,0 +1,59 @@
package com.xcs.spring;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.PropertySourcesPropertyResolver;
import java.util.HashMap;
import java.util.Map;
/**
* @author xcs
* @date 20231130 1527
**/
public class SimplePropertyResolverDemo {
public static void main(String[] args) {
// 创建属性源
Map<String, Object> properties = new HashMap<>();
properties.put("app.name", "Spring-Reading");
properties.put("app.version", "1.0.0");
properties.put("app.description", "This is a ${app.name} with version ${app.version}");
MapPropertySource propertySource = new MapPropertySource("myPropertySource", properties);
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(propertySource);
// 使用 PropertySourcesPropertyResolver
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
// 获取属性
String appName = propertyResolver.getProperty("app.name");
String appVersion = propertyResolver.getProperty("app.version");
System.out.println("获取属性 app.name: " + appName);
System.out.println("获取属性 app.version: " + appVersion);
// 检查属性是否存在
boolean containsDescription = propertyResolver.containsProperty("app.description");
boolean containsReleaseDate = propertyResolver.containsProperty("app.releaseDate");
System.out.println("是否包含 'app.description' 属性: " + containsDescription);
System.out.println("是否包含 'app.releaseDate' 属性: " + containsReleaseDate);
// 带默认值的属性获取
String appReleaseDate = propertyResolver.getProperty("app.releaseDate", "2023-11-30");
System.out.println("带默认值的属性获取 app.releaseDate : " + appReleaseDate);
// 获取必需属性
String requiredAppName = propertyResolver.getRequiredProperty("app.name");
System.out.println("获取必需属性 app.name: " + requiredAppName);
// 解析占位符
String appDescription = propertyResolver.resolvePlaceholders(properties.get("app.description").toString());
System.out.println("解析占位符 app.description: " + appDescription);
// 解析必需的占位符
String requiredAppDescription = propertyResolver.resolveRequiredPlaceholders("App Description: ${app.description}");
System.out.println("解析必需的占位符 : " + requiredAppDescription);
}
}