起因

Springboot支持yml和properties两种方式的配置,不知道有没有同学和我一样,对yml, properties, xml这类配置非常不喜欢,配置太多了之后,可读性急剧下降,维护配置非常困难,估计只有java这样的编程语言的框架使用大量的xml, properties等作为配置文件了

但是Java支持groovy脚本,我们可以利用groovy来取代yml和properties,使用application.groovy替代application.yml或application.propertie,使用application-xxx.groovy替代application-xxx.yml或application-xxx.properties,并且支持groovy语法,配置类似如下图,对于groovy类中类型为String或者GString的属性都会被认为是一个property,对于类型为Map的属性,会认为是property的集合,基于这个特性,我们可以将同一类型的配置用同一个Map表示,极大的增加了可读性,降低了维护成本:




SpringBoot和Android上传和下载文件方案 springboot android_1


如果只需要用一个Map表示所有的配置,则可以不定义类,只定义一个Map:


SpringBoot和Android上传和下载文件方案 springboot android_1_02


在工程的resources目录下,通过application.groovy或者application-xxx.groovy表示配置:


SpringBoot和Android上传和下载文件方案 springboot android_1_03


可支持profile,本文中的例子是一个简化的配置,实际中的配置要复杂得多,在实际应用中,可将application.groovy与application.properties或者application.yml共存。

应用的启动类则不变,还是原来的样子:


SpringBoot和Android上传和下载文件方案 springboot android_1_04


关于实现方式,我们先从springboot的扩展开始

SpringBoot的扩展

这里不从头讲述springboot的扩展,这不是文章的重点,我们直接进入到一个类PropertySourcesLoader,其中初始化相关代码:


SpringBoot和Android上传和下载文件方案 springboot android_1_05


可以看到,这里通过SpringFactoriesLoader获取了PropertySourceLoader接口的实例,那么SpringFactoriesLoader是干嘛的?它就是用来加载spring的jar包中的spring.factories文件的,源码如下:


SpringBoot和Android上传和下载文件方案 springboot android_1_06


SpringBoot和Android上传和下载文件方案 springboot android_1_07


咱们再来看看PropertySourceLoader有哪些实现:


SpringBoot和Android上传和下载文件方案 springboot android_1_08


可以看到,springboot提供了properties和yml两种实现,咱们再看看PropertySourcesLoader中加载配置的代码:


SpringBoot和Android上传和下载文件方案 springboot android_1_09


通过这个方法我们可以看到,springboot分别用了不同的PropertySourceLoader加载不同格式的配置

实现对groovy配置的支持

咱们先看看PropertySourceLoader接口的定义:


SpringBoot和Android上传和下载文件方案 springboot android_1_10


它只有两个方法:

  • getFileExtensions:用于获取支持的配置文件的后缀
  • load:用于加载配置,得到PropertySource

讲到这里,大家应该就明白了,想要支持groovy,分两步即可:

  1. 实现一个PropertySourceLoader,用于加载groovy文件并得到PropertySource
  2. 创建META-INF/spring.factories并将实现的PropertySourceLoader配置在此文件中

咱们来看代码,先是对PropertySourceLoader的实现:


import


有了这个GroovyPropertySourceLoader后,我们再创建spring.factories:


SpringBoot和Android上传和下载文件方案 springboot android_1_11


其中的内容:

org.springframework.boot.env.PropertySourceLoader=cn.yxffcode.springboot.configuration.groovy.GroovyPropertySourceLoader

最后,GroovyPropertySourceLoader中使用到的Reflections类:


import