写点什么

spring4.1.8 扩展实战之一:自定义环境变量验证

作者:程序员欣宸
  • 2022 年 6 月 12 日
  • 本文字数:9244 字

    阅读完需:约 30 分钟

spring4.1.8扩展实战之一:自定义环境变量验证

欢迎访问我的 GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos

关于扩展

  • 在之前学习 spring 环境初始化源码的过程中,见到有些地方能通过子类来实现自定义扩展,从本章开始,我们来逐个实践这些扩展,除了加深对 spring 的理解,有的扩展也能解决一些通用的问题;

  • 文中涉及的 spring 版本号为 4.1.8.RELEASE;

相关文章链接

扩展功能介绍

  • 今天实战的内容,是通过 spring 容器来确保环境变量 MYSQL_HOST 一定存在,如果不存在应用就会启动失败;

分析 spring 源码

  • 通过分析 spring 源码来确定如何扩展;

  • 在 spring 环境初始化的时候,AbstractApplicationContext 的 prepareRefresh 方法会被调用,源码如下


protected void prepareRefresh() {    this.startupDate = System.currentTimeMillis();    this.closed.set(false);    this.active.set(true);
if (logger.isInfoEnabled()) { logger.info("Refreshing " + this); }
// Initialize any placeholder property sources in the context environment initPropertySources();
// Validate that all properties marked as required are resolvable // see ConfigurablePropertyResolver#setRequiredProperties getEnvironment().validateRequiredProperties(); }
复制代码


  • initPropertySources 是个空方法,留给子类扩展;

  • 来看看 getEnvironment().validateRequiredProperties()的源码,对应的是 AbstractPropertyResolver 类的 validateRequiredProperties 方法:


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;    }  }
复制代码


  • 可见 spring 容器初始化的时候,会从集合 requiredProperties 中取出所有 key,然后获取这些 key 的环境变量(包括系统环境变量和进程环境变量),如果有一个 key 对应的环境变量为空,就会抛出异常,导致 spring 容器初始化失败;

扩展功能分析

  • 看了 AbstractPropertyResolver 类的 validateRequiredProperties 方法源码后,可以确定该方法能强制要求一些环境变量必须存在,否则停止 spring 启动,我们只要把我们认为必要的环境变量的 key 存入集合 requiredProperties 中即可,达到此目标需要解决下面两个问题:

  • 如何将环境变量的 key 存入集合 requiredProperties?调用 AbstractPropertyResolver 类的 setRequiredProperties 方法,注意该方法是向集合 requiredProperties 中添加数据,并不会将已有数据清除;

  • 在什么时候执行 AbstractPropertyResolver 类的 setRequiredProperties 方法设置 key?创建一个 AbstractApplicationContext 的子类,重写 initPropertySources 方法,在此方法中执行 AbstractPropertyResolver 类的 setRequiredProperties;

开始实战

  • 用 maven 创建一个 SpringBoot 工程,工程名为 customizepropertyverify,pom.xml 内容如下:


<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>
<groupId>com.bolingcavalry</groupId> <artifactId>customizepropertyverify</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging>
<name>customizepropertyverify</name> <description>Demo project for Spring Boot</description>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
复制代码


  • 创建一个类 CustomApplicationContext,继承自 AnnotationConfigServletWebServerApplicationContext,重写 initPropertySources 方法,将"MYSQL_HOST"作为启动时必须要存在的环境变量:


package com.bolingcavalry.customizepropertyverify.context;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
/** * @Description : AnnotationConfigServletWebServerApplicationContext,重写了initPropertySources方法, * 要求spring启动的时候环境变量MYSQL_HOST必须存在 * @Author : zq2599@gmail.com * @Date : 2018-08-10 21:40 */public class CustomApplicationContext extends AnnotationConfigServletWebServerApplicationContext {
@Override protected void initPropertySources() { super.initPropertySources(); //把"MYSQL_HOST"作为启动的时候必须验证的环境变量 getEnvironment().setRequiredProperties("MYSQL_HOST"); }}
复制代码


  • 创建应用启动类 CustomizepropertyverifyApplication,指定 ApplicationContext 的 class 为 CustomApplicationContext:


package com.bolingcavalry.customizepropertyverify;
import com.bolingcavalry.customizepropertyverify.context.CustomApplicationContext;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class CustomizepropertyverifyApplication {
public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(CustomizepropertyverifyApplication.class); springApplication.setApplicationContextClass(CustomApplicationContext.class); springApplication.run(args); }}
复制代码


  • 打包,在 pom.xml 文件所在目录下执行命令 mvn clean package -U -DskipTests,命令执行完成后,即可在 target 目录找到文件 customizepropertyverify-0.0.1-SNAPSHOT.jar;

实战源码下载

  • 本章实战的源码可以在 github 下载,地址和链接信息如下表所示:



  • 这个 git 项目中有多个文件夹,本章源码在文件夹 customizepropertyverify 下,如下图红框所示:


验证

  • 接下来我们来验证自定义的 ApplicationContext 是否实现了环境变量检查的功能;

  • "MYSQL_HOST"这个环境变量是不存在的,所以我们先验证环境变量校验不通过导致 spring 容器启动失败的情况,在 target 目录执行命令 java -jar customizepropertyverify-0.0.1-SNAPSHOT.jar,会发现应用启动失败,日志中显示由于找不到环境变量"MYSQL_HOST",在 AbstractPropertyResolver.validateRequiredProperties 位置抛出异常,如下所示:


D:\github\blog_demos\customizepropertyverify\target>java -jar customizepropertyverify-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.4.RELEASE)
2018-08-12 14:10:16.165 INFO 19624 --- [ main] c.b.c.CustomizepropertyverifyApplication : Starting CustomizepropertyverifyApplication v0.0.1-SNAPSHOT on DESKTOP-82CCEBN with PID 19624 (D:\github\blog_demos\customizepropertyverify\target\customizepropertyverify-0.0.1-SNAPSHOT.jar started by 12167 in D:\github\blog_demos\customizepropertyverify\target)2018-08-12 14:10:16.168 INFO 19624 --- [ main] c.b.c.CustomizepropertyverifyApplication : No active profile set, falling back to default profiles: default2018-08-12 14:10:16.218 INFO 19624 --- [ main] c.b.c.context.CustomApplicationContext : Refreshing com.bolingcavalry.customizepropertyverify.context.CustomApplicationContext@6d1e7682: startup date [Sun Aug 12 14:10:16 GMT+08:00 2018]; root of context hierarchy2018-08-12 14:10:16.221 WARN 19624 --- [ main] o.s.boot.SpringApplication : Error handling failed (ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: com.bolingcavalry.customizepropertyverify.context.CustomApplicationContext@6d1e7682: startup date [Sun Aug 12 14:10:16 GMT+08:00 2018]; root of context hierarchy)2018-08-12 14:10:16.226 ERROR 19624 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.core.env.MissingRequiredPropertiesException: The following properties were declared as required but could not be resolved: [MYSQL_HOST] at org.springframework.core.env.AbstractPropertyResolver.validateRequiredProperties(AbstractPropertyResolver.java:146) ~[spring-core-5.0.8.RELEASE.jar!/:5.0.8.RELEASE] at org.springframework.core.env.AbstractEnvironment.validateRequiredProperties(AbstractEnvironment.java:519) ~[spring-core-5.0.8.RELEASE.jar!/:5.0.8.RELEASE] at org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:598) ~[spring-context-5.0.8.RELEASE.jar!/:5.0.8.RELEASE] at org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.prepareRefresh(AnnotationConfigServletWebServerApplicationContext.java:200) ~[spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:519) ~[spring-context-5.0.8.RELEASE.jar!/:5.0.8.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) ~[spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) ~[spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) ~[spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE] at com.bolingcavalry.customizepropertyverify.CustomizepropertyverifyApplication.main(CustomizepropertyverifyApplication.java:13) [classes!/:0.0.1-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_181] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_181] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_181] at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [customizepropertyverify-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [customizepropertyverify-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [customizepropertyverify-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [customizepropertyverify-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]...
复制代码


  • 接下来验证环境变量"MYSQL_HOST"存在的时候应用是否能初始化成功,执行命令 java -DMYSQL_HOST="192.168.0.1" -jar customizepropertyverify-0.0.1-SNAPSHOT.jar,这个命令可以将"MYSQL_HOST"设置到进程环境变量中,这次顺利通过校验,应用启动成功:


D:\github\blog_demos\customizepropertyverify\target>java -DMYSQL_HOST="192.168.0.1" -jar customizepropertyverify-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.4.RELEASE)
2018-08-12 14:15:31.811 INFO 15328 --- [ main] c.b.c.CustomizepropertyverifyApplication : Starting CustomizepropertyverifyApplication v0.0.1-SNAPSHOT on DESKTOP-82CCEBN with PID 15328 (D:\github\blog_demos\customizepropertyverify\target\customizepropertyverify-0.0.1-SNAPSHOT.jar started by 12167 in D:\github\blog_demos\customizepropertyverify\target)2018-08-12 14:15:31.813 INFO 15328 --- [ main] c.b.c.CustomizepropertyverifyApplication : No active profile set, falling back to default profiles: default2018-08-12 14:15:31.850 INFO 15328 --- [ main] c.b.c.context.CustomApplicationContext : Refreshing com.bolingcavalry.customizepropertyverify.context.CustomApplicationContext@3339ad8e: startup date [Sun Aug 12 14:15:31 GMT+08:00 2018]; root of context hierarchy2018-08-12 14:15:32.776 INFO 15328 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2018-08-12 14:15:32.796 INFO 15328 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2018-08-12 14:15:32.796 INFO 15328 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.322018-08-12 14:15:32.805 INFO 15328 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\jdk\bin;C:\software\Git\cmd;C:\software\apache-maven-3.5.0\bin;;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Users\12167\AppData\Local\Microsoft\WindowsApps;;.]2018-08-12 14:15:32.867 INFO 15328 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2018-08-12 14:15:32.868 INFO 15328 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1021 ms2018-08-12 14:15:32.919 INFO 15328 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]2018-08-12 14:15:32.923 INFO 15328 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2018-08-12 14:15:32.923 INFO 15328 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2018-08-12 14:15:32.926 INFO 15328 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2018-08-12 14:15:32.927 INFO 15328 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2018-08-12 14:15:33.006 INFO 15328 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-08-12 14:15:33.136 INFO 15328 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: com.bolingcavalry.customizepropertyverify.context.CustomApplicationContext@3339ad8e: startup date [Sun Aug 12 14:15:31 GMT+08:00 2018]; root of context hierarchy2018-08-12 14:15:33.190 INFO 15328 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.bolingcavalry.customizepropertyverify.controller.HelloWorldController.hello()2018-08-12 14:15:33.194 INFO 15328 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2018-08-12 14:15:33.195 INFO 15328 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)2018-08-12 14:15:33.226 INFO 15328 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-08-12 14:15:33.227 INFO 15328 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-08-12 14:15:33.314 INFO 15328 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-08-12 14:15:33.343 INFO 15328 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2018-08-12 14:15:33.346 INFO 15328 --- [ main] c.b.c.CustomizepropertyverifyApplication : Started CustomizepropertyverifyApplication in 1.761 seconds (JVM running for 2.113)2018-08-12 14:15:39.626 INFO 15328 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'2018-08-12 14:15:39.626 INFO 15328 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2018-08-12 14:15:39.645 INFO 15328 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
复制代码


  • 验证完成,我们可以通过自定义子类来强制要求指定的环境变量必须存在;

  • 至此,我们 spring 扩展实战的第一章就结束了,接下来的章节我们会进行更多的实战,来了解 spring 强大的扩展机制;

欢迎关注 InfoQ:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

发布于: 刚刚阅读数: 4
用户头像

搜索"程序员欣宸",一起畅游Java宇宙 2018.04.19 加入

前腾讯、前阿里员工,从事Java后台工作,对Docker和Kubernetes充满热爱,所有文章均为作者原创,个人Github:https://github.com/zq2599/blog_demos

评论

发布
暂无评论
spring4.1.8扩展实战之一:自定义环境变量验证_Java_程序员欣宸_InfoQ写作社区