feat: 升级版本,删除过时的reflections依赖,使用spring内置扫描方法实现
This commit is contained in:
parent
ea51622604
commit
33c5772738
@ -4,7 +4,7 @@ Rest Proxy组件是本人在长期的系统对接工作中提炼出的一个可
|
|||||||
具体是这样的:**写一个interface,定义几个method,然后加上注解,完成!**,在业务里直接调用method即可,不需要写任何实现类!
|
具体是这样的:**写一个interface,定义几个method,然后加上注解,完成!**,在业务里直接调用method即可,不需要写任何实现类!
|
||||||
|
|
||||||
## REST请求代理(基于Http Interface)
|
## REST请求代理(基于Http Interface)
|
||||||
本组件基于SpringBoot2.7.7构建,支持完整的AutoConfiguraiton。仅需要极少量配置就可以完成配置。当前最新版本为`1.1.3`,新增了标准REST注解。
|
本组件基于SpringBoot2.7.7构建,支持完整的AutoConfiguraiton。仅需要极少量配置就可以完成配置。当前最新版本为`1.1.4`,新增了标准REST注解。
|
||||||
|
|
||||||
### 特性列表
|
### 特性列表
|
||||||
- 无感知自动注入,只需interface + 注解即可轻松完成对接
|
- 无感知自动注入,只需interface + 注解即可轻松完成对接
|
||||||
@ -29,7 +29,7 @@ Rest Proxy组件是本人在长期的系统对接工作中提炼出的一个可
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy-core</artifactId>
|
<artifactId>rest-proxy-core</artifactId>
|
||||||
<version>1.1.3</version>
|
<version>1.1.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
9
pom.xml
9
pom.xml
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.1.3</version>
|
<version>1.1.4</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>8</maven.compiler.source>
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
@ -22,7 +22,7 @@
|
|||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<commons-collection.version>4.4</commons-collection.version>
|
<commons-collection.version>4.4</commons-collection.version>
|
||||||
<commons.lang.version>2.6</commons.lang.version>
|
<commons.lang.version>2.6</commons.lang.version>
|
||||||
<sdk.version>1.1.3</sdk.version>
|
<sdk.version>1.1.4</sdk.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
@ -45,11 +45,6 @@
|
|||||||
<artifactId>commons-collections4</artifactId>
|
<artifactId>commons-collections4</artifactId>
|
||||||
<version>${commons-collection.version}</version>
|
<version>${commons-collection.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.reflections</groupId>
|
|
||||||
<artifactId>reflections</artifactId>
|
|
||||||
<version>0.10.2</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy-api</artifactId>
|
<artifactId>rest-proxy-api</artifactId>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.1.3</version>
|
<version>1.1.4</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.1.3</version>
|
<version>1.1.4</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -10,18 +10,25 @@ import lombok.Getter;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.reflections.Reflections;
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.BeanFactoryAware;
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
|
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.*;
|
import org.springframework.beans.factory.support.*;
|
||||||
|
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
||||||
|
import org.springframework.core.type.classreading.MetadataReader;
|
||||||
|
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,13 +73,21 @@ public class RestApiRegistry implements BeanDefinitionRegistryPostProcessor, Bea
|
|||||||
if (DataUtils.isNotEmpty(packageNames)) {
|
if (DataUtils.isNotEmpty(packageNames)) {
|
||||||
// 初始化反射
|
// 初始化反射
|
||||||
try {
|
try {
|
||||||
Reflections reflections = new Reflections(packageNames.toArray());
|
RestServiceComponentProvider scanner = new RestServiceComponentProvider();
|
||||||
// 得到Resource注解的类
|
scanner.resetFilters(false);
|
||||||
Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(RestService.class);
|
scanner.addIncludeFilter(new AnnotationTypeFilter(RestService.class));
|
||||||
|
// 获取扫描器的ClassLoader,保证同源
|
||||||
|
ClassLoader cl = scanner.getClass().getClassLoader();
|
||||||
|
for (String packageName : packageNames) {
|
||||||
|
Set<BeanDefinition> bfs = scanner.findCandidateComponents(packageName);
|
||||||
// 不存在,不要浪费性能
|
// 不存在,不要浪费性能
|
||||||
if (CollectionUtils.isEmpty(classSet)) return;
|
if (CollectionUtils.isEmpty(bfs)) return;
|
||||||
// 代理并生成子类,并注册到ioc容器
|
// 代理并生成子类,并注册到ioc容器
|
||||||
classSet.forEach(clazz -> registry.registerBeanDefinition(clazz.getName(), generate(clazz)));
|
bfs.stream()
|
||||||
|
.map(bf -> resolveType(bf, cl))
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.forEach(clazz -> registry.registerBeanDefinition(clazz.getName(), generate(clazz)));
|
||||||
|
}
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
log.error("初始化Rest映射时出错", e);
|
log.error("初始化Rest映射时出错", e);
|
||||||
}
|
}
|
||||||
@ -103,4 +118,41 @@ public class RestApiRegistry implements BeanDefinitionRegistryPostProcessor, Bea
|
|||||||
Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory, "当前bean factory不被支持!");
|
Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory, "当前bean factory不被支持!");
|
||||||
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
|
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Class<?> resolveType(BeanDefinition bf, ClassLoader cl) {
|
||||||
|
if (null != bf.getBeanClassName()) {
|
||||||
|
try {
|
||||||
|
return ClassUtils.forName(bf.getBeanClassName(), cl);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内部的包扫描器,提供特定注解扫描
|
||||||
|
*
|
||||||
|
* @author wangyu
|
||||||
|
*/
|
||||||
|
private static class RestServiceComponentProvider extends ClassPathScanningCandidateComponentProvider {
|
||||||
|
|
||||||
|
private final AnnotationTypeFilter filter = new AnnotationTypeFilter(RestService.class);
|
||||||
|
|
||||||
|
private RestServiceComponentProvider() {
|
||||||
|
super(false);
|
||||||
|
resetFilters(false);
|
||||||
|
addIncludeFilter(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isCandidateComponent(@NonNull MetadataReader metadataReader) throws IOException {
|
||||||
|
return filter.match(metadataReader, getMetadataReaderFactory());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isCandidateComponent(@NonNull AnnotatedBeanDefinition beanDefinition) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user