【scanner】增加两个scanner扫描到url是否添加app和context-path的开关

pull/3/head
fengshuonan 2020-12-14 22:34:03 +08:00
parent 995b7e791b
commit 4579ddffd1
4 changed files with 68 additions and 39 deletions

View File

@ -17,27 +17,36 @@ public class ScannerProperties {
private Boolean open; private Boolean open;
/** /**
* * urlappCodefalse
* <p>
* urlappCodetrue
*/ */
private String appName; private Boolean urlWithAppCode = false;
/** /**
* * 使spring.application.name
*/ */
private String appCode; private String appCode;
/** /**
* * urlcontext-pathtrue
* <p>
* trueurlcontext-path
* <p>
* falseurlcontext-path
* <p>
* urlWithAppCodeurlWithContextPathurl /appCode/contextPath/xxx
*/
private Boolean urlWithContextPath = true;
/**
* context-path
*/
private String contextPath;
/**
*
*/ */
private String linkSymbol = "$"; private String linkSymbol = "$";
/**
* 使spring.application.name
* <p>
*
*
* @since 2.2.12
*/
private String projectCode;
} }

View File

@ -50,15 +50,9 @@ public class ApiResourceScanner implements BeanPostProcessor {
*/ */
private final ScannerProperties scannerProperties; private final ScannerProperties scannerProperties;
/** public ApiResourceScanner(ResourceCollectorApi resourceCollectorApi, ScannerProperties scannerProperties) {
*
*/
private final String springApplicationName;
public ApiResourceScanner(ResourceCollectorApi resourceCollectorApi, ScannerProperties scannerProperties, String springApplicationName) {
this.resourceCollectorApi = resourceCollectorApi; this.resourceCollectorApi = resourceCollectorApi;
this.scannerProperties = scannerProperties; this.scannerProperties = scannerProperties;
this.springApplicationName = springApplicationName;
} }
@Override @Override
@ -229,7 +223,7 @@ public class ApiResourceScanner implements BeanPostProcessor {
resourceDefinition.setRequiredPermission(requiredPermission); resourceDefinition.setRequiredPermission(requiredPermission);
resourceDefinition.setMenuFlag(menuFlag); resourceDefinition.setMenuFlag(menuFlag);
resourceDefinition.setName(name); resourceDefinition.setName(name);
resourceDefinition.setUrl(getControllerClassRequestPath(clazz) + path[0]); resourceDefinition.setUrl(getControllerClassRequestPath(clazz, path[0]));
StringBuilder methodNames = new StringBuilder(); StringBuilder methodNames = new StringBuilder();
for (RequestMethod requestMethod : requestMethods) { for (RequestMethod requestMethod : requestMethods) {
methodNames.append(requestMethod.name()).append(","); methodNames.append(requestMethod.name()).append(",");
@ -251,7 +245,7 @@ public class ApiResourceScanner implements BeanPostProcessor {
resourceDefinition.setCreateTime(new Date()); resourceDefinition.setCreateTime(new Date());
// 填充项目编码 // 填充项目编码
resourceDefinition.setProjectCode(scannerProperties.getProjectCode()); resourceDefinition.setProjectCode(scannerProperties.getAppCode());
// 填充方法的校验分组 // 填充方法的校验分组
Set<String> methodValidateGroup = MethodReflectUtil.getMethodValidateGroup(method); Set<String> methodValidateGroup = MethodReflectUtil.getMethodValidateGroup(method);
@ -277,28 +271,48 @@ public class ApiResourceScanner implements BeanPostProcessor {
} }
/** /**
* RequestMapping,path * RequestMapping
* <p> *
* 201852spring.application.name * @param clazz
* @param path path
* @author fengshuonan
* @date 2020/12/14 22:17
*/ */
private String getControllerClassRequestPath(Class<?> clazz) { private String getControllerClassRequestPath(Class<?> clazz, String path) {
String result; String controllerPath;
ApiResource controllerRequestMapping = clazz.getDeclaredAnnotation(ApiResource.class); ApiResource controllerRequestMapping = clazz.getDeclaredAnnotation(ApiResource.class);
if (controllerRequestMapping == null) { if (controllerRequestMapping == null) {
result = ""; controllerPath = "";
} else { } else {
String[] paths = controllerRequestMapping.path(); String[] paths = controllerRequestMapping.path();
if (paths.length > 0) { if (paths.length > 0) {
result = paths[0]; controllerPath = paths[0];
} else { } else {
result = ""; controllerPath = "";
} }
} }
// 拼接spring.application.name // 拼接最终url的时候依据如下规则拼接/appCode/contextPath/xxx
result = "/" + springApplicationName + result; // 第一部分是appCode
return result; String appCode = "";
if (scannerProperties.getUrlWithAppCode()) {
appCode = "/" + StrUtil.removePrefix(scannerProperties.getAppCode(), "/");
}
// 第二部分是context-path
String contextPath = "";
if (scannerProperties.getUrlWithContextPath()) {
contextPath = "/" + StrUtil.removePrefix(scannerProperties.getContextPath(), "/");
}
// 依据如下规则拼接:/appCode/contextPath/xxx
String resultPath = appCode + contextPath + controllerPath + path;
// 前缀多个左斜杠替换为一个
resultPath = resultPath.replaceAll("/+", "/");
return resultPath;
} }
/** /**
@ -311,7 +325,7 @@ public class ApiResourceScanner implements BeanPostProcessor {
try { try {
Class<? extends Annotation> annotationType = apiResource.annotationType(); Class<? extends Annotation> annotationType = apiResource.annotationType();
Method method = annotationType.getMethod(methodName); Method method = annotationType.getMethod(methodName);
return (T)method.invoke(apiResource); return (T) method.invoke(apiResource);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
log.error("扫描api资源时出错!", e); log.error("扫描api资源时出错!", e);
} }

View File

@ -47,7 +47,7 @@ public class ResourceReportListener implements ApplicationListener<ApplicationRe
// 持久化资源,发送资源到资源服务或本项目(单体项目) // 持久化资源,发送资源到资源服务或本项目(单体项目)
ResourceReportApi resourceService = applicationContext.getBean(ResourceReportApi.class); ResourceReportApi resourceService = applicationContext.getBean(ResourceReportApi.class);
resourceService.reportResources(new ReportResourceParam(scannerProperties.getProjectCode(), modularResources)); resourceService.reportResources(new ReportResourceParam(scannerProperties.getAppCode(), modularResources));
// 设置标识已经扫描过 // 设置标识已经扫描过
InitScanFlagHolder.setFlag(); InitScanFlagHolder.setFlag();

View File

@ -23,9 +23,12 @@ public class GunsResourceAutoConfiguration {
public static final String SCANNER_PREFIX = "scanner"; public static final String SCANNER_PREFIX = "scanner";
@Value("${spring.application.name}") @Value("${spring.application.name:}")
private String springApplicationName; private String springApplicationName;
@Value("${server.servlet.context-path:}")
private String contextPath;
/** /**
* *
* *
@ -48,10 +51,13 @@ public class GunsResourceAutoConfiguration {
@ConditionalOnMissingBean(ApiResourceScanner.class) @ConditionalOnMissingBean(ApiResourceScanner.class)
@ConditionalOnProperty(prefix = GunsResourceAutoConfiguration.SCANNER_PREFIX, name = "open", havingValue = "true") @ConditionalOnProperty(prefix = GunsResourceAutoConfiguration.SCANNER_PREFIX, name = "open", havingValue = "true")
public ApiResourceScanner apiResourceScanner(ResourceCollectorApi resourceCollectorApi, ScannerProperties scannerProperties) { public ApiResourceScanner apiResourceScanner(ResourceCollectorApi resourceCollectorApi, ScannerProperties scannerProperties) {
if (StrUtil.isBlank(scannerProperties.getProjectCode())) { if (StrUtil.isBlank(scannerProperties.getAppCode())) {
scannerProperties.setProjectCode(springApplicationName); scannerProperties.setAppCode(springApplicationName);
} }
return new ApiResourceScanner(resourceCollectorApi, scannerProperties, scannerProperties.getAppCode()); if (StrUtil.isBlank(scannerProperties.getContextPath())) {
scannerProperties.setContextPath(contextPath);
}
return new ApiResourceScanner(resourceCollectorApi, scannerProperties);
} }
/** /**