cancel
Showing results for 
Search instead for 
Did you mean: 

Inject/Load bean spring from activiti context

kse
Champ in-the-making
Champ in-the-making
Hi all,

Is there any way to inject and load spring beans from activiti context ?
Here is my case:  I have my own custom class implementing Tasklistener containing a property which is (should be) injected using setter by spring.


 
public final class AddDateMetierUserTaskListener implements TaskListener {

   
    public static final String DATE_METIER_VAR = "dateMetier";

    private CalculDateMetierService calculDateMetierService;

    @Override
    public void notify(DelegateTask delegateTask) {
        Assert.argumentNotNull("delegateTask", delegateTask);
        UserTask userTask = new DelegateTaskAdapter(delegateTask, delegateTask.getVariables());

        LocalDate dateMetier = calculDateMetierService.dispatchCalculDateMetier(userTask);
        if (dateMetier != null) {
            delegateTask.createVariableLocal(DATE_METIER_VAR,
                    dateMetier.toString(DateTimeFormat.forPattern("dd/MM/yyyy")));
        }
    }

    public void setCalculDateMetierService(CalculDateMetierService calculDateMetierService) {
        this.calculDateMetierService = calculDateMetierService;
    }

}

But what I noticed is that property calculDateMetierService is always null. What I understood from that is we have 2 separate contexts: one Spring and other Activiti. And i d'ont know how to communicate between these 2 contexts.
In Spring context, when tomcat is starting these injections are done properly. But when the notify method is launched from the listener the injected service is not injected and is null.

Many developpers had this problem but no one had a good solution to solve this issue. Can anyone give me some tips ?
Any ideas ?

Thanks a lot for your help


Karnan
4 REPLIES 4

kse
Champ in-the-making
Champ in-the-making
We use Activiti version 5.8

kse
Champ in-the-making
Champ in-the-making
I also tried to use this expression to provide the service bean as parameter of the tasklistener:


<activiti:taskListener event="assignment" class="x.x.x.y.AddDateMetierUserTaskListener">
          <activiti:field name="calculDateMetierService">
            <activiti:expression>${calculDateMetierService}</activiti:expression>
          </activiti:field>
        </activiti:taskListener>

But I had an error during the server starting: The tasklistener expect a field type of Expression but I would like to have a injected bean type of CalculDateMetierService

kse
Champ in-the-making
Champ in-the-making
We finally found the solution.
In the previous version, the tasklistener was used on event assignment and type Java class.
Now we changed the type to Delegate expression and it works. The injection is done.

jbarrez
Star Contributor
Star Contributor
thanks for posting back. But do you think an exception should be thrown here … somehow?