com.addplus.server.action.config.system.SystemActionConfig Maven / Gradle / Ivy
The newest version!
package com.addplus.server.action.config.system;
import com.addplus.server.core.model.base.ServiceMap;
import com.addplus.server.core.utils.PackageUtil;
import com.addplus.server.core.utils.security.DecriptUtil;
import com.esotericsoftware.reflectasm.MethodAccess;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import javax.validation.constraints.Null;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.stream.Collectors;
/**
* 类名: SystemActionConfig
*
* @author zhangjiehang
* @version V1.0
* 时间: 2017-10-24 10:34
* 类描述: 系统请求初始化bean
*/
@Configuration
public class SystemActionConfig {
@Value("${action.package.name}")
private String packageName;
@Value("${action.package.base}")
private String base;
@Value("${action.package.baseService:com.addplus.server.api.base.BaseService}")
private String baseServiceName;
@Bean
public Set> getPackageClass() {
Assert.notNull(this.packageName, "packageName must not be null!");
String[] packageBase = packageName.split(",");
if (packageBase.length == 0) {
return null;
}
// 获取所有service
Set> allClasses = new HashSet>();
for (String tmp : packageBase) {
Set> classes = PackageUtil.getClasses(tmp).stream().filter(k -> k.isInterface()).collect(Collectors.toSet());
allClasses.addAll(classes);
}
return allClasses;
}
@Bean
public ServiceMap getInfos(Set> getPackageClass) {
Map describeMap = new HashMap<>();
Map serviceClassMap = new HashMap<>();
Map serviceParameterMap = new HashMap<>();
// 生成service所需要的容器环境
if (getPackageClass != null && !getPackageClass.isEmpty()) {
Assert.notNull(this.baseServiceName, "baseServiceName must not be null!");
String[] baseService = baseServiceName.split(",");
List baseList = new ArrayList<>();
if (baseService.length > 0) {
baseList.addAll(Arrays.asList(baseService));
}
for (Class tmp : getPackageClass) {
Method[] methods = tmp.getMethods();
for (Method method : methods) {
if (!method.getDeclaringClass().getName().equals(tmp.getName())) {
if (baseList.isEmpty() || !baseList.contains(method.getDeclaringClass().getName())) {
continue;
}
}
Parameter[] parameters = method.getParameters();
serviceParameterMap.put(tmp.getName() + "_" + method.getName(), parameters);
describeMap.put(tmp.getName() + "_" + method.getName(), DecriptUtil.SHA1(method.getName()).substring(0, 16));
}
serviceClassMap.put(tmp.getName(), tmp);
}
}
ServiceMap serviceMap = new ServiceMap();
serviceMap.setDecrtipetMap(describeMap);
serviceMap.setServiceClassMap(serviceClassMap);
serviceMap.setServiceParameterMap(serviceParameterMap);
return serviceMap;
}
@Bean
public AddplusContainer getAddPlusContainer(ServiceMap serviceMap) {
Assert.notNull(this.base, "package base must not be null!");
return AddplusContainer.newInstance(serviceMap, this.base);
}
}