🌯Definition and use
# Default context
LiteFlow provides a default implementation of the data context: DefaultContext
. This default implementation is actually a Map, which is the main container for storing data.
You can put data in through the setData
method in the DefaultContext
and get data through the getData
method.
suggestion
DefaultContext
can be used, but in the actual business, there will be a lot of weak types with this, and it is quite inconvenient to convert all the time when accessing data. So it is officially recommended that you implement your own data context.
# Customized context
You can use any of your own beans as a context to pass in; LiteFlow has no requirements for the context bean.
The context you define is essentially a simple value object, and the context you define is more relevant to the business because it is strongly typed.
You can do incoming like this:
LiteflowResponse response = flowExecutor.execute2Resp("chain1", 流程初始参数, CustomContext.class);
Once passed in, LiteFlow will initialize it when called, assigning a unique instance to the context. You can get this context instance in the component like this:
@LiteflowComponent("yourCmpId")
public class YourCmp extends NodeComponent {
@Override
public void process() {
CustomContext context = this.getContextBean(CustomContext.class);
//Or you can use this method to get a context instance, which is equivalent to the above
//CustomContext context = this.getFirstContextBean();
...
}
}
Please refer to Common Components for more information about what other default parameters are available in the component.
# Multi-Context
LiteFlow supports multiple contexts in the new version, the framework will initialize the multiple contexts you pass in before execution. It's also easy to get in the component based on the class type.
You can pass it in like this (scroll back if you can't see it all).
LiteflowResponse response = flowExecutor.execute2Resp("chain1", parameters, OrderContext.class, UserContext.class, SignContext.class);
The context instance can be obtained in the component as follows:
@LiteflowComponent("yourCmpId")
public class YourCmp extends NodeComponent {
@Override
public void process() {
OrderContext orderContext = this.getContextBean(OrderContext.class);
UserContext userContext = this.getContextBean(UserContext.class);
SignContext signContext = this.getContextBean(SignContext.class);
//If you only want to get the first context, and the first context is OrderContext, then you can also use this method
//OrderContext orderContext = this.getFirstContextBean();
...
}
}