cancel
Showing results for 
Search instead for 
Did you mean: 

A Problem about spring inject bean into activiti

lykm02
Champ in-the-making
Champ in-the-making
Hi, all:
       Here is my case.
       I defined a userTask as following at first:
      

              <userTask id="DataValidator" name="Data Validator">
                 <extensionElements>
                         <activiti:taskListener event="create" class="com.xxx.xxx.TaskAssignmentHandler">
                              <activiti:field name="candidateGroup" stringValue="QA" />
                         </activiti:taskListener>
                </extensionElements>
            </userTask>
      
       Corresponding Java Code like following
      

       @Service
       public class TaskAssignmentHandler implements TaskListener{
       @Autowired
       private IdentityService identityService;
       @Autowired
       private  WorkflowHistoryService historyService;
       private Expression candidateGroup;      

       @Override
       public void notify(DelegateTask task){
         // do something 
       }
       }
      
    
       I found there is always nullpointerexception thrown out, because the class will be load into activiti by activiti on his own way, while other service is managed by spring.
       So, there will keep those service null pointer.
      
       Then I changed xml definition into following.
      

             <userTask id="DataValidator" name="Data Validator">
                 <extensionElements>
                         <activiti:taskListener event="create" class="${taskAssignHandlerBean}">
                              <activiti:field name="candidateGroup" stringValue="QA" />
                         </activiti:taskListener>
                </extensionElements>
            </userTask>
      
       And I add serialiable on to the class definition
      

         @Service
       public class TaskAssignmentHandler implements TaskListener, [color=#0000FF]Serializable[/color]{
      
       And add one instance of TaskAssignmentHandler into instance of activiti. It still not work, because it requires any field of TaskAssignmentHandler implements Serializable.
       Ohhhh, it's unacceptable to modify those dependent class.
      
       So, there is any solution to handle the case?

Thanks
2 REPLIES 2

trademak
Star Contributor
Star Contributor
Hi,

In the Activiti in Action book you get an example of how to implement this.
When you want to use a Spring bean you should use the expression attribute in stead of the class attribute.

Best regards,

lykm02
Champ in-the-making
Champ in-the-making
Hi,

In the Activiti in Action book you get an example of how to implement this.
When you want to use a Spring bean you should use the expression attribute in stead of the class attribute.

Best regards,
Sorry, I have mentioned I didn't want to add  Serializable interface to those field of my self extend class.
It will affact a lot of exists class.
And I don't think that's a good solution.

Yesterday, I read source code of activiti, and I found out a simply way to avoid this case.
Just remove all the use-less annotation and add a constructor for the class. As IOC container of activiti will load it, it works.

Here is my solution.


       public class TaskAssignmentHandler implements TaskListener{
              private IdentityService identityService;
              private  WorkflowHistoryService historyService;
          
              private Expression candidateGroup;      

              public TaskAssignmentHandler(){
                         //Initialize those field here.
                         identityService = AppContext.getApplicationContext().getBean("xxxx");
                         // Same like this
                         WorkflowHistoryService = ………..;
              }
             
              @Override
              public void notify(DelegateTask task){
                       // do something
              }
       }
In the definition xml, I still use the class instead of delegateExpression