public static String setTypeAliasesPackage(String typeAliasesPackage) { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver); List<String> allResult = new ArrayList<>(); try { for (String aliasesPackage : typeAliasesPackage.split(",")) { List<String> result = new ArrayList<>(); aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = resolver.getResources(aliasesPackage); if (resources != null && resources.length > 0) { MetadataReader metadataReader = null; for (Resource resource : resources) { if (resource.isReadable()) { metadataReader = metadataReaderFactory.getMetadataReader(resource); try { result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName()); } catch (ClassNotFoundException e) { log.debug(e.getMessage()); } } } } if (!result.isEmpty()) { HashSet<String> hashResult = new HashSet<>(result); allResult.addAll(hashResult); } } if (!allResult.isEmpty()) { typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0])); } else { throw new ServiceException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包"); } } catch (IOException e) { log.debug(e.getMessage()); } return typeAliasesPackage; } 优化这段代码
时间: 2024-02-10 11:13:01 浏览: 66
首先,这段代码的作用是从给定的包路径中获取所有的类并返回这些类所在的包路径,用于配置 MyBatis 的类型别名。
接下来,我列出了一些可以优化的地方:
1. 对于每个包路径,都会进行一次 ResourcePatternResolver 的初始化。可以将其提到方法外部,避免重复创建。
2. 使用了两层循环对每个包路径中的类进行遍历,效率较低。可以将其改为单层循环,并使用 Lambda 表达式优化代码。
3. 在获取所有结果后,使用了 HashSet 进行去重。可以在遍历时使用 Set 存储结果,避免重复添加。
综上所述,下面是我优化后的代码:
```
public static String setTypeAliasesPackage(String typeAliasesPackage) {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
List<String> allResult = new ArrayList<>();
String[] packages = typeAliasesPackage.split(",");
try {
for (String pkg : packages) {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resolver.getResources(pattern);
Set<String> result = Arrays.stream(resources)
.filter(Resource::isReadable)
.map(resource -> {
try {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
return Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName();
} catch (ClassNotFoundException | IOException e) {
log.debug(e.getMessage());
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (!result.isEmpty()) {
allResult.addAll(result);
} else {
throw new ServiceException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + pkg + "未找到任何包");
}
}
return String.join(",", allResult);
} catch (IOException e) {
log.debug(e.getMessage());
return typeAliasesPackage;
}
}
```
阅读全文