Can't Autowire Other Class When Using TaskListener

Hello,

I have implemented TaskListener and it is working fine but when i am trying to bind/autowire any other class it is not working.
Please check my code below, i amusing spring boot & camunda task-listner

@Component
public class TaskCreateListener implements TaskListener {

@Autowired
private EmailSender emailSender;
// this is "Null" , Why ??

@Override
public void notify(DelegateTask delegateTask) {

//This is working, but emailSender is null

}
}



@Component
public class EmailSender {
      @Value("${spring.mail.manager}")
      private String manager;
}

Can someone help me on this ?

I got some work around to do it

 @Component
 public class TaskCreateListener implements TaskListener {
    public static EmailSender getEmailSenderObject() {
    return SpringApplicationContextHolder.getApplicationContext().getBean(EmailSender.class);
  }
@Override
  public void notify(DelegateTask delegateTask) {
  //EmailSender Object, It is not null now
   getEmailSenderObject(); 
  }
}

@Component
public class SpringApplicationContextHolder implements ApplicationContextAware {

private static ApplicationContext applicationContext = null;

public static ApplicationContext getApplicationContext() {
    return applicationContext;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
   this.applicationContext = applicationContext;
   }
  }
1 Like

Hi,

HERE you will find a better solution.

In your bpmn you should not use JavaClass Listener, you should use DelegateExpression.As the event type you choose complete. Under DelegateExpression you can type in the name of your listener like this: “${ExamplerListener}”.
If you do it this way, Spring will Autowire the needed class :slight_smile:

Is there a more elegant Solution