2

I have a question about spring bean injection in service tasks of Flowable, why only this kind of injection with a static modifier worked, and what is the logic of it?

I must inject a spring bean in a Flowable java service task, and I tested some different kind of injection Field, constructor, and setter injection, eventually setter injection with static modifier worked for me like this :

public class GetCurrentUserDlg implements JavaDelegate {

    private static PersonService personService;

    @Autowired
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    @Override
    public void execute(DelegateExecution execution) {
        personService.getCurrentUser();
    }
}
Malte Jacobson
  • 215
  • 1
  • 11
Massoud Azizi
  • 71
  • 2
  • 8

2 Answers2

6

While I can not answer your question, the following works fine for me:

public class SomeDelegate implements JavaDelegate {

    @Autowired
    private SomeBean bean;

    @Override
    public void execute(DelegateExecution execution) {
        System.out.println(this.bean);
    }
}

The class is then used in the process via flowable:class="packages.SomeDelegate"

But, be aware, that you may have problems with autowiring dependencies in the SomeBean bean. This dependencies are not injected when using the flowable:class attribute. In order for this to work you have to make the SomeDelegate a actual bean itself (e.g. via @Service) and use it in your process definition via flowable:delegateExpression="${someDelegate}"

Example:

@Service("someDelegate")
public class SomeDelegate implements JavaDelegate {
...

and

<serviceTask id="doSomething" name="Do Something" flowable:delegateExpression="${someDelegate}"/>
Malte Jacobson
  • 215
  • 1
  • 11
  • If the answer was part of the solution would you mind to accept the answer for further references – Malte Jacobson Aug 15 '20 at 07:53
  • Thanks for your response Actually I changed my approach, I made JavaDelegate class as a Spring bean, also I used ‘activiti:delegateExpression’ tag in BPMN I actually do not understand why setter injection in static field only work in the previous approach – Massoud Azizi Aug 23 '20 at 04:20
  • When using `flowable:class` then Flowable will instantiate the class. This means that you can't use Spring Dependency autowiring in those classes. The reply from Matte is the way to go when you want to use Spring Beans – Filip Sep 30 '20 at 08:54
0

It should work like this:

@Component
public class GetCurrentUserDlg implements JavaDelegate {

    @Autowired
    private PersonService personService;

    @Override
    public void execute(DelegateExecution execution) {
        personService.getCurrentUser();
    }
}

@Component
public class PersonService {
   // its methods
}
MC Emperor
  • 22,334
  • 15
  • 80
  • 130