优化架构的代码逻辑

This commit is contained in:
季圣华
2020-03-31 21:08:33 +08:00
parent f954061dba
commit 1aa5d1eb08

View File

@@ -13,48 +13,19 @@ import java.util.Map;
*/ */
@Service @Service
public class InterfaceContainer { public class InterfaceContainer {
private final Map<String, Integer> nameTypeMap; private final Map<String, ICommonQuery> configComponentMap = new HashMap<>();
private final Map<Integer, ICommonQuery> configComponentMap;
public InterfaceContainer() {
nameTypeMap = new HashMap<>();
configComponentMap = new HashMap<>();
}
@Autowired(required = false) @Autowired(required = false)
private void init(ICommonQuery[] configComponents) { private synchronized void init(ICommonQuery[] configComponents) {
for (ICommonQuery configComponent : configComponents) { for (ICommonQuery configComponent : configComponents) {
ResourceInfo info = AnnotationUtils.getAnnotation(configComponent, ResourceInfo.class); ResourceInfo info = AnnotationUtils.getAnnotation(configComponent, ResourceInfo.class);
if (info != null) { if (info != null) {
initResourceInfo(info); configComponentMap.put(info.value(), configComponent);
configComponentMap.put(info.type(), configComponent);
} }
} }
} }
public int getResourceType(String apiName) {
if (!nameTypeMap.containsKey(apiName)) {
throw new RuntimeException("资源:" + apiName + "的组件不存在");
}
return nameTypeMap.get(apiName);
}
public ICommonQuery getCommonQuery(String apiName) { public ICommonQuery getCommonQuery(String apiName) {
return getCommonQuery(this.getResourceType(apiName)); return configComponentMap.get(apiName);
} }
private ICommonQuery getCommonQuery(int resourceType) {
Assert.isTrue(configComponentMap.containsKey(resourceType));
return configComponentMap.get(resourceType);
}
private synchronized void initResourceInfo(ResourceInfo info) {
if (nameTypeMap.containsKey(info.value())) {
Assert.isTrue(nameTypeMap.get(info.value()).equals(info.type()));
} else {
nameTypeMap.put(info.value(), info.type());
}
}
} }