Skip to content

Commit

Permalink
自定义classloader问题
Browse files Browse the repository at this point in the history
当项目中使用了自定义classloader的时候,可以通过设置classloader上下文的方式来使得自己的mapper class能够被找到
  • Loading branch information
liyongjun1 authored and abel533 committed Jan 22, 2018
1 parent 7afd35d commit c15579a
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/main/java/tk/mybatis/mapper/util/MsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,28 @@ public static Class<?> getMapperClass(String msId) {
throw new MapperException("当前MappedStatement的id=" + msId + ",不符合MappedStatement的规则!");
}
String mapperClassStr = msId.substring(0, msId.lastIndexOf("."));
try {
return Class.forName(mapperClassStr);
} catch (ClassNotFoundException e) {
return null;
ClassLoader[] classLoader = getClassLoaders();
Class<?> mapperClass = null;
for (ClassLoader cl : classLoader) {
if (null != cl) {
try {
mapperClass = Class.forName(mapperClassStr, true, cl);
if (mapperClass != null) {
break;
}
} catch (ClassNotFoundException e) {
// we'll ignore this until all class loaders fail to locate the class
}
}
}
if (mapperClass == null) {
throw new MapperException("class loaders failed to locate the class " + mapperClassStr);
}
return mapperClass;
}

private static ClassLoader[] getClassLoaders() {
return new ClassLoader[]{Thread.currentThread().getContextClassLoader(), MsUtil.class.getClassLoader()};
}

/**
Expand Down

0 comments on commit c15579a

Please sign in to comment.