ModuleExcutorFactory
package com.cloud.gcm.excutor;
import com.cloud.gcm.config.ModuleAnnotation;
import com.cloud.gcm.service.common.ModuleConvert;
import com.cloud.gcm.util.AopTargetUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @description: 转换器工厂
* @author: wangzhixiang
* @create: 2020-10-27 20:19
**/
@Component
public class ModuleExcutorFactory implements ApplicationContextAware {
private static Map<String, ModuleConvert> executors = new HashMap<>();
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T extends ModuleConvert> T getExecutor(String module) {
return (T)executors.get(module);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(executors.size() > 0 ) {
return;
}
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ModuleAnnotation.class);
if(beans!=null && !beans.isEmpty()) {
Iterator<Map.Entry<String, Object>> iterator = beans.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
if(entry.getValue() instanceof ModuleConvert) {
@SuppressWarnings("rawtypes")
ModuleConvert bean = (ModuleConvert)entry.getValue();
ModuleAnnotation instanceInfo = null;
try {
instanceInfo = AopTargetUtils.getTarget(bean).getClass().getAnnotation(ModuleAnnotation.class);
} catch (Exception e) {
e.printStackTrace();
}
String key = instanceInfo.module();
executors.put(key, bean);
}
}
}
}
}
ModuleConvert
public R updateUserStatus(UserStatusRecordEntity entity, Integer scene) {
//throw new RRException(ErrorCode.SYS_ERROR);
return R.success();
}
BHOSTModuleConvert
package com.cloud.gcm.service.impl.common;
import com.cloud.gcm.cloudapi.bhost.BHostReqDTO;
import com.cloud.gcm.config.ModuleAnnotation;
import com.cloud.gcm.config.R;
import com.cloud.gcm.entity.api.UserStatusRecordEntity;
import com.cloud.gcm.mapper.api.UserStatusRecordMapper;
import com.cloud.gcm.service.common.ModuleConvert;
import com.cloud.gcm.service.common.TaskLogService;
import com.cloud.gcm.service.fortress.FortressApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@ModuleAnnotation(module = "BHOST")
@Component
public class BHOSTModuleConvert extends ModuleConvert {
@Autowired
FortressApiService fortressApiService;
@Autowired
UserStatusRecordMapper userStatusRecordMapper;
@Autowired
TaskLogService taskLogService;
@Override
public R updateUserStatus(UserStatusRecordEntity item, Integer scene) {
R result = new R();
return result;
}
}
调用代码:
ModuleExcutorFactory.getExecutor(item.getModule()).updateUserStatus(item, 1);