Autowired object(userService) is null within CustomTaskListener code

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
paiyyavj13
Established Member II

Autowired object(userService) is null within CustomTaskListener code

Jump to solution

Hi,

I am writing a custom TaskListener with the below code: I have hooked this TaskListener to Complete event of a User Task. I noticed that all the Autowired objects are null.

I am not efficient in Spring, am I missing a step by which I can properly Autowire these objects? Please assist!

Regards.

Sample Code:

package com.activiti.extension.bean;

@Component("customTaskListener")
public class CustomTaskListener implements TaskListener {

private static final long serialVersionUID = 4218009794670685270L;

private static final Logger logger = LoggerFactory.getLogger(CustomTaskListener.class);

@Autowired
private HistoryService historyService;
@Autowired
private UserService userService;

@Override
public void notify(DelegateTask delegateTask) {
//...removed custom code for this post
//Here userService is null
User subTaskUser = userService.getUser(subTaskAssigneeId);

}
}

1 Solution

Accepted Solutions
cjose
Senior Member II

Re: Autowired object(userService) is null within CustomTaskListener code

Jump to solution

I'm confused. Can you share a simple bpmn xml showing your Task Listener configuration? Otherwise, please find attached a simple process showing the use of task listener.

Ciju

5 Replies
cjose
Senior Member II

Re: Autowired object(userService) is null within CustomTaskListener code

Jump to solution

Which version are you using?

paiyyavj13
Established Member II

Re: Autowired object(userService) is null within CustomTaskListener code

Jump to solution

Hi Ciju Joseph,

The versions are as below:

<activiti.version>1.5.3</activiti.version>
<activiti.engine.version>5.21.0.3</activiti.engine.version>
<spring.version>4.1.6.RELEASE</spring.version>

Deployment is on Alfresco Activiti Enterprise 1.5.3, tried the same with Activiti Process Services 1.6 too with same problem.

I was reading in the admin guide and community regarding bean injections into service task through expressions and JavaDelegate. But in my case it is a TaskListener... I am not quite sure how to go about it.

I read a community post: Injecting a Service in a TaskListener returns Null 

where Dirla Vasile‌ mentions: "in order to have properly injected the services you should use expression or delegateExpression.
if you really need to use "class" for setting the listener, then you have to use Expression for the userService and evaluate it at runtime in the notify method."

Any pointers from your end would help!!!!

Appreciate your assistance..... Thank you....

Regards.

cjose
Senior Member II

Re: Autowired object(userService) is null within CustomTaskListener code

Jump to solution

I'm confused. Can you share a simple bpmn xml showing your Task Listener configuration? Otherwise, please find attached a simple process showing the use of task listener.

Ciju

paiyyavj13
Established Member II

Re: Autowired object(userService) is null within CustomTaskListener code

Jump to solution

Hi Ciju,

Your example bpmn xml helped me fix the problem.

I was calling the TaskListener by mentioning the class name and not through delegateExpression. Once I removed the class name and used delegateExpression alone then the Autowired Objects were initialized properly.

Sorry I couldn't explain it better.

Yet again... Thank you...

Regards.

cjose
Senior Member II

Re: Autowired object(userService) is null within CustomTaskListener code

Jump to solution

Bean injection won't work with "class". When you use Java Delegates, Task Listeners and Event Listeners it is important to understand the usage of "class", "delegate expression" and "expression". Check this out for more detailshttps://community.alfresco.com/community/bpm/blog/2016/11/15/activiti-enterprise-developer-series-se... 

Ciju