package cn.hsa.spp.comm.factory;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
@SuppressWarnings("all")
public class HandlerAwareFactory implements InitializingBean, ApplicationContextAware {
private ApplicationContext applicationContext;
private Map<String,HandlerFactory> handlerMap = new ConcurrentHashMap<>();
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public Map<String, HandlerFactory> getHandlerMap() {
return handlerMap;
}
@Override
public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
Map<String, HandlerFactory> beanMap = applicationContext.getBeansOfType(HandlerFactory.class);
for (String key : beanMap.keySet()) {
this.handlerMap.put(beanMap.get(key).getType(), beanMap.get(key));
}
}
}
package cn.hsa.spp.comm.factory;
import org.springframework.stereotype.Component;
@Component
public abstract class HandlerFactory<T> {
private String type;
private String typeName;
protected HandlerFactory() {
}
public String getType(){
return type;
}
public String getTypeName() {
return typeName;
}
public void handler(T t){}
}
package cn.hsa.spp.tradeinterface.handler;
import cn.hsa.spp.comm.factory.HandlerFactory;
import cn.hsa.spp.tradeinterface.dto.ProcNameBdDTO;
import org.springframework.stereotype.Component;
@Component
public class ReflectInClassLogHandler extends HandlerFactory<ProcNameBdDTO> {
@Override
public String getType() {
return "reflectInClassLog";
}
@Override
public String getTypeName() {
return "入参日志";
}
@Override
public void handler(ProcNameBdDTO procNameBdDTO) {
System.out.println("入参日志==>"+procNameBdDTO);
}
}
package cn.hsa.spp.tradeinterface.handler;
import cn.hsa.spp.comm.factory.HandlerFactory;
import org.springframework.stereotype.Component;
@Component
public class ReflectOutClassLogHandler extends HandlerFactory<String> {
@Override
public String getType() {
return "reflectOutClassLog";
}
@Override
public String getTypeName() {
return "结果日志";
}
@Override
public void handler(String selectJson) {
System.out.println("结果日志==>结果日志" + selectJson);
}
}
// 参数拦截器
Optional.ofNullable(dto.getReflectInClass()).map(in -> {
handlerAwareFactory.getHandlerMap().get(dto.getReflectInClass()).handler(dto);
return true;
});
// 结果拦截器
Optional.ofNullable(dto.getReflectOutClass()).map(out -> {
handlerAwareFactory.getHandlerMap().get(dto.getReflectOutClass()).handler(selectJson);
return true;
});
|