更新
This commit is contained in:
@@ -6,6 +6,9 @@ import java.nio.file.*;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 资源解析器,用于在不同运行状况(JAR 包、WAR 包及 IDE 调试状态)下读写本地资源
|
||||
*/
|
||||
@Slf4j
|
||||
public class SmartResourceResolver {
|
||||
|
||||
@@ -39,27 +42,24 @@ public class SmartResourceResolver {
|
||||
* @param relativePath 相对路径
|
||||
* @param writable 是否一定可写
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static InputStream loadResource(String relativePath) {
|
||||
try {
|
||||
Path externalPath = resolveExternalPath(relativePath);
|
||||
public static InputStream loadResource(String relativePath) throws IOException {
|
||||
Path externalPath = resolveExternalPath(relativePath);
|
||||
|
||||
if (externalPath != null && Files.exists(externalPath)) {
|
||||
log.debug("从外部文件系统加载资源: {}", externalPath);
|
||||
return Files.newInputStream(externalPath);
|
||||
}
|
||||
|
||||
// 否则回退到 classpath(JAR、WAR、IDE)
|
||||
InputStream in = SmartResourceResolver.class.getClassLoader().getResourceAsStream(relativePath);
|
||||
if (in != null) {
|
||||
log.debug("从 classpath 内部加载资源: {}", relativePath);
|
||||
return in;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException("无法找到资源: " + relativePath);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("读取资源失败: " + relativePath, e);
|
||||
if (externalPath != null && Files.exists(externalPath)) {
|
||||
log.debug("从外部文件系统加载资源: {}", externalPath);
|
||||
return Files.newInputStream(externalPath);
|
||||
}
|
||||
|
||||
// 否则回退到 classpath(JAR、WAR、IDE)
|
||||
InputStream in = SmartResourceResolver.class.getClassLoader().getResourceAsStream(relativePath);
|
||||
if (in != null) {
|
||||
log.debug("从 classpath 内部加载资源: {}", relativePath);
|
||||
return in;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException("无法找到资源: " + relativePath);
|
||||
}
|
||||
|
||||
public static void saveText(String relativePath, String content) throws IOException {
|
||||
|
||||
44
src/main/java/quant/rich/emoney/util/SpringBeanDetector.java
Normal file
44
src/main/java/quant/rich/emoney/util/SpringBeanDetector.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package quant.rich.emoney.util;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.stereotype.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SpringBeanDetector {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static final List<Class> SPRING_COMPONENT_ANNOTATIONS = List.of(
|
||||
Component.class,
|
||||
Service.class,
|
||||
Repository.class,
|
||||
Controller.class,
|
||||
RestController.class,
|
||||
Configuration.class,
|
||||
ControllerAdvice.class,
|
||||
RestControllerAdvice.class,
|
||||
Aspect.class
|
||||
);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static boolean isSpringManagedClass(Class<?> clazz) {
|
||||
for (@SuppressWarnings("rawtypes") Class ann : SPRING_COMPONENT_ANNOTATIONS) {
|
||||
if (AnnotationUtils.findAnnotation(clazz, ann) != null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static String findMatchedAnnotation(Class<?> clazz) {
|
||||
for (@SuppressWarnings("rawtypes") Class ann : SPRING_COMPONENT_ANNOTATIONS) {
|
||||
if (AnnotationUtils.findAnnotation(clazz, ann) != null) {
|
||||
return ann.getSimpleName();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user