com.iohao.profile.io.support.ResourcePatternResolverConfig Maven / Gradle / Ivy
package com.iohao.profile.io.support;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
/**
*
* profile 配置文件加载.
* 这里说的是 /resources/conf下面的目录.
* 加载顺序 common -> {然后到开发人员指定的目录}.
*
* create time : 2019-02-02 12:14
*
* @author [email protected]
*/
@Slf4j
public class ResourcePatternResolverConfig {
/** 默认主目录 */
@Setter
private static String rootDir = "conf";
/** 默认加载主目录-子目录 */
@Setter
private static String defaultDir = "common";
/** 默认加载指定后缀文件 */
@Setter
private static String suffix = ".props";
/** 需要加载的目录列表 */
private LinkedList dirNameList = new LinkedList<>();
public void addDir(String dir) {
dirNameList.add(dir);
}
public List toUrls() {
// 优先加载 common 目录
dirNameList.addFirst(defaultDir);
List files = new LinkedList<>();
String locationPatternTemplate = "classpath*:%s/%s/*%s";
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
for (String dir : dirNameList) {
String locationPattern = String.format(locationPatternTemplate, rootDir, dir, suffix);
Resource[] resources = resolver.getResources(locationPattern);
log.debug("locationPattern: {}", locationPattern);
for (Resource resource : resources) {
URL url = resource.getURL();
files.add(url);
}
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return files;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy