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

举报
程序员欣宸 发表于 2022/06/12 15:39:59 2022/06/12
【摘要】 在之前学习spring环境初始化源码的过程中,见到有些地方能通过子类来实现自定义扩展,从本章开始,我们来逐个实践这些扩展,除了加深对spring的理解,有的扩展也能解决一些通用的问题

欢迎访问我的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;

@SpringBootApplication
public 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下载,地址和链接信息如下表所示:
名称 链接 备注
项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议
  • 这个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: default
2018-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 hierarchy
2018-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: default
2018-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 hierarchy
2018-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.32
2018-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 WebApplicationContext
2018-08-12 14:15:32.868  INFO 15328 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1021 ms
2018-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 hierarchy
2018-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 startup
2018-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 started
2018-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强大的扩展机制;

欢迎关注华为云博客:程序员欣宸

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

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。