# Dependency injection not working with CDI

**URL:** <https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136>\
**Category:** Uncategorized\
**Created:** [December 24, 2019, 10:06am UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136 "2019-12-24T10:06:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik](https://avatars.discourse-cdn.com/v4/letter/e/b5ac83/32.png) [@erik](https://forum.flowable.org/u/erik)\
**Post date:** [December 24, 2019, 10:06am UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136/1 "2019-12-24T10:06:50Z")

</div>

Dependency Injection is not working for my `JavaDelegates` and `TaskListeners`. It works for neither Flowable beans (e.g., `RuntimeService`) nor application EJB beans. In the other parts of the application, dependency injection works for both of them.

I use Flowable 6.4.2 with the `CdiStandaloneProcessEngineConfiguration` on TomEE 7.1.1, and I use the `flowable:class` attribute in the BPMN 2.0 XML file.

I followed the [documentation](https://flowable.com/open-source/docs/bpmn/ch16-Cdi/#setting-up-flowable-cdi) on setting up Flowable with CDI.

My `flowable.cfg.xml` looks as follows:

```auto
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="processEngineConfiguration" class="org.flowable.cdi.CdiStandaloneProcessEngineConfiguration">
    <property name="dataSourceJndiName" value="openejb:Resource/ProcessEngine" />
    <property name="databaseSchemaUpdate" value="true" />

    <property name="asyncExecutorActivate" value="true" />
  </bean>

</beans>

```

What am I missing?

---

<div class="post-metadata">

**Author:** ![wwitt](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/wwitt/32/706_2.png) [@wwitt](https://forum.flowable.org/u/wwitt)\
**Post date:** [December 26, 2019, 4:00pm UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136/2 "2019-12-26T16:00:07Z")

</div>

I’ve never used Flowable with CDI, but in general `flowable:class` uses the `new` operator to instantiate your delegates. Your best bet is to use `flowable:expression` or `flowable:delegateExpression` so the beans can be injected/autowired.

> **[CDI integration · Flowable Open Source Documentation](https://flowable.com/open-source/docs/bpmn/ch16-Cdi/#referencing-beans-from-the-process)**
>
> The flowable-cdi modules leverages both the configurability of Flowable and the extensibility of cdi. The most prominent features of flowable-cdi are:

---

<div class="post-metadata">

**Author:** ![erik](https://avatars.discourse-cdn.com/v4/letter/e/b5ac83/32.png) [@erik](https://forum.flowable.org/u/erik)\
**Post date:** [December 26, 2019, 6:43pm UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136/3 "2019-12-26T18:43:10Z")

</div>

OK, thank you.

That’ s too bad because it keeps me from migrating to Flowable. For Camunda, injection works with `camunda:class` too. My `JavaDelegates` and `TaskListeners` are not stateless, so I cannot easily switch to `flowable:expression` and `flowable:delegateExpression`.

---

<div class="post-metadata">

**Author:** ![joram](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/joram/32/26_2.png) [@joram](https://forum.flowable.org/u/joram)\
**Post date:** [January 6, 2020, 9:25pm UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136/4 "2020-01-06T21:25:33Z")

</div>

Do you have an example xml of where the class injection is working? I’m trying to see if this was old behavior that was working differently in the past.

A delegateExpression should give you the same behavior (as no new instance is created). However, do note that in multi-threaded usages this can lead to unforseen behavior.

---

<div class="post-metadata">

**Author:** ![erik](https://avatars.discourse-cdn.com/v4/letter/e/b5ac83/32.png) [@erik](https://forum.flowable.org/u/erik)\
**Post date:** [January 6, 2020, 10:25pm UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136/5 "2020-01-06T22:25:40Z")

</div>

Sorry, maybe I misled you.

I need separate instances for each invocation. Otherwise, I will get unforeseen behavior when multiple activity instances execute concurrently as my delegate is not stateless.

In Camunda injection works, in Flowable it does not.

In Camunda my XML looks like

```auto
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="definitions-external" name="definitions-external" targetNamespace="de.montigem.wf.external">
    <bpmn:process isExecutable="true" name="External" id="external">
        <bpmn:laneSet>
            <bpmn:lane name="Secretariat" id="lane-secretariat">
                <bpmn:flowNodeRef>foo</bpmn:flowNodeRef>
                <bpmn:flowNodeRef>anonymous_1</bpmn:flowNodeRef>
                <bpmn:flowNodeRef>anonymous_2</bpmn:flowNodeRef>
            </bpmn:lane>
        </bpmn:laneSet>
        <bpmn:serviceTask name="Foo" id="foo" camunda:class="de.montigem.be.wf.external.service.Foo" camunda:asyncBefore="true">
            <bpmn:incoming xmlns="">from-anonymous_1-to-foo</bpmn:incoming>
            <bpmn:outgoing xmlns="">from-foo-to-anonymous_2</bpmn:outgoing>
        </bpmn:serviceTask>
        <bpmn:startEvent parallelMultiple="false" name="Anonymous_1" id="anonymous_1">
            <bpmn:outgoing xmlns="">from-anonymous_1-to-foo</bpmn:outgoing>
        </bpmn:startEvent>
        <bpmn:endEvent name="Anonymous_2" id="anonymous_2">
            <bpmn:incoming xmlns="">from-foo-to-anonymous_2</bpmn:incoming>
        </bpmn:endEvent>
        <bpmn:sequenceFlow sourceRef="anonymous_1" targetRef="foo" isImmediate="true" id="from-anonymous_1-to-foo"/>
        <bpmn:sequenceFlow sourceRef="foo" targetRef="anonymous_2" isImmediate="true" id="from-foo-to-anonymous_2"/>
    </bpmn:process>
</bpmn:definitions>

```

In Flowable:

```auto
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:flowable="http://flowable.org/bpmn" id="definitions-external" name="definitions-external" targetNamespace="de.montigem.wf.external">
    <bpmn:process isExecutable="true" name="External" id="external">
        <bpmn:laneSet>
            <bpmn:lane name="Secretariat" id="lane-secretariat">
                <bpmn:flowNodeRef>foo</bpmn:flowNodeRef>
                <bpmn:flowNodeRef>anonymous_1</bpmn:flowNodeRef>
                <bpmn:flowNodeRef>anonymous_2</bpmn:flowNodeRef>
            </bpmn:lane>
        </bpmn:laneSet>
        <bpmn:serviceTask name="Foo" id="foo" flowable:class="de.montigem.be.wf.external.service.Foo" flowable:asyncBefore="true">
            <bpmn:incoming xmlns="">from-anonymous_1-to-foo</bpmn:incoming>
            <bpmn:outgoing xmlns="">from-foo-to-anonymous_2</bpmn:outgoing>
        </bpmn:serviceTask>
        <bpmn:startEvent parallelMultiple="false" name="Anonymous_1" id="anonymous_1">
            <bpmn:outgoing xmlns="">from-anonymous_1-to-foo</bpmn:outgoing>
        </bpmn:startEvent>
        <bpmn:endEvent name="Anonymous_2" id="anonymous_2">
            <bpmn:incoming xmlns="">from-foo-to-anonymous_2</bpmn:incoming>
        </bpmn:endEvent>
        <bpmn:sequenceFlow sourceRef="anonymous_1" targetRef="foo" isImmediate="true" id="from-anonymous_1-to-foo"/>
        <bpmn:sequenceFlow sourceRef="foo" targetRef="anonymous_2" isImmediate="true" id="from-foo-to-anonymous_2"/>
    </bpmn:process>
</bpmn:definitions>

```

---

<div class="post-metadata">

**Author:** ![joram](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/joram/32/26_2.png) [@joram](https://forum.flowable.org/u/joram)\
**Post date:** [January 17, 2020, 11:06pm UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136/6 "2020-01-17T23:06:09Z")

</div>

Ok, i think we were indeed talking about different things. I thought you mean class injection, but you mean injection of other services/beans into your service task class Foo in this example?

My CDI is quite rusty. Can you give an example how that Foo service task looks like (doesn’t need the logic, just the injection part).

---

<div class="post-metadata">

**Author:** ![erik](https://avatars.discourse-cdn.com/v4/letter/e/b5ac83/32.png) [@erik](https://forum.flowable.org/u/erik)\
**Post date:** [January 17, 2020, 11:32pm UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136/7 "2020-01-17T23:32:25Z")

</div>

Exactly. I would like to inject other service beans into the service task delegate classes.

Like `MyService` below.

```auto
package demo;

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

import demo.MyService;

import javax.inject.Inject;

public class Foo implements JavaDelegate {

    @Inject
    protected MyService service;

    @Override
    public void execute(DelegateExecution delegateExecution) {
      service.doStuff();
    }
}

```

```auto
package demo;

import javax.ejb.Stateless;

@Stateless
public class MyService {

  public void doStuff() {
    // ...
  }

}

```

---

<div class="post-metadata">

**Author:** ![cirux88](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/cirux88/32/2421_2.png) [@cirux88](https://forum.flowable.org/u/cirux88)\
**Post date:** [August 3, 2021, 12:58pm UTC](https://forum.flowable.org/t/dependency-injection-not-working-with-cdi/5136/8 "2021-08-03T12:58:29Z")

</div>

> [@joram](#):
>
> an you give an example how that Foo service task lo

Hi, any solution for this problem?
