mirror of https://gitee.com/stylefeng/roses
【scanner】增加两个scanner扫描到url是否添加app和context-path的开关
parent
995b7e791b
commit
4579ddffd1
|
@ -17,27 +17,36 @@ public class ScannerProperties {
|
|||
private Boolean open;
|
||||
|
||||
/**
|
||||
* 被扫描应用的名称
|
||||
* 扫描到的资源的url是否要带appCode属性,此值默认为false
|
||||
* <p>
|
||||
* 也就是资源的url上不会带appCode属性,一般在微服务的系统中需要把此值设为true
|
||||
*/
|
||||
private String appName;
|
||||
private Boolean urlWithAppCode = false;
|
||||
|
||||
/**
|
||||
* 应用的编码
|
||||
* 项目编码(如果您不设置的话,默认使用spring.application.name填充,一般不用手动设置此值)
|
||||
*/
|
||||
private String appCode;
|
||||
|
||||
/**
|
||||
* 链接符号
|
||||
* 扫描到的url是否要带context-path,默认为true也就是会带上
|
||||
* <p>
|
||||
* 如果设置为true,则资源的url属性会带当前项目的context-path
|
||||
* <p>
|
||||
* 如果设置为false,则资源url属性不会带context-path
|
||||
* <p>
|
||||
* 如果urlWithAppCode开关和urlWithContextPath都开了,生成的url会是如下: /appCode/contextPath/xxx
|
||||
*/
|
||||
private Boolean urlWithContextPath = true;
|
||||
|
||||
/**
|
||||
* 项目的context-path
|
||||
*/
|
||||
private String contextPath;
|
||||
|
||||
/**
|
||||
* 链接符号,一般不要修改此符号
|
||||
*/
|
||||
private String linkSymbol = "$";
|
||||
|
||||
/**
|
||||
* 项目编码(如果您不设置的话,默认使用spring.application.name填充,请不要设置此值,这个值和网关资源过滤有关)
|
||||
* <p>
|
||||
* 修复一个项目启动的时候会误删别的项目资源的问题
|
||||
*
|
||||
* @since 2.2.12
|
||||
*/
|
||||
private String projectCode;
|
||||
|
||||
}
|
||||
|
|
|
@ -50,15 +50,9 @@ public class ApiResourceScanner implements BeanPostProcessor {
|
|||
*/
|
||||
private final ScannerProperties scannerProperties;
|
||||
|
||||
/**
|
||||
* 项目名称
|
||||
*/
|
||||
private final String springApplicationName;
|
||||
|
||||
public ApiResourceScanner(ResourceCollectorApi resourceCollectorApi, ScannerProperties scannerProperties, String springApplicationName) {
|
||||
public ApiResourceScanner(ResourceCollectorApi resourceCollectorApi, ScannerProperties scannerProperties) {
|
||||
this.resourceCollectorApi = resourceCollectorApi;
|
||||
this.scannerProperties = scannerProperties;
|
||||
this.springApplicationName = springApplicationName;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -229,7 +223,7 @@ public class ApiResourceScanner implements BeanPostProcessor {
|
|||
resourceDefinition.setRequiredPermission(requiredPermission);
|
||||
resourceDefinition.setMenuFlag(menuFlag);
|
||||
resourceDefinition.setName(name);
|
||||
resourceDefinition.setUrl(getControllerClassRequestPath(clazz) + path[0]);
|
||||
resourceDefinition.setUrl(getControllerClassRequestPath(clazz, path[0]));
|
||||
StringBuilder methodNames = new StringBuilder();
|
||||
for (RequestMethod requestMethod : requestMethods) {
|
||||
methodNames.append(requestMethod.name()).append(",");
|
||||
|
@ -251,7 +245,7 @@ public class ApiResourceScanner implements BeanPostProcessor {
|
|||
resourceDefinition.setCreateTime(new Date());
|
||||
|
||||
// 填充项目编码
|
||||
resourceDefinition.setProjectCode(scannerProperties.getProjectCode());
|
||||
resourceDefinition.setProjectCode(scannerProperties.getAppCode());
|
||||
|
||||
// 填充方法的校验分组
|
||||
Set<String> methodValidateGroup = MethodReflectUtil.getMethodValidateGroup(method);
|
||||
|
@ -277,28 +271,48 @@ public class ApiResourceScanner implements BeanPostProcessor {
|
|||
}
|
||||
|
||||
/**
|
||||
* 获取控制器类上的RequestMapping注解的映射路径,用于拼接path
|
||||
* <p>
|
||||
* 2018年5月2日修改,控制器路径前加上spring.application.name
|
||||
* 根据控制器类上的RequestMapping注解的映射路径,以及方法上的路径,拼出整个接口的路径
|
||||
*
|
||||
* @param clazz 控制器类
|
||||
* @param path 当前被扫描接口的path路径
|
||||
* @author fengshuonan
|
||||
* @date 2020/12/14 22:17
|
||||
*/
|
||||
private String getControllerClassRequestPath(Class<?> clazz) {
|
||||
String result;
|
||||
private String getControllerClassRequestPath(Class<?> clazz, String path) {
|
||||
String controllerPath;
|
||||
|
||||
ApiResource controllerRequestMapping = clazz.getDeclaredAnnotation(ApiResource.class);
|
||||
if (controllerRequestMapping == null) {
|
||||
result = "";
|
||||
controllerPath = "";
|
||||
} else {
|
||||
String[] paths = controllerRequestMapping.path();
|
||||
if (paths.length > 0) {
|
||||
result = paths[0];
|
||||
controllerPath = paths[0];
|
||||
} else {
|
||||
result = "";
|
||||
controllerPath = "";
|
||||
}
|
||||
}
|
||||
|
||||
// 拼接spring.application.name
|
||||
result = "/" + springApplicationName + result;
|
||||
return result;
|
||||
// 拼接最终url的时候,依据如下规则拼接:/appCode/contextPath/xxx
|
||||
// 第一部分是appCode
|
||||
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 {
|
||||
Class<? extends Annotation> annotationType = apiResource.annotationType();
|
||||
Method method = annotationType.getMethod(methodName);
|
||||
return (T)method.invoke(apiResource);
|
||||
return (T) method.invoke(apiResource);
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
log.error("扫描api资源时出错!", e);
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ public class ResourceReportListener implements ApplicationListener<ApplicationRe
|
|||
|
||||
// 持久化资源,发送资源到资源服务或本项目(单体项目)
|
||||
ResourceReportApi resourceService = applicationContext.getBean(ResourceReportApi.class);
|
||||
resourceService.reportResources(new ReportResourceParam(scannerProperties.getProjectCode(), modularResources));
|
||||
resourceService.reportResources(new ReportResourceParam(scannerProperties.getAppCode(), modularResources));
|
||||
|
||||
// 设置标识已经扫描过
|
||||
InitScanFlagHolder.setFlag();
|
||||
|
|
|
@ -23,9 +23,12 @@ public class GunsResourceAutoConfiguration {
|
|||
|
||||
public static final String SCANNER_PREFIX = "scanner";
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
@Value("${spring.application.name:}")
|
||||
private String springApplicationName;
|
||||
|
||||
@Value("${server.servlet.context-path:}")
|
||||
private String contextPath;
|
||||
|
||||
/**
|
||||
* 资源扫描器的配置
|
||||
*
|
||||
|
@ -48,10 +51,13 @@ public class GunsResourceAutoConfiguration {
|
|||
@ConditionalOnMissingBean(ApiResourceScanner.class)
|
||||
@ConditionalOnProperty(prefix = GunsResourceAutoConfiguration.SCANNER_PREFIX, name = "open", havingValue = "true")
|
||||
public ApiResourceScanner apiResourceScanner(ResourceCollectorApi resourceCollectorApi, ScannerProperties scannerProperties) {
|
||||
if (StrUtil.isBlank(scannerProperties.getProjectCode())) {
|
||||
scannerProperties.setProjectCode(springApplicationName);
|
||||
if (StrUtil.isBlank(scannerProperties.getAppCode())) {
|
||||
scannerProperties.setAppCode(springApplicationName);
|
||||
}
|
||||
return new ApiResourceScanner(resourceCollectorApi, scannerProperties, scannerProperties.getAppCode());
|
||||
if (StrUtil.isBlank(scannerProperties.getContextPath())) {
|
||||
scannerProperties.setContextPath(contextPath);
|
||||
}
|
||||
return new ApiResourceScanner(resourceCollectorApi, scannerProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue