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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-01为什么公共事业机构会偏爱 TiDB :TiDB 数据库在某省妇幼健康管理系统的应用
- 2024-04-26敏捷开发:想要快速交付就必须舍弃产品质量?
- 2024-04-26静态代码分析的这些好处,我竟然都不知道?
- 2024-04-26你在测试金字塔的哪一层?(下)
- 2024-04-26快刀斩乱麻,DevOps让代码评审也自动起来
- 2024-04-262024年最好用的10款ER图神器!
- 2024-04-2203-为啥大模型LLM还没能完全替代你?
- 2024-04-2101-大语言模型发展
- 2024-04-17基于SpringWeb MultipartFile文件上传、下载功能
- 2024-04-14个人开发者,Spring Boot 项目如何部署