Refactor: 补充 SpringIdHolder ,空校验与异常类

This commit is contained in:
LuanY77
2025-07-23 13:56:27 +08:00
parent 215458eade
commit bfbb826c76
4 changed files with 66 additions and 16 deletions

View File

@@ -64,9 +64,6 @@ public class AIComponentFactory {
* @return NodeComponent实例如果不是AI组件则返回null
*/
public NodeComponent createAIComponent(Class<?> interfaceClass, String beanName) {
LOG.info("Attempting to create AI component for interface: {}, beanName: {}",
interfaceClass.getName(), beanName);
// 检查是否是AI组件
AIComponent aiComponent = interfaceClass.getAnnotation(AIComponent.class);
if (Objects.isNull(aiComponent)) {

View File

@@ -1,14 +1,14 @@
package com.yomahub.liteflow.ai.proxy;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import com.yomahub.liteflow.process.holder.SpringNodeIdHolder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import java.util.Objects;
/**
* AI组件后置处理器
*
@@ -38,16 +38,12 @@ public class AIComponentPostProcessor implements BeanPostProcessor, Ordered {
// 使用工厂创建AI组件
NodeComponent aiComponent = aiComponentFactory.createAIComponent(clazz, beanName);
if (Objects.nonNull(aiComponent)) {
LOG.info("Successfully created AI component for interface: {}, replacing bean: {}",
clazz.getName(), beanName);
return aiComponent;
} else {
LOG.warn("Failed to create AI component for interface: {}, returning original bean",
clazz.getName());
return bean;
}
LOG.info("AI proxy component[{}] has been created for interface: {}", beanName, clazz.getName());
String nodeId = StrUtil.isNotBlank(aiComponent.getNodeId()) ? aiComponent.getNodeId() : SpringNodeIdHolder.getRealBeanName(clazz, beanName);
SpringNodeIdHolder.add(nodeId);
return aiComponent;
} catch (Exception e) {
LOG.error("Error creating AI component for interface: {}, beanName: {}",
clazz.getName(), beanName, e);

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.ai.annotation.AIComponent;
import com.yomahub.liteflow.ai.enums.AITypeEnum;
import com.yomahub.liteflow.ai.proxy.wrap.AIProxyWrapBean;
import com.yomahub.liteflow.ai.util.SetUtil;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.exception.ProxyException;
import com.yomahub.liteflow.log.LFLog;
@@ -136,8 +137,8 @@ public abstract class AbstractAIComponentHandler<T extends Annotation> {
.getLoaded()
.newInstance();
nodeComponent.setNodeId(wrapBean.getNodeId());
nodeComponent.setName(wrapBean.getNodeName());
SetUtil.setIfPresent(nodeComponent::setNodeId, wrapBean.getNodeId());
SetUtil.setIfPresent(nodeComponent::setName, wrapBean.getNodeName());
LOG.info("Created AI component: {}, beanName: {}, type: {}",
nodeComponent.getNodeId(), wrapBean.getBeanName(), aiType);

View File

@@ -0,0 +1,56 @@
package com.yomahub.liteflow.ai.util;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
/**
* TODO
*
* @author 苍镜月
* @since TODO
*/
public class SetUtil {
/**
* 如果给定的值不为 null 或 "空" 或 默认值,则调用消费者。
*
* @param consumer 要执行的操作。
* @param value 要检查的值。
* @param <T> 值的类型。
*/
public static <T> void setIfPresent(Consumer<T> consumer, T value) {
if (!isNullOrEmptyOrDefault(value)) {
consumer.accept(value);
}
}
/**
* 检查给定的对象是否为 null 或 "空" 或 默认值
* 如果一个对象是 String、Collection、Map 或数组,且其元素个数或长度为零,
* 那么它被认为是"空"的。
* 如果一个对象是 TriState 类型,并且其值为 UNSET那么认为它是默认值
*
* @param value 要检查的对象。
* @return 如果对象为 null 或 “空” 或 默认值,则返回 true否则返回 false。
*/
private static boolean isNullOrEmptyOrDefault(Object value) {
if (Objects.isNull(value)) {
return true;
} else if (value instanceof Collection) {
return ((Collection<?>) value).isEmpty();
} else if (value instanceof Map) {
return ((Map<?, ?>) value).isEmpty();
} else if (value instanceof String) {
return ((String) value).trim().isEmpty();
} else if (value.getClass().isArray()) {
return Array.getLength(value) == 0;
} else if (value instanceof TriState) {
return value == TriState.UNSET;
}
return false;
}
}