受不了springboot的yml和properties配置,我扩展出了groovy配置

文中代码地址:https://github.com/gaohanghbut/groovy-configuration

起因

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表示,极大的增加了可读性,降低了维护成本:

image

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

image

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

image

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

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

image

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

SpringBoot的扩展

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

image

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

image
image

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

image

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

image

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

实现对groovy配置的支持

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

image

它只有两个方法:

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

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

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

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

import com.google.common.collect.Sets;
import groovy.lang.GString;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scripting.groovy.GroovyScriptFactory;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * springboot 支持groovy配置
 *
 * @author gaohang
 */
public class GroovyPropertySourceLoader implements PropertySourceLoader {

  private static final String[] STRINGS = {"groovy"};

  private final Set<String> loaded = Sets.newHashSet();

  @Override
  public String[] getFileExtensions() {
    return STRINGS;
  }

  @Override
  public PropertySource<?> load(final String name, final Resource resource, final String profile) throws IOException {
    return createStringValueResolver((ClassPathResource) resource);
  }

  private PropertySource createStringValueResolver(final ClassPathResource resource) throws IOException {

    if (loaded.contains(resource.getPath())) {
      return null;
    }

    final Properties properties = new Properties();

    try {
      final Object scriptedObject = getGroovyConfigObject(resource);

      if (scriptedObject instanceof Map) {
        putToProperties(properties, (Map<?, ?>) scriptedObject);
      } else {
        final List<Field> fields = Reflections.getFields(scriptedObject.getClass());
        for (Field field : fields) {
          final Object value = Reflections.getField(field.getName(), scriptedObject);
          if (value instanceof Map) {
            putToProperties(properties, (Map<?, ?>) value);
          } else if (value instanceof String || value instanceof GString) {
            properties.put(field.getName(), String.valueOf(value));
          }
        }
      }
      return new PropertiesPropertySource("groovy:" + resource.getPath(), properties);
    } finally {
      loaded.add(resource.getPath());
    }
  }

  private void putToProperties(final Properties properties, final Map<?, ?> values) {
    if (CollectionUtils.isEmpty(values)) {
      return;
    }
    for (Map.Entry<?, ?> en : values.entrySet()) {
      properties.put(String.valueOf(en.getKey()), String.valueOf(en.getValue()));
    }
  }

  private Object getGroovyConfigObject(final ClassPathResource scriptSourceLocator) throws IOException {
    final GroovyScriptFactory groovyScriptFactory = new GroovyScriptFactory(scriptSourceLocator.getPath());
    groovyScriptFactory.setBeanClassLoader(getClass().getClassLoader());

    final ResourceScriptSource resourceScriptSource = new ResourceScriptSource(scriptSourceLocator);
    return groovyScriptFactory.getScriptedObject(resourceScriptSource);
  }

}

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

image

其中的内容:

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

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

import com.google.common.collect.Lists;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * @author gaohang on 15/12/4.
 */
final class Reflections {
  private Reflections() {
  }

  private static Field findField(Class<?> clazz, String name) {
    return findField(clazz, name, null);
  }

  public static Field findField(Class<?> clazz, String name, Class<?> type) {
    Class<?> searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
      Field[] fields = searchType.getDeclaredFields();
      for (Field field : fields) {
        if ((name == null || name.equals(field.getName())) && (type == null || type
            .equals(field.getType()))) {
          return field;
        }
      }
      searchType = searchType.getSuperclass();
    }
    return null;
  }

  public static List<Field> getFields(Class<?> clazz) {
    final List<Field> fields = Lists.newArrayList();
    Class<?> type = clazz;
    while (type != Object.class) {
      final Field[] declaredFields = type.getDeclaredFields();
      fields.addAll(Arrays.asList(declaredFields));
      type = type.getSuperclass();
    }
    return fields;
  }

  public static Object getField(String fieldName, Object target) {
    Field field = findField(target.getClass(), fieldName);
    if (!field.isAccessible()) {
      field.setAccessible(true);
    }
    try {
      return field.get(target);
    } catch (IllegalAccessException ex) {
      throw new IllegalStateException("Unexpected reflection exception - " + ex.getClass()
          .getName() + ": " + ex.getMessage(), ex);
    }
  }

}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容

  • SpringBoot基础 学习目标: 能够理解Spring的优缺点 能够理解SpringBoot的特点 能够理解S...
    dwwl阅读 5,407评论 4 81
  • 一、Spring Boot基本配置 1、入口类和@SpringBootApplication Spring Boo...
    梦中一点心雨阅读 18,319评论 0 5
  • 配置文件解析(下) 原创者:文思 一、yml(YAML Ain’t Markup Language)基本用法...
    文思li阅读 1,912评论 0 2
  • SpringMVC原理分析 Spring Boot学习 5、Hello World探究 1、POM文件 1、父项目...
    jack_jerry阅读 1,203评论 0 1
  • 对呀,已经是2017年了,准确来说,2017年开始3天了。对于一个毕业生来说,2017年应该是憧憬的一年吧,...
    沐11阅读 308评论 3 2