I7L5DX #I7L5DX 2.10.5版本中ScriptBean注解注入bean失败

This commit is contained in:
everywhere.z 2023-07-16 23:18:37 +08:00
parent 8cce43db12
commit 62419c4419
6 changed files with 95 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.exception.ComponentMethodDefineErrorException;
import com.yomahub.liteflow.exception.LiteFlowException;
import com.yomahub.liteflow.exception.ProxyException;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import com.yomahub.liteflow.util.LiteFlowProxyUtil;
@ -153,7 +154,7 @@ public class ComponentProxy {
return nodeComponent;
}
catch (Exception e) {
throw new LiteFlowException(e);
throw new ProxyException(e);
}
}).collect(Collectors.toList());
}

View File

@ -0,0 +1,31 @@
package com.yomahub.liteflow.exception;
/**
* @author Bryan.Zhang
*/
public class ProxyException extends RuntimeException {
private static final long serialVersionUID = 1L;
/** 异常信息 */
private String message;
public ProxyException(String message) {
this.message = message;
}
public ProxyException(Throwable cause) {
super(cause);
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -3,6 +3,7 @@ package com.yomahub.liteflow.script.proxy;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.*;
import com.yomahub.liteflow.exception.LiteFlowException;
import com.yomahub.liteflow.exception.ProxyException;
import com.yomahub.liteflow.exception.ScriptBeanMethodInvokeException;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
@ -10,8 +11,18 @@ import com.yomahub.liteflow.script.annotation.ScriptBean;
import com.yomahub.liteflow.util.LiteFlowProxyUtil;
import com.yomahub.liteflow.util.SerialsUtil;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.implementation.InvocationHandlerAdapter;
import net.bytebuddy.implementation.MethodCall;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bytecode.constant.DefaultValue;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;
@ -56,19 +67,18 @@ public class ScriptBeanProxy {
}
try {
return new ByteBuddy().subclass(orignalClass)
.name(StrUtil.format("{}.ByteBuddy${}", ClassUtil.getPackage(orignalClass),
SerialsUtil.generateShortUUID()))
Class<?> c = new ByteBuddy().subclass(orignalClass).name(StrUtil.format("{}.ByteBuddy${}", ClassUtil.getPackage(orignalClass),SerialsUtil.generateShortUUID()))
.method(ElementMatchers.any())
.intercept(InvocationHandlerAdapter.of(new AopInvocationHandler(bean, methodNameList)))
.annotateType(orignalClass.getAnnotations())
.make()
.load(ScriptBeanProxy.class.getClassLoader())
.getLoaded()
.newInstance();
.getLoaded();
return ReflectUtil.newInstanceIfPossible(c);
}
catch (Exception e) {
throw new LiteFlowException(e);
throw new ProxyException(e);
}
}

View File

@ -92,4 +92,13 @@ public class LiteFlowScriptScriptbeanGroovyELTest extends BaseTest {
Assertions.assertEquals("hello", context.get("demo"));
}
//测试用构造方法的方式注入bean的场景
@Test
public void testScriptBean8() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain8", "arg");
Assertions.assertTrue(response.isSuccess());
DefaultContext context = response.getFirstContextBean();
Assertions.assertEquals("hello,jordan", context.getData("demo"));
}
}

View File

@ -0,0 +1,26 @@
package com.yomahub.liteflow.test.script.groovy.scriptbean.bean;
import com.yomahub.liteflow.script.annotation.ScriptBean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@ScriptBean("demo5")
public class DemoBean5 {
private final DemoBean2 demoBean2;
public DemoBean5(DemoBean2 demoBean2) {
this.demoBean2 = demoBean2;
}
public String getDemoStr1() {
return "hello";
}
public String getDemoStr2(String name) {
return demoBean2.getDemoStr2(name);
}
}

View File

@ -50,6 +50,13 @@
abcCx.put("demo", str)
]]>
</node>
<node id="f" type="script" language="groovy">
<![CDATA[
def str = demo5.getDemoStr2("jordan")
defaultContext.setData("demo", str)
]]>
</node>
</nodes>
<chain name="chain1">
@ -79,4 +86,8 @@
<chain name="chain7">
THEN(a,b,c,s5);
</chain>
<chain name="chain8">
THEN(a,b,c,f);
</chain>
</flow>