Skip to content

Commit 6567a5c

Browse files
authored
Merge pull request #1 from admintertar/master
兼容spring-native环境下protocol返回的是resource
2 parents bb8c8b4 + 1899b8d commit 6567a5c

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/main/java/io/mybatis/config/defaults/VersionConfig.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@
1818

1919
import io.mybatis.config.Config;
2020
import io.mybatis.config.ConfigHelper;
21+
import org.springframework.core.io.Resource;
22+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
23+
import org.springframework.core.io.support.ResourcePatternResolver;
2124

2225
import java.io.*;
2326
import java.net.JarURLConnection;
27+
import java.net.URI;
2428
import java.net.URL;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
2531
import java.util.*;
2632
import java.util.function.Function;
2733
import java.util.jar.JarEntry;
@@ -230,6 +236,42 @@ private Properties chooseFromFile(File file, String version) throws IOException
230236
});
231237
}
232238

239+
/**
240+
* 选择版本
241+
*/
242+
private Properties chooseFromResource(String version) throws IOException {
243+
244+
Map<String, URI> fileMap = new HashMap<>();
245+
246+
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
247+
Resource[] resources = resolver.getResources("classpath*:io/mybatis/provider/config/*.properties");
248+
for (Resource resource : resources) {
249+
URI uri = resource.getURI();
250+
251+
String urlString = uri.toString();
252+
if (urlString.endsWith(FILE_TYPE)) {
253+
Path path = Paths.get(uri);
254+
// 获取文件名
255+
Path fileName = path.getFileName();
256+
fileMap.put(fileName.toString(), uri);
257+
}
258+
}
259+
260+
List<ConfigVersion> versions = sortVersions(new ArrayList<>(fileMap.keySet()));
261+
ConfigVersion chooseVersion = chooseVersion(versions, version);
262+
263+
return build(versions, chooseVersion, configVersion -> {
264+
URI url = fileMap.get(chooseVersion.getFileName());
265+
InputStream inputStream = null;
266+
try {
267+
inputStream = url.toURL().openStream();
268+
} catch (IOException e) {
269+
return null;
270+
}
271+
return inputStream;
272+
});
273+
}
274+
233275
/**
234276
* 获取版本配置
235277
*/
@@ -263,6 +305,12 @@ protected Properties buildVersionProperties() {
263305
} catch (IOException e) {
264306
throw new RuntimeException(e);
265307
}
308+
} else if (resource.getProtocol().equals("resource")) {
309+
try {
310+
return chooseFromResource(version);
311+
} catch (Exception e) {
312+
throw new RuntimeException(e);
313+
}
266314
}
267315
return null;
268316
}

0 commit comments

Comments
 (0)