How to access Spring Beans in Activiti JavaDelegate tasks
I am using activiti 5.16.1 to enable workflow in my project.
I have spring beans that do a business task like populating data from a database for a given id.
When I try to use these beans in a JavaDelegate task, these beans are not populated.
@Component
public class ServiceClassDelegateSample implements JavaDelegate {
@Autowired
private SampleService sampleService;
@Override
public void execute(DelegateExecution delegateExecution) throws Exception {
sampleService.doSomeTask();
}
}
However, sampleService is always null.
Do you know how to use spring beans in JavaDelegate task?
There is a similar question here without a proper solution and I hope there is a better way in the newer version of Activiti.
source to share
If I remember correctly, this is some kind of glitch in Activiti, but you can easily solve it if you replace
<serviceTask id="servicetask" name="Service Task" activiti:class="some.class.path.ServiceClassDelegateSample"></serviceTask>
with a delegate expression;
<serviceTask id="serviceTask" activiti:delegateExpression="${serviceClassDelegateSample}"></serviceTask>
where serviceClassDelegateSample
is the name of your bean.
But why doesn't work in the first place, I don't know.
source to share