本文内容

  1. Environment抽象的2个重要概念
  2. @Profile 的使用
  3. @PropertySource 的使用

Environment抽象的2个重要概念

Environment 接口表示当前应用程序运行环境的接口。对应用程序环境的两个关键方面进行建模:配置文件( profiles )和属性(properties)。与属性访问相关的方法通过 PropertyResolver 超接口公开。环境对象的配置必须通过 ConfigurableEnvironment 接口完成,该接口从所有 AbstractApplicationContext 子类 getEnvironment() 方法返回

环境与配置文件

配置文件是一个命名的、逻辑的 bean 定义组,仅当给定的配置文件处于活动状态时才向容器注册。可以将 Bean 分配给配置文件,无论是在 XML 中定义还是通过注释 @Profile 定义;与配置文件相关的环境对象的作用是确定哪些配置文件(如果有)当前处于活动状态,以及哪些配置文件(如果有)默认应该是活动的

环境与属性

属性在几乎所有应用程序中都发挥着重要作用,并且可能源自多种来源:属性文件、JVM 系统属性、系统环境变量、JNDI、servlet 上下文参数、属性对象、map等。与属性相关的环境对象的作用是为用户提供一个方便的服务接口,用于配置属性源并从中解析属性

在 ApplicationContext 中管理的 Bean 可以注册为 EnvironmentAware 或 @Inject Environment,以便直接查询配置文件状态或解析属性。然而,在大多数情况下,应用程序级别的 bean 不需要直接与 Environment 交互,而是可能必须将 ${...} 属性值替换为属性占位符配置器,例如 PropertySourcesPlaceholderConfigurer,它本身是 EnvironmentAware 并且从 Spring 3.1 开始使用 context:property-placeholder 时默认注册 ,或是通过java bean的方式注册到容器中。

PropertySourcesPlaceholderConfigurer 分析可以阅读上一篇: Spring系列14:IoC容器的扩展点

接口源码粗览

接口继承关系

接口源码如下提供配置文件相关的接口方法,其继承的 PropertyResolver 提供属性相关的接口。

  1. public interface Environment extends PropertyResolver {
  2. // 当前激活的配置文件列表
  3. // 设置系统属性值 spring.profiles.active=xxx 可激活
  4. // 或是调用 ConfigurableEnvironment#setActiveProfiles(String...)激活
  5. String[] getActiveProfiles();
  6. // 当没有明确设置活动配置文件时,默认配置文件集返回为活动状态。
  7. String[] getDefaultProfiles();
  8. // 返回活动配置文件是否与给定的 Profiles 匹配
  9. boolean acceptsProfiles(Profiles profiles);
  10. }

PropertyResolver 是针对任何底层源解析属性的接口,主要接口方法如下。有一个非常重要的实现类是 PropertySourcesPlaceholderConfigurer 。

  1. public interface PropertyResolver {
  2. // 是否包含属性
  3. boolean containsProperty(String key);
  4. // 获取属性值
  5. String getProperty(String key);
  6. // 获取属性值带默认值
  7. String getProperty(String key, String defaultValue);
  8. // 获取属性值
  9. <T> T getProperty(String key, Class<T> targetType);
  10. // 获取属性值带默认值
  11. <T> T getProperty(String key, Class<T> targetType, T defaultValue);
  12. // 获取属性值
  13. String getRequiredProperty(String key) throws IllegalStateException;
  14. // 获取属性值
  15. <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException;
  16. // 解析给定文本中的 ${...} 占位符
  17. String resolvePlaceholders(String text);
  18. // 解析给定文本中的 ${...} 占位符
  19. String resolveRequiredPlaceholders(String text) throws IllegalArgumentException;
  20. }

ConfigurablePropertyResolver 是大多数 PropertyResolver 类型都将实现的配置接口。提供用于访问和自定义将属性值从一种类型转换为另一种类型时使用的 ConversionService 的工具。

  1. public interface ConfigurablePropertyResolver extends PropertyResolver {
  2. ConfigurableConversionService getConversionService();
  3. void setConversionService(ConfigurableConversionService conversionService);
  4. // 设置占位符前缀 默认的 "${"怎么来的
  5. void setPlaceholderPrefix(String placeholderPrefix);
  6. // 设置占位符后缀 默认的 "}"怎么来的
  7. void setPlaceholderSuffix(String placeholderSuffix);
  8. // 设置占位符值分分隔符 默认的 ":"怎么来的
  9. void setValueSeparator(@Nullable String valueSeparator);
  10. void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders);
  11. void setRequiredProperties(String... requiredProperties);
  12. void validateRequiredProperties() throws MissingRequiredPropertiesException;
  13. }

ConfigurableEnvironment是大多数环境类型都将实现的配置接口。提供用于设置活动和默认配置文件以及操作基础属性源的工具。允许客户端通过 ConfigurablePropertyResolver 超级接口设置和验证所需属性、自定义转换服务等。

  1. public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
  2. void setActiveProfiles(String... profiles);
  3. void addActiveProfile(String profile);
  4. void setDefaultProfiles(String... profiles);
  5. MutablePropertySources getPropertySources();
  6. // 关键的系统属性 System#getProperties()
  7. Map<String, Object> getSystemProperties();
  8. // 关键的系统环境 System#getenv()
  9. Map<String, Object> getSystemEnvironment();
  10. void merge(ConfigurableEnvironment parent);
  11. }

@Profile 的使用

@Profile 表示当一个或多个profiles处于活动状态时,组件有资格注册。可以通过以下的方式设置活跃的一个或是多个配置文件:

  • 编程方式:ConfigurableEnvironment#setActiveProfiles(String...)
  • 启动参数: -Dspring.profiles.active="profile1,profile2"
  • xml配置方式:

使用案例

来看一个实际场景:不同环境要求在容器中注入不同类型的的数据源,dev环境使用H2,生产环境prod使用Mysql,default环境使用 HSQL。

定义不同环境的数据源,并标识 @Profile

  1. @Configuration
  2. @ComponentScan
  3. public class AppConfig {
  4. // 测试环境数据源H2
  5. @Profile("dev")
  6. @Bean
  7. public DataSource devDataSource() {
  8. DataSource dataSource = new DataSource();
  9. dataSource.setType("H2");
  10. dataSource.setUrl("jdbc:h2:xxxxxx");
  11. return dataSource;
  12. }
  13. // 生产环境数据源mysql
  14. @Profile("prod")
  15. @Bean
  16. public DataSource prodDataSource() {
  17. DataSource dataSource = new DataSource();
  18. dataSource.setType("mysql");
  19. dataSource.setUrl("jdbc:mysql:xxxxxx");
  20. return dataSource;
  21. }
  22. // default 环境的 HSQL
  23. @Profile("default")
  24. @Bean
  25. public DataSource defaultDataSource() {
  26. DataSource dataSource = new DataSource();
  27. dataSource.setType("HSQL");
  28. dataSource.setUrl("jdbc:HSQL:xxxxxx");
  29. return dataSource;
  30. }
  31. }

测试程序,首先不指定 profile

  1. @org.junit.Test
  2. public void test_profile() {
  3. AnnotationConfigApplicationContext context =
  4. new AnnotationConfigApplicationContext();
  5. // context.getEnvironment().setActiveProfiles("prod");
  6. context.register(AppConfig.class);
  7. context.refresh();
  8. DataSource dataSource = context.getBean(DataSource.class);
  9. System.out.println(dataSource.getType());
  10. context.close();
  11. }
  12. // 输出结果
  13. HSQL

从结果可知,注册到容器中的 default 环境对应的 HSQL

指定 profile 为 prod ,观察输出

  1. context.getEnvironment().setActiveProfiles("prod")
  2. // 结果
  3. mysql

从结果可知,注册到容器中的 prod 环境对应的 mysql 。

支持逻辑操作符

支持与或非操作组合

  • &
  • |

组合&和|必须使用小括号

反例:production & us-east | eu-central

正例:production & (us-east | eu-central)

使用 @Profile 自定义组合注解

定义组合注解

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Profile("production")
  4. public @interface Production {
  5. }

使用

  1. @Configuration
  2. @Production
  3. public class MyConfiguration {
  4. }

如果@Configuration 类用@Profile 标记,则与该类关联的所有@Bean 方法和@Import 注释都将被绕过,除非一个或多个指定的配置文件处于活动状态。

使用xml指定 profile

标签中的 profile元素的可以指定配置文件。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans profile="prod"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean class="com.crab.spring.ioc.demo13.DataSource" id="dataSource">
  7. <property name="type" value="mysql"/>
  8. <property name="url" value="jdbc:mysql/xxxxx"/>
  9. </bean>
  10. </beans>

PropertySource 抽象

Spring 的 Environment 抽象在可配置的属性源层次结构上提供搜索操作。来看下案例如何从Spring 容器获取属性。

  1. @org.junit.Test
  2. public void test_property_source() {
  3. ApplicationContext ctx = new GenericApplicationContext();
  4. Environment env = ctx.getEnvironment();
  5. boolean containsMyProperty = env.containsProperty("my-property");
  6. System.out.println("Does my environment contain the 'my-property' property? " + containsMyProperty);
  7. }

PropertySource 是对任何键值对源的简单抽象。Spring 的 StandardEnvironment 配置了两个 PropertySource 对象:

  • 一个表示一组 JVM 系统属性 (System.getProperties())

  • 一个表示一组系统环境变量 (System.getenv())

  1. public class StandardEnvironment extends AbstractEnvironment {
  2. /** System environment property source name: {@value}. */
  3. public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME = "systemEnvironment";
  4. /** JVM system properties property source name: {@value}. */
  5. public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = "systemProperties";
  6. // 自定义适合任何标准的属性源自定义一组属性源
  7. @Override
  8. protected void customizePropertySources(MutablePropertySources propertySources) {
  9. propertySources.addLast(
  10. new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
  11. propertySources.addLast(
  12. new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
  13. }
  14. }

在属性源中查找属性是否存在的优先级顺序如下,从高到低:

  1. ServletConfig parameters (web上下文)
  2. ServletContext parameters (web.xml context-param entries)
  3. JNDI environment variables (java:comp/env/ entries)
  4. JVM system properties (-D command-line arguments)
  5. JVM system environment (operating system environment variables)

自定义 PropertySource

自定义 MyPropertySource 实现 Property 提供基于 Map 属性键值对的属性源

  1. /**
  2. * 自定义 PropertySource
  3. * @author zfd
  4. * @version v1.0
  5. * @date 2022/1/22 22:13
  6. * @关于我 请关注公众号 螃蟹的Java笔记 获取更多技术系列
  7. */
  8. public class MyPropertySource extends PropertySource<Map<String, Object>> {
  9. public MyPropertySource(String name, Map<String, Object> source) {
  10. super(name, source);
  11. }
  12. public MyPropertySource(String name) {
  13. super(name);
  14. }
  15. @Override
  16. public Object getProperty(String name) {
  17. return this.source.get(name);
  18. }
  19. }

添加到Spring 容器环境中,优先级最高

  1. @org.junit.Test
  2. public void test_custom_property_source() {
  3. ConfigurableApplicationContext ctx = new GenericApplicationContext();
  4. MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
  5. Map<String, Object> map = new HashMap<>();
  6. map.put("my-property", "xxx");
  7. sources.addFirst(new MyPropertySource("myPropertySource",map));
  8. // true
  9. boolean containsMyProperty = ctx.getEnvironment().containsProperty("my-property");
  10. System.out.println("Does my environment contain the 'my-property' property? " + containsMyProperty);
  11. }

@PropertySource 使用

相比上面的编程式添加 PropertySource,@PropertySource 注解为将 PropertySource 添加到 Spring 的环境中提供了一种方便且声明性的机制。直接看案例。

app.properties配置

  1. testBean.name=xxx

配置类

  1. @Configuration
  2. // 注入配置文件
  3. @PropertySource("classpath:demo13/app.properties")
  4. public class AppConfig3 {
  5. @Autowired
  6. private Environment env;
  7. @Bean
  8. public TestBean testBean() {
  9. TestBean testBean = new TestBean();
  10. testBean.setName(env.getProperty("testBean.name"));
  11. return testBean;
  12. }
  13. }

测试结果观察

  1. @org.junit.Test
  2. public void test_property_source_annotation() {
  3. AnnotationConfigApplicationContext context =
  4. new AnnotationConfigApplicationContext(AppConfig3.class);
  5. TestBean testBean = context.getBean(TestBean.class);
  6. System.out.println(testBean.getName());
  7. }
  8. // 结果
  9. xxx

@PropertySource 中指定配置文件也是可以使用占位符${...}的。如果环境中属性值my.config.path已经存在则进行解析,否则使用默认值demo13

  1. @Configuration
  2. // 注入配置文件
  3. @PropertySource("classpath:${my.config.path:demo13}/app.properties")
  4. public class AppConfig3 {}

总结

本文介绍了Spring中的Environment抽象的2个重要概念:Bean定义配置文件和属性源。同时介绍了@Profile使用和@PropertySource 的使用。

本篇源码地址: https://github.com/kongxubihai/pdf-spring-series/tree/main/spring-series-ioc/src/main/java/com/crab/spring/ioc/demo13

知识分享,转载请注明出处。学无先后,达者为先!

Spring系列15:Environment抽象的更多相关文章

  1. Spring Environment抽象

    1:概述 Spring中Environment是Spring3.1版本引入的,是Spring核心框架定义的一个接口,用来表示整个应用运行时环境.该环境模型只接受两种应用环境profiles(配置文件) ...

  2. Spring系列之JDBC对不同数据库异常如何抽象的?

    前言 使用Spring-Jdbc的情况下,在有些场景中,我们需要根据数据库报的异常类型的不同,来编写我们的业务代码.比如说,我们有这样一段逻辑,如果我们新插入的记录,存在唯一约束冲突,就会返回给客户端 ...

  3. Spring系列.Environment接口

    Environment 接口介绍 在 Spring 中,Environment 接口主要管理应用程序两个方面的内容:profile 和 properties. profile 可以简单的等同于环境,比 ...

  4. Spring系列(零) Spring Framework 文档中文翻译

    Spring 框架文档(核心篇1和2) Version 5.1.3.RELEASE 最新的, 更新的笔记, 支持的版本和其他主题,独立的发布版本等, 是在Github Wiki 项目维护的. 总览 历 ...

  5. 朱晔和你聊Spring系列S1E2:SpringBoot并不神秘

    朱晔和你聊Spring系列S1E2:SpringBoot并不神秘 [编辑器丢失了所有代码的高亮,建议查看PDF格式文档] 文本我们会一步一步做一个例子来看看SpringBoot的自动配置是如何实现的, ...

  6. Spring 系列: Spring 框架简介 -7个部分

    Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...

  7. Java 集合系列 15 Map总结

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  8. Spring 系列: Spring 框架简介(转载)

    Spring 系列: Spring 框架简介 http://www.ibm.com/developerworks/cn/java/wa-spring1/ Spring AOP 和 IOC 容器入门 在 ...

  9. Spring系列

    Spring系列之访问数据库   阅读目录 一.概述 二.JDBC API的最佳实践 三.Spring对ORM的集成 回到顶部 一.概述 Spring的数据访问层是以统一的数据访问异常层体系为核心,结 ...

随机推荐

  1. kafka学习笔记(七)kafka的状态机模块

    概述 这一篇随笔介绍kafka的状态机模块,Kafka 源码中有很多状态机和管理器,比如之前我们学过的 Controller 通道管理器 ControllerChannelManager.处理 Con ...

  2. Android中添加监听回调接口的方法

    在Android中,我们经常会添加一些监听回调的接口供别的类来回调,比如自定义一个PopupWindow,需要让new这个PopupWindow的Activity来监听PopupWindow中的一些组 ...

  3. 『德不孤』Pytest框架 — 2、Pytest的基本使用

    目录 1.Pytest安装 2.Pytest常用插件 3.Pytest运行的第一个例子 4.Pytest框架的运行方式 5.在PyCharm中以Pytest的方式运行测试用例 1.Pytest安装 C ...

  4. CMake语法—普通变量与子目录(Normal Variable And Subdirectory)

    目录 CMake语法-普通变量与子目录(Normal Variable And Subdirectory) 1 CMake普通变量与子目录示例 1.1 代码目录结构 1.2 父目录CMakeLists ...

  5. gin中的SecureJSON 防止 json 劫持

    使用 SecureJSON 防止 json 劫持.如果给定的结构是数组值或map,则默认预置 "while(1)," 到响应体. package main import ( &qu ...

  6. IDEA2020.1破解

    IDEA2020.1破解 安装 下载idea idea官方下载地址:https://www.jetbrains.com/webstorm/download/other.html 下载破解插件 链接:h ...

  7. 用c#实现编写esp32单片机获取DHT11温度传感器参数

    欢迎爱好c#的爱好者,本文章我们将用C#的nanoframework框架来编写获取esp32单片机上的DHT11传感器的温度和湿度 实现我们需要准备配置好esp32的环境可以看看之前写的esp32搭建 ...

  8. plsql 将游标读取到table中

    -- 将游标中的数据 读取到table中 根据部门编号获得emp所有信息. declare cursor c(no emp.deptno%type)is select * from emp where ...

  9. 计算机电子书 2016 BiliDrive 备份

    下载方式 根据你的操作系统下载不同的 BiliDrive 二进制. 执行: bilidrive download <link> 链接 文档 链接 Go入门指南.epub (1.87 MB) ...

  10. php include,require,include_once,require_once 的区别

    include(),require(),include_once(),require_once()作用都是包含并运行指定文件,但是使用场景又有很大区别. 1.include()和require()的区 ...