Spring bean injection problem in Flowable service task

Viewed 2212

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();
    }
}
2 Answers

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}"/>

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
}
Related