cancel
Showing results for 
Search instead for 
Did you mean: 

Injecting a spring bean to a task listener

romschn
Star Collaborator
Star Collaborator
Hi,

I have a mailBean which is configured as below in a spring context file.
<bean id="mailBean"   class="com.utils.MailBean" >
      <property name="mailService">
          <ref bean="mailService"/>
       </property>
   </bean>

In my activiti workflow process definition, I am using a task listener class - TestListener extends ScriptTaskListener.

Now, I want to inject the email bean to this listener class. I am not finding any way of doing this.

Could someone please help me on this at the earliest.

Thanks,
2 REPLIES 2

billerby
Champ on-the-rise
Champ on-the-rise
Is it something similar to this you are trying to achieve?

https://forums.alfresco.com/en/viewtopic.php?f=34&t=42982

bhavikp
Champ on-the-rise
Champ on-the-rise
Hi Ramesh,

The only way I've found to solve this problem is to manually register your bean in activiti-context.xml by adding new entry to activitiBeanRegistry map as follows:

<util:map id="activitiBeanRegistry" map-class="java.util.HashMap">

        <entry key="services" value-ref="ServiceRegistry" />

    <entry key="SetWorkflowUsers" value-ref="SetWorkflowUsers" />

    // In my case SetWorkflowUsers extends ScriptExecutionListener. Also SetWorkflowUsers is my bean id.



    </util:map>

and define your bean as follows:


<bean id="SetWorkflowUsers" class="com.demo.workflow.SetWorkflowUsers">
      <property name="serviceRegistry">
         <ref bean="ServiceRegistry" />
      </property>
      <property name="nodeService">
            <ref bean="NodeService" />
        </property>
        <property name="searchService">
            <ref bean="SearchService"/>
        </property>
   </bean>
and put below entry/method in SetWorkflowUsers.java

/**
    *
    * get SetworkflowUsers 's object from Context
    *
    * */
    protected SetWorkflowUsers getSetWorkflowUsers()
       {
           ProcessEngineConfigurationImpl config = Context.getProcessEngineConfiguration();
           if (config != null)
           {
               // Fetch the registry that is injected in the activiti spring-configuration
              SetWorkflowUsers setWorkflowUsers = (SetWorkflowUsers) config.getBeans().get("SetWorkflowUsers");
               if (setWorkflowUsers == null)
               {
                   throw new RuntimeException(
                               "setWorkflowUsers not present in ProcessEngineConfiguration beans, expected setWorkflowUsers with key SetWorkflowUsers");
               }
               return setWorkflowUsers;
           }
           throw new IllegalStateException("No ProcessEngineCOnfiguration found in active context");
       }

So by this way, I can get the object of NodeService, SearchService and ServiceRegistry from my bean SetWorkflowUsers.
Let me know if you need more help.

Regards,
Bhavik Shah
CIGNEX Datamatics