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

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

相关文章的链接

为了方便开发和测试,我们的扩展实战是在springboot框架下进行的,在springboot自定义spring扩展的方式请参考《springboot应用使用自定义的ApplicationContext实现类》

扩展功能介绍

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

分析spring源码

通过分析spring源码来确定如何扩展;
在spring环境初始化的时候,AbstractApplicationContext的prepareRefesh方法被调用,源码如下:

/**
	 * Prepare this context for refreshing, setting its startup date and
	 * active flag as well as performing any initialization of property sources.
	 */
	protected void prepareRefresh() {
		this.startupDate = System.currentTimeMillis();
		this.closed.set(false);
		this.active.set(true);

		if (logger.isDebugEnabled()) {
			if (logger.isTraceEnabled()) {
				logger.trace("Refreshing " + this);
			}
			else {
				logger.debug("Refreshing " + getDisplayName());
			}
		}
		// Initialize any placeholder property sources in the context environment
		initPropertySources();
		// Validate that all properties marked as required are resolvable
		// see ConfigurablePropertyResolver#setRequiredProperties
		getEnvironment().validateRequiredProperties();
		// Allow for the collection of early ApplicationEvents,
		// to be published once the multicaster is available...
		this.earlyApplicationEvents = new LinkedHashSet<>();
	}

initPropertySource()是个空方法,留给子类扩展;
来看看getEnvironment().validateRequiredProperties(),对应的是AbstractPropertyResolver类的validateRequiredProperties方法:

@Override
	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即可,达到此目标需要解决下面两个问题:

  1. 如何将环境变量的key存入集合requiredProperties?
    调用AbstractPropertyResolver类的setRequiredProperties方法;
  2. 在什么时候执行AbstractPropertyResolver类的setRequiredProperties方法设置key?
    创建AbstractApplicationContext的子类,重写initPropertyResource方法,在此方法中执行AbstractPropertyResolver的setRequiredProperties;

开始实战

  1. 用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.lucas</groupId>
    <artifactId>customizepropertyverify</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <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>
        </dependency>
    </dependencies>
</project>
  1. 创建一个类CustomizeApplicationContext继承AnnotationConfigServletWebServerApplicationContext,重写initPropertySources方法,将MY_HOST作为启动时必须要存在的环境变量:
public class CustomizeApplicationContext extends AnnotationConfigServletWebServerApplicationContext {
    @Override
    protected void initPropertySources() {
        super.initPropertySources();
        //把"MY_HOST"作为启动的时候必须验证的环境变量
        getEnvironment().setRequiredProperties("MY_HOST");
    }
}
  1. 创建CustomizePropertyTest启动类,指定ApplicationContext的class为CustomizeApplicationContext :
public class CustomizePropertyTest {
    public static void main(String[] args) {
        SpringApplication application=new SpringApplication(CustomizePropertyTest.class);
        application.setApplicationContextClass(CustomizeApplicationContext.class);
        application.run(args);
    }
}

验证

接下来我们验证自定义的ApplicationContext是否实现了环境变量查询功能;
“MY_HOST”这个环境变量是不存在的,所有我们先验证环境变量校验不通过导致spring容器启动失败的情况,target目录执行命令java -jar customizepropertyverify-0.0.1-SNAPSHOT.jar,会发现应用启动失败,日志中显示由于找不到环境变量”MYSQL_HOST”,在AbstractPropertyResolver.validateRequiredProperties位置抛出异常,如下所示:

D:\ideaWork2019\blog-demo\customizepropertyverify\target>java -jar customizepropertyverify-1.0-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-20 17:05:27.387  INFO 7900 --- [           main] c.l.properties.CustomizePropertyTest     : Starting CustomizePropertyTest v1.0-SNAPSHOT on LAPTOP-45RFMD7C with PID 7900 (D:\ideaWork2019\blog-demo\customizepropertyverify\target\customizepropertyverify-1.0-SNAPSHOT.jar started by 李龙龙 in D:\ideaWork2019\blog-demo\customizepropertyverify\target)
2019-01-20 17:05:27.390  INFO 7900 --- [           main] c.l.properties.CustomizePropertyTest     : No active profile set, falling back to default profiles: default
2019-01-20 17:05:27.416  WARN 7900 --- [           main] o.s.boot.SpringApplication               : Error handling failed (ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: com.lucas.properties.CustomizeApplicationContext@5679c6c6, started on Sun Jan 20 17:05:27 CST 2019)
2019-01-20 17:05:27.416 ERROR 7900 --- [           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: [MY_HOST]
        at org.springframework.core.env.AbstractPropertyResolver.validateRequiredProperties(AbstractPropertyResolver.java:146) ~[spring-core-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.core.env.AbstractEnvironment.validateRequiredProperties(AbstractEnvironment.java:523) ~[spring-core-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:599) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.prepareRefresh(AnnotationConfigServletWebServerApplicationContext.java:200) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at com.lucas.properties.CustomizePropertyTest.main(CustomizePropertyTest.java:9) [classes!/:1.0-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [customizepropertyverify-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [customizepropertyverify-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [customizepropertyverify-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [customizepropertyverify-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]

2019-01-20 17:05:27.416  WARN 7900 --- [           main] c.l.p.CustomizeApplicationContext        : Exception thrown from ApplicationListener handling ContextClosedEvent

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: com.lucas.properties.CustomizeApplicationContext@5679c6c6, started on Sun Jan 20 17:05:27 CST 2019
        at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:419) [spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:398) [spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:355) [spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:994) [spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:961) [spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:834) [spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at com.lucas.properties.CustomizePropertyTest.main(CustomizePropertyTest.java:9) [classes!/:1.0-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [customizepropertyverify-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [customizepropertyverify-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [customizepropertyverify-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [customizepropertyverify-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]

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

D:\ideaWork2019\blog-demo\customizepropertyverify\target>java -DMYSQL_HOST="192.168.0.101" -jar customizepropertyverify-1.0-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-20 17:40:16.719  INFO 12448 --- [           main] c.l.properties.CustomizePropertyTest     : Starting CustomizePropertyTest v1.0-SNAPSHOT on LAPTOP-45RFMD7C with PID 12448 (D:\
ideaWork2019\blog-demo\customizepropertyverify\target\customizepropertyverify-1.0-SNAPSHOT.jar started by 李龙龙 in D:\ideaWork2019\blog-demo\customizepropertyverify\target)
2019-01-20 17:40:16.722  INFO 12448 --- [           main] c.l.properties.CustomizePropertyTest     : No active profile set, falling back to default profiles: default
2019-01-20 17:40:18.136  INFO 12448 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-01-20 17:40:18.168  INFO 12448 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-20 17:40:18.168  INFO 12448 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-20 17:40:18.180  INFO 12448 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in prod
uction environments was not found on the java.library.path: [D:\config\jdk1.8.0_65\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Intel\iCLS Client\;
C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\
Common;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) Man
agement Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerS
hell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\config\jdk1.8.0_65\bin;D:\config\gradle-5.1.1\bin;D:\config\Git\bin;C:\Users\李龙龙\AppData\Local\Microsoft\WindowsApps;;.]
2019-01-20 17:40:18.312  INFO 12448 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-20 17:40:18.313  INFO 12448 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1538 ms
2019-01-20 17:40:18.548  INFO 12448 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-20 17:40:18.746  INFO 12448 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-01-20 17:40:18.749  INFO 12448 --- [           main] c.l.properties.CustomizePropertyTest     : Started CustomizePropertyTest in 2.409 seconds (JVM running for 2.894)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值