二十五. AbstractEnvironment抽象类分析

说明: 用于环境实现的抽象基类。支持保留默认配置文件名称的概念,并允许通过ACTIVE_PROFILES_PROPERTY_NAME和DEFAULT_PROFILES_PROPERTY_NAME属性指定活动和默认配置文件。

具体的子类主要区别于默认添加的PropertySource对象。AbstractEnvironment不添加任何内容。子类应该通过受保护的customizePropertySources(MutablePropertySources)钩子提供属性源,而客户端应该使用ConfigurableEnvironment.getPropertySources()和MutablePropertySources API进行定制。参见ConfigurableEnvironment javadoc获取使用示例。

该抽象类的属性有:

// 指示Spring忽略系统环境变量的系统属性,即从不尝试通过 System.getenv()检索此类变量。

// 默认值为“false”,返回到系统环境变量检查Spring环境属性(例如,配置字符串中的占位符)是否无法解析。如果您遇到来自Spring的getenv调用的日志警告,请考虑将此标志切换为“true”,例如在具有严格SecurityManager设置和AccessControlExceptions警告的WebSphere上。

public static final String IGNORE_GETENV_PROPERTY_NAME = "spring.getenv.ignore";

// 用于指定活动配置文件的属性名称:"spring.profiles.active"。值可以用逗号分隔。

// 注意,某些shell环境(如Bash)不允许在变量名中使用句点字符。假设正在使用Spring的SystemEnvironmentPropertySource,可以将此属性指定为环境变量SPRING_PROFILES_ACTIVE。

public static final String ACTIVE_PROFILES_PROPERTY_NAME = "spring.profiles.active";

// 指定默认激活配置文件的属性名称:"spring.profiles.default"。值可以用逗号分隔。

// 注意,某些shell环境(如Bash)不允许在变量名中使用句点字符。假设正在使用Spring的SystemEnvironmentPropertySource,可以将此属性指定为环境变量SPRING_PROFILES_DEFAULT。

public static final String DEFAULT_PROFILES_PROPERTY_NAME = "spring.profiles.default";

// 保留默认配置文件名称:“default”。如果没有显式地设置默认配置文件名称,并且没有显式地设置激活的配置文件名称,该配置文件将在默认情况下自动激活。

protected static final String RESERVED_DEFAULT_PROFILE_NAME = "default";

// 记录日志

protected final Log logger = LogFactory.getLog(getClass());

// 激活了的配置文件集合

private final Set<String> activeProfiles = new LinkedHashSet<>();

// 默认的配置文件集合

private final Set<String> defaultProfiles = new LinkedHashSet<>(getReservedDefaultProfiles());

// 可变的属性资源集合

private final MutablePropertySources propertySources;

// 可配置的属性解析器

private final ConfigurablePropertyResolver propertyResolver;

该类中有两个构造函数:

// 创建一个新的Environment实例,在构造期间回调customizePropertySources(MutablePropertySources),以允许子类根据需要添加或操作PropertySource实例。

public AbstractEnvironment() {

  this(new MutablePropertySources());

}

// 用一个特定的MutablePropertySources实例创建一个新的Environment实例,在构造期间回调customizePropertySources(MutablePropertySources),以允许子类根据需要添加或操作PropertySource实例

protected AbstractEnvironment(MutablePropertySources propertySources) {

  this.propertySources = propertySources;

  this.propertyResolver = createPropertyResolver(propertySources);

  customizePropertySources(propertySources);

}

其他方法:

// 工厂方法,用于创建环境使用的ConfigurablePropertyResolver实例

protected ConfigurablePropertyResolver createPropertyResolver(MutablePropertySources propertySources) {

  return new PropertySourcesPropertyResolver(propertySources);

}

// 返回环境正在使用的ConfigurablePropertyResolver。

protected final ConfigurablePropertyResolver getPropertyResolver() {

  return this.propertyResolver;

}

// 自定义在调用getProperty(String) 和相关方法期间此环境要搜索的PropertySource对象集

// 鼓励重写这个方法的子类使用MutablePropertySources.addLast(PropertySource)添加属性源,这样以后的子类就可以调用super.customizePropertySources(),并得到可预测的结果。例如:

//  public class Level1Environment extends AbstractEnvironment {

//       @Override

//       protected void customizePropertySources(MutablePropertySources propertySources) {

//           super.customizePropertySources(propertySources); // no-op from base class

//           propertySources.addLast(new PropertySourceA(...));

//           propertySources.addLast(new PropertySourceB(...));

//       }

//   }


//   public class Level2Environment extends Level1Environment {

//       @Override

//       protected void customizePropertySources(MutablePropertySources propertySources) {

  //          super.customizePropertySources(propertySources); // add all from superclass

   //         propertySources.addLast(new PropertySourceC(...));

//           propertySources.addLast(new PropertySourceD(...));

//       }

  //  }

// 在这种安排下,将按照源A、B、C和D的顺序解析属性。也就是说,属性源“A”优先于属性源“D”。如果Level2Environment子类希望赋予属性源C和D比A和B更高的优先级,它可以简单地调用super.customizePropertySources在添加自己的属性之后,而不是之前:

// public class Level2Environment extends Level1Environment {

//       @Override

  //      protected void customizePropertySources(MutablePropertySources propertySources) {

   //         propertySources.addLast(new PropertySourceC(...));

//           propertySources.addLast(new PropertySourceD(...));

  //          super.customizePropertySources(propertySources); // add all from superclass

//       }

//   }

// 根据需要,搜索顺序现在是C、D、A、B。

// 除了这些建议之外,子类可以使用任何由MutablePropertySources公开的add*、remove或replace方法,以创建所需的属性源的精确排列。

// 基本实现不注册属性源

//注意,任何ConfigurableEnvironment的客户端都可以通过getPropertySources()访问器进一步定制属性源,通常在ApplicationContextInitializer中。例如:

// ConfigurableEnvironment env = new StandardEnvironment();

   // env.getPropertySources().addLast(new PropertySourceX(...));

// 关于实例变量访问的警告

// 在子类中声明并具有默认初始值的实例变量不应从该方法中访问。由于Java对象创建生命周期的限制,当AbstractEnvironment()构造函数调用此回调时,尚未分配任何初始值,这可能导致NullPointerException或其他问题。如果需要访问实例变量的默认值,请将此方法保留为无操作,并在子类构造函数中直接执行属性源操作和实例变量访问。请注意,为实例变量赋值没有问题;它只是试图读取必须避免的默认值。

protected void customizePropertySources(MutablePropertySources propertySources) {

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容