SPRING TUTORIAL SPRING TUTORIAL
综合篇
核心篇
数据篇
Web篇
IO篇
集成篇
其他
GitHub (opens new window)
综合篇
核心篇
数据篇
Web篇
IO篇
集成篇
其他
GitHub (opens new window)
  • 框架

    • Spring

      • Spring综合

      • Spring核心

        • Spring Bean
        • Spring IoC
        • Spring 依赖查找
        • Spring 依赖注入
        • Spring IoC 依赖来源
        • Spring Bean 作用域
        • Spring Bean 生命周期
        • Spring 配置元数据
        • Spring 应用上下文生命周期
        • Spring AOP
        • Spring 资源管理
        • Spring 校验
        • Spring 数据绑定
        • Spring 类型转换
        • Spring EL 表达式
        • Spring 事件
        • Spring 国际化
        • Spring 泛型处理
        • Spring 注解
        • Spring Environment 抽象
          • 理解 Spring Environment 抽象
          • Spring Environment 接口使用场景
          • Environment 占位符处理
          • 理解条件配置 Spring Profiles
          • Spring 4 重构 @Profile
          • 依赖注入 Environment
          • 依赖查找 Environment
          • 依赖注入 @Value
          • Spring 类型转换在 Environment 中的运用
          • Spring 类型转换在 @Value 中的运用
          • Spring 配置属性源 PropertySource
          • Spring 內建的配置属性源
          • 基于注解扩展 Spring 配置属性源
          • 基于 API 扩展 Spring 配置属性源
          • 问题
          • 参考资料
        • SpringBoot 之快速入门
        • SpringBoot 之属性加载详解
        • SpringBoot 之 Profile
      • Spring数据

      • SpringWeb

      • SpringIO

      • Spring集成

      • Spring安全

      • Spring其他

  • Java
  • 框架
  • Spring
  • Spring核心
dunwu
2022-12-23
目录

Spring Environment 抽象

# Spring Environment 抽象

# 理解 Spring Environment 抽象

统一的 Spring 配置属性管理

Spring Framework 3.1 开始引入 Environment 抽象,它统一 Spring 配置属性的存储,包括占位符处理和类型转换,不仅完整地替换 PropertyPlaceholderConfigurer,而且还支持更丰富的配置属性源(PropertySource)

条件化 Spring Bean 装配管理

通过 Environment Profiles 信息,帮助 Spring 容器提供条件化地装配 Bean

# Spring Environment 接口使用场景

  • ⽤于属性占位符处理
  • 用于转换 Spring 配置属性类型
  • 用于存储 Spring 配置属性源(PropertySource)
  • 用于 Profiles 状态的维护

# Environment 占位符处理

Spring 3.1 前占位符处理

  • 组件:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
  • 接口:org.springframework.util.StringValueResolver

Spring 3.1 + 占位符处理

  • 组件:org.springframework.context.support.PropertySourcesPlaceholderConfigurer
  • 接口:org.springframework.beans.factory.config.EmbeddedValueResolver

# 理解条件配置 Spring Profiles

Spring 3.1 条件配置

  • API:org.springframework.core.env.ConfigurableEnvironment
  • 修改:addActiveProfile(String)、setActiveProfiles(String...) 和 setDefaultProfiles(String...)
  • 获取:getActiveProfiles() 和 getDefaultProfiles()
  • 匹配:#acceptsProfiles(String...) 和 acceptsProfiles(Profiles)
  • 注解:@org.springframework.context.annotation.Profile

# Spring 4 重构 @Profile

基于 Spring 4 org.springframework.context.annotation.Condition 接口实现

org.springframework.context.annotation.ProfileCondition

# 依赖注入 Environment

直接依赖注入

  • 通过 EnvironmentAware 接口回调
  • 通过 @Autowired 注入 Environment

间接依赖注入

  • 通过 ApplicationContextAware 接口回调
  • 通过 @Autowired 注入 ApplicationContext

# 依赖查找 Environment

直接依赖查找

  • 通过 org.springframework.context.ConfigurableApplicationContext#ENVIRONMENT_BEAN_NAME

间接依赖查找

  • 通过 org.springframework.context.ConfigurableApplicationContext#getEnvironment

# 依赖注入 @Value

通过注入 @Value

实现 - org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor

# Spring 类型转换在 Environment 中的运用

Environment 底层实现

  • 底层实现 - org.springframework.core.env.PropertySourcesPropertyResolver
  • 核心方法 - convertValueIfNecessary(Object,Class)
  • 底层服务 - org.springframework.core.convert.ConversionService
  • 默认实现 - org.springframework.core.convert.support.DefaultConversionService

# Spring 类型转换在 @Value 中的运用

@Value 底层实现

  • 底层实现 - org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
    • org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency
  • 底层服务 - org.springframework.beans.TypeConverter
    • 默认实现 - org.springframework.beans.TypeConverterDelegate
      • java.beans.PropertyEditor
      • org.springframework.core.convert.ConversionService

# Spring 配置属性源 PropertySource

  • API
    • 单配置属性源 - org.springframework.core.env.PropertySource
    • 多配置属性源 - org.springframework.core.env.PropertySources
  • 注解
    • 单配置属性源 - @org.springframework.context.annotation.PropertySource
    • 多配置属性源 - @org.springframework.context.annotation.PropertySources
  • 关联
    • 存储对象 - org.springframework.core.env.MutablePropertySources
    • 关联方法 - org.springframework.core.env.ConfigurableEnvironment#getPropertySources()

# Spring 內建的配置属性源

內建 PropertySource

PropertySource 类型 说明
org.springframework.core.env.CommandLinePropertySource 命令行配置属性源
org.springframework.jndi.JndiPropertySource JDNI 配置属性源
org.springframework.core.env.PropertiesPropertySource Properties 配置属性源
org.springframework.web.context.support.ServletConfigPropertySource Servlet 配置属性源
org.springframework.web.context.support.ServletContextPropertySource ServletContext 配置属性源
org.springframework.core.env.SystemEnvironmentPropertySource 环境变量配置属性源

# 基于注解扩展 Spring 配置属性源

@org.springframework.context.annotation.PropertySource 实现原理

  • 入口 - org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass
    • org.springframework.context.annotation.ConfigurationClassParser#processPropertySource
  • 4.3 新增语义
    • 配置属性字符编码 - encoding
    • org.springframework.core.io.support.PropertySourceFactory
  • 适配对象 - org.springframework.core.env.CompositePropertySource

# 基于 API 扩展 Spring 配置属性源

  • Spring 应用上下文启动前装配 PropertySource
  • Spring 应用上下文启动后装配 PropertySource

# 问题

简单介绍 Spring Environment 接口?

  • 核心接口 - org.springframework.core.env.Environment
  • 父接口 - org.springframework.core.env.PropertyResolver
  • 可配置接口 - org.springframework.core.env.ConfigurableEnvironment
  • 职责:
    • 管理 Spring 配置属性源
    • 管理 Profiles

# 参考资料

  • Spring 官方文档之 Core Technologies (opens new window)
  • 《小马哥讲 Spring 核心编程思想》 (opens new window)
📝 帮助改善此页面! (opens new window)
#Java#框架#Spring
上次更新: 2024/04/24, 07:33:33
Spring 注解
SpringBoot 之快速入门

← Spring 注解 SpringBoot 之快速入门→

最近更新
01
Spring MVC 之视图技术
02-17
02
Spring MVC 之跨域
02-16
03
Spring Web 应用
02-14
更多文章>
Theme by Vdoing | Copyright © 2019-2024 钝悟(dunwu) | CC-BY-SA-4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×