Spring源码分析之Environment

2022/5/5 9:13:01

本文主要是介绍Spring源码分析之Environment,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前言

Environment表示当前Spring程序运行的环境,主要管理profiles和properties两种信息。
profiles用来区分当前是dev(开发)环境还是test(测试)环境或者prod(生产)环境。
properties表示所有的属性,包括操作系统环境变量,如PATH,JDK相关配置,如java.vm.specification.version(JDK版本),还有我们通过properties文件和yml文件自定义的属性。

Environment

相关类图如下

默认实现为StandardEnvironment,内部委托PropertySourcesPropertyResolver(属性解析器)来处理属性相关的添加和查询。

Profile

import org.springframework.core.env.StandardEnvironment;

public class TestProfile {

  public static void main(String[] args) {
    StandardEnvironment environment = new StandardEnvironment();
    environment.addActiveProfile("dev");
    for (String defaultProfile : environment.getDefaultProfiles()) {
      System.out.println(defaultProfile);//default
    }
    for (String activeProfile : environment.getActiveProfiles()) {
      System.out.println(activeProfile);//dev
    }
  }

}

管理activeProfile(活跃的profile)。

系统属性

import org.springframework.core.env.StandardEnvironment;

public class TestSystemProperties {

  public static void main(String[] args) {
    StandardEnvironment environment = new StandardEnvironment();
    environment.getSystemProperties().forEach((k, v) -> {
      System.out.println(k + ":" + v);
    });
    System.out.println("====================");
    environment.getSystemEnvironment().forEach((k, v) -> {
      System.out.println(k + ":" + v);
    });
    String property = environment.getProperty("os.version");
    System.out.println(property);
  }

}

加载及查询系统属性,内部是通过System.getProperties()来实现的。

自定义属性

定义一个properties文件

name=lisi
age=23
import java.io.IOException;
import java.util.Properties;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class TestLocalProperties {

  public static void main(String[] args) throws IOException {
    StandardEnvironment environment = new StandardEnvironment();
    System.out.println(environment.getProperty("name"));//null
    Properties properties = PropertiesLoaderUtils
        .loadProperties(new ClassPathResource("abc.properties"));
    PropertiesPropertySource propertySource = new PropertiesPropertySource("local", properties);
    environment.getPropertySources().addLast(propertySource);
    System.out.println(environment.getProperty("name"));//lisi
  }

}

从classpath下加载此properties文件,添加到environment中,每一个properties文件都可以看做一个PropertySource,environment中包含一系列的PropertySource。

占位符

name=lisi
nameAndAge=${name}${age:23}
import java.io.IOException;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.PropertyPlaceholderHelper;

public class TestPlaceholderHelper {

  public static void main(String[] args) throws IOException {
    Properties properties = PropertiesLoaderUtils
        .loadProperties(new ClassPathResource("abc.properties"));
    PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}", ":",
        true);
    String result = placeholderHelper.replacePlaceholders("${nameAndAge}", properties);
    System.out.println(result);//lisi23
  }

}

使用PropertyPlaceholderHelper用来处理占位符,${name}${age:23}表示取属性name的值和属性age的值,:表示如果age属性值不存在,取默认值23。
PropertySourcesPropertyResolver(属性解析器)内部就是使用PropertyPlaceholderHelper来处理占位符的。

在Spring中的使用

在Spring中所有关于属性的管理,都是通过environment来完成的,在处理@Value注解的注入时,就是通过解析占位符来实现的。



这篇关于Spring源码分析之Environment的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程