Skip to content

Commit 885cb72

Browse files
committed
Enable the thread context class loader in SpringBootVFS
Fixes gh-542 * Use the returned instance from ClassUtils#getDefaultClassLoader() * Can customize to use any class loader instance using Supplier
1 parent d9590b0 commit 885cb72

File tree

1 file changed

+21
-2
lines changed
  • mybatis-spring-boot-autoconfigure/src/main/java/org/mybatis/spring/boot/autoconfigure

1 file changed

+21
-2
lines changed

mybatis-spring-boot-autoconfigure/src/main/java/org/mybatis/spring/boot/autoconfigure/SpringBootVFS.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,13 +22,15 @@
2222
import java.nio.charset.Charset;
2323
import java.text.Normalizer;
2424
import java.util.List;
25+
import java.util.function.Supplier;
2526
import java.util.stream.Collectors;
2627
import java.util.stream.Stream;
2728

2829
import org.apache.ibatis.io.VFS;
2930
import org.springframework.core.io.Resource;
3031
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
3132
import org.springframework.core.io.support.ResourcePatternResolver;
33+
import org.springframework.util.ClassUtils;
3234

3335
/**
3436
* @author Hans Westerbeek
@@ -38,14 +40,16 @@
3840
public class SpringBootVFS extends VFS {
3941

4042
private static Charset urlDecodingCharset;
43+
private static Supplier<ClassLoader> classLoaderSupplier;
4144
private final ResourcePatternResolver resourceResolver;
4245

4346
static {
4447
setUrlDecodingCharset(Charset.defaultCharset());
48+
setClassLoaderSupplier(ClassUtils::getDefaultClassLoader);
4549
}
4650

4751
public SpringBootVFS() {
48-
this.resourceResolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
52+
this.resourceResolver = new PathMatchingResourcePatternResolver(classLoaderSupplier.get());
4953
}
5054

5155
@Override
@@ -77,6 +81,21 @@ public static void setUrlDecodingCharset(Charset charset) {
7781
urlDecodingCharset = charset;
7882
}
7983

84+
/**
85+
* Set the supplier for providing {@link ClassLoader} to used.
86+
* <p>
87+
* Default is a returned instance from {@link ClassUtils#getDefaultClassLoader()}.
88+
* </p>
89+
*
90+
* @param supplier
91+
* the supplier for providing {@link ClassLoader} to used
92+
*
93+
* @since 3.0.2
94+
*/
95+
public static void setClassLoaderSupplier(Supplier<ClassLoader> supplier) {
96+
classLoaderSupplier = supplier;
97+
}
98+
8099
private static String preserveSubpackageName(final String baseUrlString, final Resource resource,
81100
final String rootPath) {
82101
try {

0 commit comments

Comments
 (0)