Merge pull request #40 from Yuao-github/main

perf[storage] 把@Resource指定路径优化为指定别名
This commit is contained in:
godotg
2022-12-29 13:26:47 +08:00
committed by GitHub
4 changed files with 21 additions and 35 deletions
@@ -90,7 +90,7 @@ public class StorageManager implements IStorageManager {
throw new RuntimeException(StringUtils.format("Unable to get resource [class:{}]", clazzName));
}
var resourceFile = getResourceFile(resourceClazz);
var resourceFile = scanResourceFile(resourceClazz);
ResourceDef resourceDef = new ResourceDef(resourceClazz, resourceFile);
if (resourceDefinitionMap.containsKey(resourceClazz)) {
// 类的资源定义已经存在
@@ -252,14 +252,24 @@ public class StorageManager implements IStorageManager {
private Resource scanResourceFile(Class<?> clazz) {
var resourcePatternResolver = new PathMatchingResourcePatternResolver();
var metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
String fileName;
if(clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value().equals("")
&&clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).alias().equals("")){
fileName=clazz.getSimpleName();
}
else{
if(clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value().equals(""))
fileName=clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).alias();
else
fileName=clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value();
}
try {
// 一个class类只能匹配一个资源文件,如果匹配多个则会有歧义
var resourceSet = new HashSet<Resource>();
var resourceLocations = StringUtils.tokenize(storageConfig.getResourceLocation(), ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (var resourceLocation : resourceLocations) {
var resources = new ArrayList<Resource>();
var packageSearchPath = StringUtils.format("{}/**/{}.*", resourceLocation, clazz.getSimpleName());
var packageSearchPath = StringUtils.format("{}/**/{}.*", resourceLocation, fileName);
packageSearchPath = packageSearchPath.replaceAll("//", "/");
try {
Arrays.stream(resourcePatternResolver.getResources(packageSearchPath)).filter(it -> ResourceEnum.containsResourceEnum(FileUtils.fileExtName(it.getFilename()))).forEach(it -> resources.add(it));
@@ -269,7 +279,7 @@ public class StorageManager implements IStorageManager {
// 通配符无法匹配根目录,所以如果找不到,再从根目录查找一遍
if (resources.isEmpty()) {
packageSearchPath = StringUtils.format("{}/{}.*", resourceLocation, clazz.getSimpleName());
packageSearchPath = StringUtils.format("{}/{}.*", resourceLocation, fileName);
packageSearchPath = packageSearchPath.replaceAll("//", "/");
Arrays.stream(resourcePatternResolver.getResources(packageSearchPath)).filter(it -> ResourceEnum.containsResourceEnum(FileUtils.fileExtName(it.getFilename()))).forEach(it -> resources.add(it));
}
@@ -277,7 +287,7 @@ public class StorageManager implements IStorageManager {
}
if (CollectionUtils.isEmpty(resourceSet)) {
throw new FileNotFoundException(clazz.getSimpleName());
throw new FileNotFoundException(fileName);
}
if (resourceSet.size() > 1) {
var resourceNames = resourceSet.stream().map(it -> it.getFilename()).collect(Collectors.joining(StringUtils.COMMA));
@@ -290,28 +300,4 @@ public class StorageManager implements IStorageManager {
throw new RuntimeException(ExceptionUtils.getMessage(e));
}
}
private Resource getResourceFileByPath(Class<?> clazz){
var resourcePatternResolver = new PathMatchingResourcePatternResolver();
var metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
var path=clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value();
if(path.equals("")){
path=clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).path();
}
var resource= resourcePatternResolver.getResource(path);
try {
var input=resource.getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
return resource;
}
private Resource getResourceFile(Class<?> clazz){
if(clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value().equals("")&&
clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).path().equals("")){
return scanResourceFile(clazz);
}
else{
return getResourceFileByPath(clazz);
}
}
}
@@ -19,8 +19,8 @@ import java.lang.annotation.*;
/**
* 资源注解
* 可以指定对应的资源路径,如果是类路径一classpath开头,如果是其它目录则以file开头
* 如果不指定路径,则默认通过扫描路径获取与类名相同的文件资源
* 可以指定对应的资源文件名(只指定文件名,不需要文件后缀)
* 如果不指定资源文件名,则默认通过扫描路径获取与类名相同的文件资源
* @author godotg
* @version 4.0
*/
@@ -28,10 +28,10 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Resource {
@AliasFor("path")
@AliasFor("alias")
String value() default "";
@AliasFor("value")
String path() default "";
String alias() default "";
}
@@ -49,7 +49,7 @@ public class ApplicationTest {
var studentManager = context.getBean(StudentManager.class);
var studentResources = studentManager.studentResources;
var studentCsvResources = studentManager.studentCsvResources;
// @Resource注解没指定路径,类名称和Excel名称必须完全一致,Excel的列名称必须对应对象的属性名称
// @Resource注解没指定别名,类名称和Excel名称必须完全一致,Excel的列名称必须对应对象的属性名称
for (StudentResource resource : studentResources.getAll()) {
logger.info(JsonUtils.object2String(resource));
}
@@ -21,7 +21,7 @@ import com.zfoo.storage.model.anno.Resource;
* @author godotg
* @version 4.0
*/
@Resource(path="classpath:/excel/StudentCsvResource.csv")
@Resource(alias="StudentCsvResource")
public class StudentCsvResource {
@Id