Skip to content

Commit

Permalink
新增基础组件解析
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchangb committed Jun 29, 2020
1 parent 0bfdbb4 commit 8ed87aa
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 3 deletions.
8 changes: 7 additions & 1 deletion service-flow-denpendencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<spring.cloud.dependencies.version>Hoxton.SR4</spring.cloud.dependencies.version>
<spring.cloud.alibaba.dependencies.version>2.1.0.RELEASE</spring.cloud.alibaba.dependencies.version>
<fatjson.version>1.2.68</fatjson.version>
<reflections.version>0.9.11</reflections.version>
<reflections.version>0.9.12</reflections.version>
<javassist.version>3.26.0-GA</javassist.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -48,6 +49,11 @@
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
6 changes: 6 additions & 0 deletions service-flow-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>

</dependency>

<!-- <dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions service-flow-sdk/src/main/java/com/service/flow/api/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
String name();

FlowTypeEnum type() default FlowTypeEnum.METHOD;

String desc() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.service.flow.index;

import org.reflections.Reflections;
import org.reflections.scanners.FieldAnnotationsScanner;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.scanners.MethodParameterScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ConfigurationBuilder;

/**
* @author zhangcb
* @ClassName ReflectionsScan.java
* @Description TODO
* @createTime 2020年06月29日 21:25:00
*/
public class ReflectionsScan {
public static Reflections scan(String scanName){
// 扫包
Reflections reflections = new Reflections(new ConfigurationBuilder()
.forPackages(scanName) // 指定路径URL
.addScanners(new SubTypesScanner()) // 添加子类扫描工具
.addScanners(new FieldAnnotationsScanner()) // 添加 属性注解扫描工具
.addScanners(new MethodAnnotationsScanner() ) // 添加 方法注解扫描工具
.addScanners(new MethodParameterScanner() ) // 添加方法参数扫描工具
);
return reflections;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.service.flow.index.component;

import com.service.flow.api.DTO;
import com.service.flow.api.Flow;
import com.service.flow.api.FlowTypeEnum;
import com.service.flow.index.ReflectionsScan;
import com.service.flow.index.dto.DTODefinition;
import org.reflections.Reflections;
import org.reflections.scanners.*;
import org.reflections.util.ConfigurationBuilder;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* @author zhangcb
* @ClassName BaseComponentClassLoader.java
* @Description TODO
* @createTime 2020年06月29日 20:29:00
*/
public class BaseComponentClassLoader {

private Map<String, ComponentDefinition> componentDefinitionMap;

public BaseComponentClassLoader(){
this.componentDefinitionMap = new HashMap<>();
}

public void load(String scanName) {
// 扫包
Reflections reflections = ReflectionsScan.scan(scanName);
// 获取带有特定注解对应的方法
Set<Method> methods = reflections.getMethodsAnnotatedWith(Flow.class ) ;
Iterator<Method> iterator = methods.iterator();
while (iterator.hasNext()){
Method method = iterator.next();
Flow annotation = method.getAnnotation(Flow.class);
String name = annotation.name();
FlowTypeEnum type = annotation.type();
String desc = annotation.desc();
String methodName = method.getName();
String className = method.getDeclaringClass().getName();
ComponentDefinition componentDefinition = new ComponentDefinition();
componentDefinition.setValue(name);
componentDefinition.setClassName(className);
componentDefinition.setMethodName(methodName);
componentDefinition.setDesc(desc);
this.componentDefinitionMap.put(name,componentDefinition);
}
}

public Map<String,ComponentDefinition> getComponentDefinitionMap(){
return this.componentDefinitionMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.service.flow.index.component;


import com.service.flow.index.dto.DTODefinition;

/**
* @author zhangcb
* @ClassName ComponentDefinition.java
* @Description TODO
* @createTime 2020年06月29日 07:15:00
*/
public class ComponentDefinition {

private String value;

private String desc;

private String className;

private String methodName;

private DTODefinition dtoDefinition;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getClassName() {
return className;
}

public void setClassName(String className) {
this.className = className;
}

public String getMethodName() {
return methodName;
}

public void setMethodName(String methodName) {
this.methodName = methodName;
}

public DTODefinition getDtoDefinition() {
return dtoDefinition;
}

public void setDtoDefinition(DTODefinition dtoDefinition) {
this.dtoDefinition = dtoDefinition;
}

@Override
public String toString() {
return "ComponentDefinition{" +
"value='" + value + '\'' +
", desc='" + desc + '\'' +
", className='" + className + '\'' +
", methodName='" + methodName + '\'' +
", dtoDefinition=" + dtoDefinition +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.service.flow.index.dto;

import com.service.flow.api.DTO;
import com.service.flow.index.ReflectionsScan;
import org.reflections.Reflections;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
Expand All @@ -25,8 +26,8 @@ public DTOClassLoader(){
this.dtoDefinitionMap = new HashMap<>();
}

public void load() {
Reflections reflections = new Reflections("com.service");
public void load(String scanName) {
Reflections reflections = ReflectionsScan.scan(scanName);
Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(DTO.class);
Iterator<Class<?>> iterator = typesAnnotatedWith.iterator();
while (iterator.hasNext()){
Expand Down

0 comments on commit 8ed87aa

Please sign in to comment.