🍣Interact with java
# Interact with data context
Because LiteFlow component data is in the context, in a scripting language you can get the data by the camelCase of the className where you define the data context.
For example, your context class is called OrderContext
, then the keyword orderContext
is used in the script to obtain data or call methods.
If you have multiple contexts, you can also use multiple contexts in your script to get data or call logic methods inside the context.
For example, you want to get the userName
object in the UserContext
. You can write (using groovy as an example):
<node id="s1" name="common script component" type="script">
<![CDATA[
//you can define it like this
def name = userContext.userName;
//It can also be defined like this, if you have a getter method for userName
def name = userContext.getUserName();
//You can call any method in the data context
userContext.doYourMethod();
]]>
</node>
# Meta data
Metadata can be obtained through the _meta
keyword in the script, and metadata can be obtained through _meta.xxx
. The metadata includes:
- slotIndex: slot subscript, you can get slot data through FlowBus.getSlot(slotIndex)
- currChainId: The id of the currently executing chain
- nodeId: The ID of the currently executing node
- tag: tag value, please refer to Component tag for the description of tag
- cmpData: Component rule parameters, please refer to Component parameters for the description of cmpData
- requestData: Process initial parameters
- subRequestData: The input parameters of the current implicit process, if this node is arranged in the implicit process, it can be obtained, otherwise it cannot
# Interact with custom JavaBeans
Since v2.9.0, LiteFlow supports injecting any java object you define yourself in the script.
In the spring system, you only need to use the @ScriptBean
annotation on the java object.
@Component
@ScriptBean("demo")
public class DemoBean1 {
@Resource
private DemoBean2 demoBean2;
public String getDemoStr1(){
return "hello";
}
public String getDemoStr2(String name){
return demoBean2.getDemoStr2(name);
}
}
In the above example, the demo
keyword can be used directly in the script to call the getDemoStr1()
and getDemoStr2()
methods.
notice
It should be noted that the java object must be registered in the context in the spring system. If it is not registered in the context, it is useless to add the @ScriptBean
annotation.
Under the non-spring system, if you want to inject your own defined java object into the script, you need to write the code manually (preferably when starting the application):
ScriptBeanManager.addScriptBean("demo", new DemoBean());