关于Spring属性处理器PropertyResolver以及应用运行环境Environment的深度分析,强大的StringValueResolver使用和解析【享学Spring】(上)

简介: 关于Spring属性处理器PropertyResolver以及应用运行环境Environment的深度分析,强大的StringValueResolver使用和解析【享学Spring】(上)

前言


若直接提PropertyResolver或者StringValueResolver可能很小伙伴会觉得非常的陌生,但是我若提Environment和EmbeddedValueResolverAware这个感知接口,相信大部分小伙伴就能感受到一种亲切感了~。


我们的任何一个Spring Bean若实现了EmbeddedValueResolverAware接口,Spring容器在启动的时候就会自动给我们我们的Bean注入进来一个StringValueResolver。然后我们借用这个resolver就能处理一系列字符串的逻辑比如:占位符解释、SpEL计算等等~


本文就是以StringValueResolver为引子,去剖析它的底层依赖逻辑:PropertyResolver和Environment


PropertyResolver


org.springframework.core.env.PropertyResolver此接口用于在底层源之上解析一系列的属性值:例如properties文件,yaml文件,甚至是一些nosql(因为nosql也是k-v形式)。


接口中定义了一系列读取,解析,判断是否包含指定属性的方法:


// @since 3.1  出现得还是相对较晚的  毕竟SpEL也3.0之后才出来嘛~~~
public interface PropertyResolver {
  // 查看规定指定的key是否有对应的value   注意:若对应值是null的话 也是返回false
  boolean containsProperty(String key);
  // 如果没有则返回null
  @Nullable
  String getProperty(String key);
  // 如果没有则返回defaultValue
  String getProperty(String key, String defaultValue);
  // 返回指定key对应的value,会解析成指定类型。如果没有对应值则返回null(而不是抛错~)
  @Nullable
  <T> T getProperty(String key, Class<T> targetType);
  <T> T getProperty(String key, Class<T> targetType, T defaultValue);
  // 若不存在就不是返回null了  而是抛出异常~  所以不用担心返回值是null
  String getRequiredProperty(String key) throws IllegalStateException;
  <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException;
  // 解析${...}这种类型的占位符,把他们替换为使用getProperty方法返回的结果,解析不了并且没有默认值的占位符会被忽略(原样输出)
  String resolvePlaceholders(String text);
  // 解析不了就抛出异常~
  String resolveRequiredPlaceholders(String text) throws IllegalArgumentException;
}


它的继承链可以描述如下:PropertyResolver -> ConfigurablePropertyResolver -> AbstractPropertyResolver -> PropertySourcesPropertyResolver


当然还有Environment分支,这里放在下面Environment章节继续解释说明~


ConfigurablePropertyResolver


顾名思义,它是一个可配置的处理器。这个方法不仅有父接口所有功能,还扩展定义类型转换、属性校验、前缀、后缀、分隔符等一些列的功能,这个在具体实现类里有所体现~


public interface ConfigurablePropertyResolver extends PropertyResolver {
  // 返回在解析属性时使用的ConfigurableConversionService。此方法的返回值可被用户定制化set
  // 例如可以移除或者添加Converter  cs.addConverter(new FooConverter());等等
  ConfigurableConversionService getConversionService();
  // 全部替换ConfigurableConversionService的操作(不常用)  一般还是get出来操作它内部的东东
  void setConversionService(ConfigurableConversionService conversionService);
  // 设置占位符的前缀  后缀    默认是${}
  void setPlaceholderPrefix(String placeholderPrefix);
  void setPlaceholderSuffix(String placeholderSuffix);
  // 默认值的分隔符   默认为冒号:
  void setValueSeparator(@Nullable String valueSeparator);
  // 是否忽略解析不了的占位符,默认是false  表示不忽略~~~(解析不了就抛出异常)
  void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders);
  /**
   * Specify which properties must be present, to be verified by
   * {@link #validateRequiredProperties()}.
   */
  void setRequiredProperties(String... requiredProperties);
  void validateRequiredProperties() throws MissingRequiredPropertiesException;
}


ConfigurableXXX成了Spring的一种命名规范,或者说是一种设计模式。它表示课配置的,所以都会提供大量的set方法


Spring很多接口都是读写分离的,最顶层接口一般都只会提供只读方法,这是Spring框架设计的一般规律之一

AbstractPropertyResolver


它是对ConfigurablePropertyResolver的一个抽象实现,实现了了所有的接口方法,并且只提供一个抽象方法给子类去实现~~~

// @since 3.1
public abstract class AbstractPropertyResolver implements ConfigurablePropertyResolver {
  @Nullable
  private volatile ConfigurableConversionService conversionService;
  // PropertyPlaceholderHelper是一个极其独立的类,专门用来解析占位符  我们自己项目中可议拿来使用  因为它不依赖于任何其他类
  @Nullable
  private PropertyPlaceholderHelper nonStrictHelper;
  @Nullable
  private PropertyPlaceholderHelper strictHelper;
  private boolean ignoreUnresolvableNestedPlaceholders = false;
  private String placeholderPrefix = SystemPropertyUtils.PLACEHOLDER_PREFIX;
  private String placeholderSuffix = SystemPropertyUtils.PLACEHOLDER_SUFFIX;
  @Nullable
  private String valueSeparator = SystemPropertyUtils.VALUE_SEPARATOR;
  private final Set<String> requiredProperties = new LinkedHashSet<>();
  // 默认值使用的DefaultConversionService
  @Override
  public ConfigurableConversionService getConversionService() {
    // Need to provide an independent DefaultConversionService, not the
    // shared DefaultConversionService used by PropertySourcesPropertyResolver.
    ConfigurableConversionService cs = this.conversionService;
    if (cs == null) {
      synchronized (this) {
        cs = this.conversionService;
        if (cs == null) {
          cs = new DefaultConversionService();
          this.conversionService = cs;
        }
      }
    }
    return cs;
  }
  ... // 省略get/set
  @Override
  public void setRequiredProperties(String... requiredProperties) {
    for (String key : requiredProperties) {
      this.requiredProperties.add(key);
    }
  }
  // 校验这些key~
  @Override
  public void validateRequiredProperties() {
    MissingRequiredPropertiesException ex = new MissingRequiredPropertiesException();
    for (String key : this.requiredProperties) {
      if (this.getProperty(key) == null) {
        ex.addMissingRequiredProperty(key);
      }
    }
    if (!ex.getMissingRequiredProperties().isEmpty()) {
      throw ex;
    }
  }
  ... //get/set property等方法省略  直接看处理占位符的方法即可
  @Override
  public String resolvePlaceholders(String text) {
    if (this.nonStrictHelper == null) {
      this.nonStrictHelper = createPlaceholderHelper(true);
    }
    return doResolvePlaceholders(text, this.nonStrictHelper);
  }
  private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
    return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix, this.valueSeparator, ignoreUnresolvablePlaceholders);
  }
  // 此处:最终都是委托给PropertyPlaceholderHelper去做  而getPropertyAsRawString是抽象方法  根据key返回一个字符串即可~
  private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
    return helper.replacePlaceholders(text, this::getPropertyAsRawString);
  }
}


最终,处理占位的核心逻辑在PropertyPlaceholderHelper身上,这个类不可小觑,是一个与业务无关非常强大的工具类,我们可以直接拿来主义~


PropertyPlaceholderHelper


将字符串里的占位符内容,用我们配置的properties里的替换。这个是一个单纯的类,没有继承没有实现,而且简单无依赖,没有依赖Spring框架其他的任何类。

个人感觉自己项目中可以拿来模仿或者直接使用。

// @since 3.0  Utility class for working with Strings that have placeholder values in them
public class PropertyPlaceholderHelper {
  // 这里保存着  通用的熟悉的 开闭的符号们~~~
  private static final Map<String, String> wellKnownSimplePrefixes = new HashMap<>(4);
  static {
    wellKnownSimplePrefixes.put("}", "{");
    wellKnownSimplePrefixes.put("]", "[");
    wellKnownSimplePrefixes.put(")", "(");
  }
  private final String placeholderPrefix;
  private final String placeholderSuffix;
  private final String simplePrefix;
  @Nullable
  private final String valueSeparator;
  private final boolean ignoreUnresolvablePlaceholders; // 是否采用严格模式~~
  // 从properties里取值  若你有就直接从Properties里取值了~~~
  public String replacePlaceholders(String value, final Properties properties)  {
    Assert.notNull(properties, "'properties' must not be null");
    return replacePlaceholders(value, properties::getProperty);
  }
  // @since 4.3.5   抽象类提供这个类型转换的方法~ 需要类型转换的会调用它
  // 显然它是委托给了ConversionService,而这个类在前面文章已经都重点分析过了~
  @Nullable
  protected <T> T convertValueIfNecessary(Object value, @Nullable Class<T> targetType) {
    if (targetType == null) {
      return (T) value;
    }
    ConversionService conversionServiceToUse = this.conversionService;
    if (conversionServiceToUse == null) {
      // Avoid initialization of shared DefaultConversionService if
      // no standard type conversion is needed in the first place...
      if (ClassUtils.isAssignableValue(targetType, value)) {
        return (T) value;
      }
      conversionServiceToUse = DefaultConversionService.getSharedInstance();
    }
    return conversionServiceToUse.convert(value, targetType);
  }
  // 这里会使用递归,根据传入的符号,默认值等来处理~~~~
  protected String parseStringValue(String value, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) { ... }
}


这个工具类不仅仅用在此处,在ServletContextPropertyUtils、SystemPropertyUtils、PropertyPlaceholderConfigurer里都是有使用到它的


PropertySourcesPropertyResolver


从上面知道AbstractPropertyResolver封装了解析占位符的具体实现。PropertySourcesPropertyResolver作为它的子类它只需要提供数据源,所以它主要是负责提供数据源。

// @since 3.1    PropertySource:就是我们所说的数据源,它是Spring一个非常重要的概念,比如可以来自Map,来自命令行、来自自定义等等~~~
public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
  // 数据源们~
  @Nullable
  private final PropertySources propertySources;
  // 唯一构造函数:必须制定数据源~
  public PropertySourcesPropertyResolver(@Nullable PropertySources propertySources) {
    this.propertySources = propertySources;
  }
  @Override
  public boolean containsProperty(String key) {
    if (this.propertySources != null) {
      for (PropertySource<?> propertySource : this.propertySources) {
        if (propertySource.containsProperty(key)) {
          return true;
        }
      }
    }
    return false;
  }
  ...
  //最终依赖的都是propertySource.getProperty(key);方法拿到如果是字符串的话
  //就继续交给 value = resolveNestedPlaceholders((String) value);处理
  @Nullable
  protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
    if (this.propertySources != null) {
      for (PropertySource<?> propertySource : this.propertySources) {
        Object value = propertySource.getProperty(key);
        if (value != null) {
          // 若值是字符串,那就处理一下占位符~~~~~~  所以我们看到所有的PropertySource都是支持占位符的
          if (resolveNestedPlaceholders && value instanceof String) {
            value = resolveNestedPlaceholders((String) value);
          }
          logKeyFound(key, propertySource, value);
          return convertValueIfNecessary(value, targetValueType);
        }
      }
    }
    return null;
  }
  ...
}


ropertySources和PropertySource属性源是Spring里一个非常重要的概念设计,涉及到Spring属性配置的非常重要的优先级关系、以及它支持的配置类型。


Environment


这个接口代表了当前应用正在运行的环境,为应用的两个重要方面建立抽象模型 【profiles】和【properties】。关于属性访问的方法通过父接口PropertyResolver暴露给客户端使用,本接口主要是扩展出访问【profiles】相关的接口。


对于他俩,我愿意这么来翻译:


  • profiles:配置。它代表应用在一启动时注册到context中bean definitions的命名的逻辑分组。
  • properties:属性。几乎在所有应用中都扮演着重要角色,他可能源自多种源头。例如属性文件,JVM系统属性,系统环境变量,JNDI,servlet上下文参数,Map等等,Environment对象和其相关的对象一起提供给用户一个方便用来配置和解析属性的服务。

// @since 3.1   可见Spring3.x版本是Spirng一次极其重要的跨越、升级
// 它继承自PropertyResolver,所以是对属性的一个扩展~
public interface Environment extends PropertyResolver {
  // 就算被激活  也是支持同时激活多个profiles的~
  // 设置的key是:spring.profiles.active
  String[] getActiveProfiles();
  // 默认的也可以有多个  key为:spring.profiles.default
  String[] getDefaultProfiles();
  // 看看传入的profiles是否是激活的~~~~  支持!表示不激活
  @Deprecated
  boolean acceptsProfiles(String... profiles);
  // Spring5.1后提供的  用于替代上面方法   Profiles是Spring5.1才有的一个函数式接口~
  boolean acceptsProfiles(Profiles profiles);
}


我们可以通过实现接口EnvironmentAware或者直接@Autowired可以很方便的得到当前应用的环境:Environment。


稍微解释一下:若你是普通的Spring MVC环境(非Boot)也只会有一个Environment的,因为Spring容器内部会判断若你已经实例化过Environment就不会再重复实例化了~


它有如下实现:

image.png

相关文章
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1785 3
|
SQL Java 数据库连接
Spring Data JPA 技术深度解析与应用指南
本文档全面介绍 Spring Data JPA 的核心概念、技术原理和实际应用。作为 Spring 生态系统中数据访问层的关键组件,Spring Data JPA 极大简化了 Java 持久层开发。本文将深入探讨其架构设计、核心接口、查询派生机制、事务管理以及与 Spring 框架的集成方式,并通过实际示例展示如何高效地使用这一技术。本文档约1500字,适合有一定 Spring 和 JPA 基础的开发者阅读。
1103 0
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
1112 8
|
Java 应用服务中间件 开发者
Spring Boot 技术详解与应用实践
本文档旨在全面介绍 Spring Boot 这一广泛应用于现代企业级应用开发的框架。内容将涵盖 Spring Boot 的核心概念、核心特性、项目自动生成与结构解析、基础功能实现(如 RESTful API、数据访问)、配置管理以及最终的构建与部署。通过本文档,读者将能够理解 Spring Boot 如何简化 Spring 应用的初始搭建和开发过程,并掌握其基本使用方法。
992 2
|
安全 算法 Java
在Spring Boot中应用Jasypt以加密配置信息。
通过以上步骤,可以在Spring Boot应用中有效地利用Jasypt对配置信息进行加密,这样即使配置文件被泄露,其中的敏感信息也不会直接暴露给攻击者。这是一种在不牺牲操作复杂度的情况下提升应用安全性的简便方法。
1898 10
|
人工智能 监控 安全
如何快速上手【Spring AOP】?核心应用实战(上篇)
哈喽大家好吖~欢迎来到Spring AOP系列教程的上篇 - 应用篇。在本篇,我们将专注于Spring AOP的实际应用,通过具体的代码示例和场景分析,帮助大家掌握AOP的使用方法和技巧。而在后续的下篇中,我们将深入探讨Spring AOP的实现原理和底层机制。 AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架中的核心特性之一,它能够帮助我们解决横切关注点(如日志记录、性能统计、安全控制、事务管理等)的问题,提高代码的模块化程度和复用性。
1063 1
|
Java Linux 网络安全
Linux云端服务器上部署Spring Boot应用的教程。
此流程涉及Linux命令行操作、系统服务管理及网络安全知识,需要管理员权限以进行配置和服务管理。务必在一个测试环境中验证所有步骤,确保一切配置正确无误后,再将应用部署到生产环境中。也可以使用如Ansible、Chef等配置管理工具来自动化部署过程,提升效率和可靠性。
1454 13
|
NoSQL Java Redis
Redis基本数据类型及Spring Data Redis应用
Redis 是开源高性能键值对数据库,支持 String、Hash、List、Set、Sorted Set 等数据结构,适用于缓存、消息队列、排行榜等场景。具备高性能、原子操作及丰富功能,是分布式系统核心组件。
1069 2
|
安全 Java Nacos
0代码改动实现Spring应用数据库帐密自动轮转
Nacos作为国内被广泛使用的配置中心,已经成为应用侧的基础设施产品,近年来安全问题被更多关注,这是中国国内软件行业逐渐迈向成熟的标志,也是必经之路,Nacos提供配置加密存储-运行时轮转的核心安全能力,将在应用安全领域承担更多职责。
|
Java 数据库 开发者
Spring Boot 框架超级详细总结及长尾关键词应用解析
本文深入讲解Spring Boot框架的核心概念、功能特性及实际应用,涵盖自动配置、独立运行、starter依赖等优势。通过Web开发、微服务架构、批处理等适用场景分析,结合在线书店实战案例,演示项目初始化、数据库设计、分层架构实现全流程。同时探讨热部署、多环境配置、缓存机制与事务管理等高级特性,助你高效掌握Spring Boot开发技巧。代码示例详尽,适合从入门到进阶的学习者。
4620 0

热门文章

最新文章

推荐镜像

更多
  • DNS