文中代碼地址: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表示缨称,極大的增加了可讀性,降低了維護成本:
如果只需要用一個Map表示所有的配置祝迂,則可以不定義類睦尽,只定義一個Map:
在工程的resources目錄下,通過application.groovy或者application-xxx.groovy表示配置:
可支持profile型雳,本文中的例子是一個簡化的配置当凡,實際中的配置要復雜得多,在實際應用中纠俭,可將application.groovy與application.properties或者application.yml共存沿量。
應用的啟動類則不變,還是原來的樣子:
關(guān)于實現(xiàn)方式冤荆,我們先從springboot的擴展開始
SpringBoot的擴展
這里不從頭講述springboot的擴展朴则,這不是文章的重點,我們直接進入到一個類PropertySourcesLoader钓简,其中初始化相關(guān)代碼:
可以看到乌妒,這里通過SpringFactoriesLoader獲取了PropertySourceLoader接口的實例肖卧,那么SpringFactoriesLoader是干嘛的许溅?它就是用來加載spring的jar包中的spring.factories文件的,源碼如下:
咱們再來看看PropertySourceLoader有哪些實現(xiàn):
可以看到辽俗,springboot提供了properties和yml兩種實現(xiàn)坐榆,咱們再看看PropertySourcesLoader中加載配置的代碼:
通過這個方法我們可以看到拴魄,springboot分別用了不同的PropertySourceLoader加載不同格式的配置
實現(xiàn)對groovy配置的支持
咱們先看看PropertySourceLoader接口的定義:
它只有兩個方法:
- getFileExtensions:用于獲取支持的配置文件的后綴
- load:用于加載配置,得到PropertySource
講到這里,大家應該就明白了匹中,想要支持groovy夏漱,分兩步即可:
- 實現(xiàn)一個PropertySourceLoader,用于加載groovy文件并得到PropertySource
- 創(chuàng)建META-INF/spring.factories并將實現(xiàn)的PropertySourceLoader配置在此文件中
咱們來看代碼顶捷,先是對PropertySourceLoader的實現(xiàn):
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后挂绰,我們再創(chuàng)建spring.factories:
其中的內(nèi)容:
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);
}
}
}