新的声明式改造支持低版本的spring

This commit is contained in:
everywhere.z
2024-01-22 16:12:47 +08:00
parent cfc1994fd4
commit dd9d2014be

View File

@@ -4,6 +4,8 @@ import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.util.ReflectUtil;
import com.yomahub.liteflow.annotation.LiteflowMethod;
import com.yomahub.liteflow.core.proxy.DeclWarpBean;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import com.yomahub.liteflow.spi.holder.DeclComponentParserHolder;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
@@ -12,6 +14,7 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -23,38 +26,42 @@ import java.util.Map;
* @since 2.11.4
*/
public class DeclBeanDefinition implements BeanDefinitionRegistryPostProcessor {
private final LFLog LOG = LFLoggerManager.getLogger(this.getClass());
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) registry;
Map<String, BeanDefinition> beanDefinitionHolderMap = (Map<String, BeanDefinition>)ReflectUtil.getFieldValue(defaultListableBeanFactory, "mergedBeanDefinitions");
beanDefinitionHolderMap.entrySet().stream().filter(entry -> {
Class<?> rawClass = entry.getValue().getResolvableType().getRawClass();
String[] beanDefinitionNames = defaultListableBeanFactory.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).filter(beanName -> {
BeanDefinition beanDefinition = defaultListableBeanFactory.getMergedBeanDefinition(beanName);
Class<?> rawClass = getRawClassFromBeanDefinition(beanDefinition);
if (rawClass == null){
return false;
}else{
return Arrays.stream(rawClass.getMethods()).anyMatch(method -> AnnotationUtil.getAnnotation(method, LiteflowMethod.class) != null);
}
}).forEach(entry -> {
Class<?> rawClass = entry.getValue().getResolvableType().getRawClass();
}).forEach(beanName -> {
BeanDefinition beanDefinition = defaultListableBeanFactory.getMergedBeanDefinition(beanName);
Class<?> rawClass = getRawClassFromBeanDefinition(beanDefinition);
List<DeclWarpBean> declWarpBeanList = DeclComponentParserHolder.loadDeclComponentParser().parseDeclBean(rawClass);
declWarpBeanList.forEach(declWarpBean -> {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(DeclWarpBean.class);
beanDefinition.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON);
GenericBeanDefinition newBeanDefinition = new GenericBeanDefinition();
newBeanDefinition.setBeanClass(DeclWarpBean.class);
newBeanDefinition.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON);
MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
mutablePropertyValues.add("nodeId", declWarpBean.getNodeId());
mutablePropertyValues.add("nodeName", declWarpBean.getNodeName());
mutablePropertyValues.add("nodeType", declWarpBean.getNodeType());
mutablePropertyValues.add("rawClazz", declWarpBean.getRawClazz());
mutablePropertyValues.add("methodWrapBeanList", declWarpBean.getMethodWrapBeanList());
mutablePropertyValues.add("rawBean", entry.getValue());
beanDefinition.setPropertyValues(mutablePropertyValues);
mutablePropertyValues.add("rawBean", beanDefinition);
newBeanDefinition.setPropertyValues(mutablePropertyValues);
defaultListableBeanFactory.setAllowBeanDefinitionOverriding(true);
defaultListableBeanFactory.registerBeanDefinition(declWarpBean.getNodeId(), beanDefinition);
defaultListableBeanFactory.registerBeanDefinition(declWarpBean.getNodeId(), newBeanDefinition);
});
});
}
@@ -62,4 +69,19 @@ public class DeclBeanDefinition implements BeanDefinitionRegistryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
private Class<?> getRawClassFromBeanDefinition(BeanDefinition beanDefinition){
try{
Method method = ReflectUtil.getMethodByName(DeclBeanDefinition.class, "getResolvableType");
if (method != null){
Object resolvableType = ReflectUtil.invoke(beanDefinition, method);
return ReflectUtil.invoke(resolvableType, "getRawClass");
}else{
return ReflectUtil.invoke(beanDefinition, "getTargetType");
}
}catch (Exception e){
LOG.error("An error occurred while obtaining the rowClass.",e);
return null;
}
}
}