# Autowired not working in JavaDelegate for ServiceTask

**URL:** <https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849>\
**Category:** Uncategorized\
**Created:** [September 29, 2020, 5:22am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849 "2020-09-29T05:22:44Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![aashi](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aashi](https://forum.flowable.org/u/aashi)\
**Post date:** [September 29, 2020, 5:22am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/1 "2020-09-29T05:22:45Z")

</div>

Hello @wwitt,

Here is the bpmn,

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

\<bpmn:definitions  
xmlns:bpmn=“[http://www.omg.org/spec/BPMN/20100524/MODEL](http://www.omg.org/spec/BPMN/20100524/MODEL)”  
xmlns:di=“[http://www.omg.org/spec/BPMN/20100524/DI](http://www.omg.org/spec/BPMN/20100524/DI)”  
xmlns:dc=“[http://www.omg.org/spec/DD/20100524/DC](http://www.omg.org/spec/DD/20100524/DC)”  
xmlns:xsi=“[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)”  
xmlns:di\_1=“[http://www.omg.org/spec/DD/20100524/DI](http://www.omg.org/spec/DD/20100524/DI)”  
xmlns:flowable=“[http://flowable.org/bpmn](http://flowable.org/bpmn)”  
id=“idCASImportProcessDefinition”  
targetNamespace=“[http://bpmn.io/schema/bpmn](http://bpmn.io/schema/bpmn)”  
exporter=“bpmn-js ([https://demo.bpmn.io](https://demo.bpmn.io))”  
exporterVersion=“7.3.0”\>  
\<bpmn:process id=“CAS\_IMPORT” name=“CAS Import Scenario” isExecutable=“true”\>

```
    <bpmn:startEvent id="idStartEvent" name="Start Event" />

    <bpmn:sequenceFlow id="idsequenceFlow1" sourceRef="idStartEvent" targetRef="idParseDescriptorsAndValidationsAndOrderList" />

    <bpmn:serviceTask id="idParseDescriptorsAndValidationsAndOrderList" name="Parse descriptors and Validations" flowable:delegateExpression="${parseDescriptorsAndValidationsAndOrderList}"/>

    <bpmn:sequenceFlow id="Flow_1unfx48" sourceRef="idParseDescriptorsAndValidationsAndOrderList" targetRef="idUploadAndDeploy">
        <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression"><![CDATA[${(isValidDescriptor == "YES")}]]></bpmn:conditionExpression>
    </bpmn:sequenceFlow>

    <bpmn:serviceTask id="idUploadAndDeploy" name="Upload and Deploy" flowable:delegateExpression="${uploadAndDeploy}"/>

    <bpmn:sequenceFlow id="idsequenceFlow3" sourceRef="idUploadAndDeploy" targetRef="idGetGACDStatus" />

    <bpmn:serviceTask id="idGetGACDStatus" name="Get Status" flowable:class="com.sap.cloud.lm.sl.contentlm.cas.utils.flowable.GetGACDStatus" />

    <bpmn:sequenceFlow id="idsequenceFlow4" sourceRef="idGetGACDStatus" targetRef="idGetGACDLogs" />

    <bpmn:serviceTask id="idGetGACDLogs" name="Get Logs" flowable:class="com.sap.cloud.lm.sl.contentlm.cas.utils.flowable.GetGACDLogs" />

    <bpmn:sequenceFlow id="idsequenceFlow5" sourceRef="idGetGACDLogs" targetRef="idGACDDelete" />

    <bpmn:serviceTask id="idGACDDelete" name="Delete Content" flowable:class="com.sap.cloud.lm.sl.contentlm.cas.utils.flowable.GACDDelete" />

    <bpmn:sequenceFlow id="idsequenceFlow6" sourceRef="idGACDDelete" targetRef="idEndEvent" />

    <bpmn:sequenceFlow id="Flow_0gqeaos" sourceRef="idParseDescriptorsAndValidationsAndOrderList" targetRef="idGACDDelete">
        <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression"><![CDATA[${(isValidDescriptor == "NO")}]]></bpmn:conditionExpression>
    </bpmn:sequenceFlow>

    <bpmn:endEvent id="idEndEvent" name="End Event">
    </bpmn:endEvent>
</bpmn:process>

```

\</bpmn:definitions\>

Here is the class implementing JavaDelegate,

@Named(“parseDescriptorsAndValidationsAndOrderList”)  
@Component  
public class ParseDescriptorsAndValidationsAndOrderList implements JavaDelegate {

```
private final Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
MtaArchiveHandler mtaArchiveHandler;

public void execute(DelegateExecution execution) {
    logger.info("first--> " + mtaArchiveHandler.toString());
    logger.info("hello from process instance " + execution.getProcessInstanceId() + " and event name " + execution.getId());
}

```

}

Now I’ve two issues which I’m facing:

1. In case I use specify like flowable:delegateExpression="{parseDescriptorsAndValidationsAndOrderList}", I'm getting below error: unknown property used in expression: {parseDescriptorsAndValidationsAndOrderList}

2. Since delegateExpression is not working, then I’m unable to Autowire dependencies in the classes which are implementing JavaDelegate.

Could you please help me to figure out what could be the possible reason? Or let me know if you need more information from my side.

Regards,  
Ashmita Sinha

---

<div class="post-metadata">

**Author:** ![fiki574](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/fiki574/32/1675_2.png) [@fiki574](https://forum.flowable.org/u/fiki574)\
**Post date:** [September 29, 2020, 7:10am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/2 "2020-09-29T07:10:19Z")

</div>

Do you have ComponentScan defined for the application?  
Have you properly configured the Spring Boot engine you are using?  
Are you using the Flowable as embedded engine in your Spring Boot app or do you want to “deploy” the class to lib folder?

---

<div class="post-metadata">

**Author:** ![aashi](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aashi](https://forum.flowable.org/u/aashi)\
**Post date:** [September 29, 2020, 7:32am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/3 "2020-09-29T07:32:58Z")

</div>

> [@fiki574](#):
>
> Do you have ComponentScan defined for the application?

@ComponentScan(“org.sample.cas”)  
public class Application {  
public static void main(String args) {  
SpringApplication.run(Application.class, args);  
}  
}

> [@fiki574](#):
>
> Have you properly configured the Spring Boot engine you are using?

Could you please be more specific about what could be the missing link?  
But I’ve tried to Autowire ParseDescriptorsAndValidationsAndOrderList class out of flowable context and I’m able to.

> [@fiki574](#):
>
> Are you using the Flowable as embedded engine in your Spring Boot app or do you want to “deploy” the class to lib folder?

Here is the flowable configuration which I’ve done,

@Configuration  
public class FlowableEngineConfiguration {

```
private final Logger logger = LoggerFactory.getLogger(FlowableEngineConfiguration.class);

@Autowired
public DataSource dataSource;

@Autowired
DbServiceImpl casDbService;

@Autowired
StrongUuidGenerator uuidGenerator;

@PostConstruct
private void init() throws Exception {
    casDbService.updateAdminTenant();
}

//	@Lazy
@Bean(name = "processEngineConfiguration")
public ProcessEngineConfigurationImpl processEngineConfiguration() throws Exception {
    init();
    javax.sql.DataSource ds = (javax.sql.DataSource) dataSource;
    SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
    processEngineConfiguration.setDatabaseSchema(TCMConstants.DbQuery.CAS_SCHEMA_NAME);
    processEngineConfiguration.setDataSource(ds);
    processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
    processEngineConfiguration.setTransactionManager(annotationDrivenTransactionManager());
    processEngineConfiguration.setIdGenerator(uuidGenerator); //to generate process instance id as UUID
    processEngineConfiguration.setHistory(HistoryLevel.NONE.getKey()); //by default archiving of process instance and related data would be stopped
    //processEngineConfiguration.setAsyncExecutorNumberOfRetries(0); //for retries
    logger.debug("processEngineConfiguration bean created");
    return processEngineConfiguration;
}

@Bean(name = "ProcessEngine")
public ProcessEngine processEngine() {
    try {
        return processEngineConfiguration().buildProcessEngine();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

@PostConstruct
public void deployDefinedProcess() throws IOException {
    repositoryService()
            .createDeployment()
            .name("casimport")
            .addClasspathResource("processes/casimport.bpmn20.xml")
            .deploy();
}

@Bean(name = "transactionManager")
public PlatformTransactionManager annotationDrivenTransactionManager() {
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
    javax.sql.DataSource ds = (javax.sql.DataSource) dataSource;
    transactionManager.setDataSource(ds);
    logger.debug("transactionManager bean created");
    return transactionManager;
}

@Bean
public RepositoryService repositoryService() {
    return processEngine().getRepositoryService();
}

```

}

Any help would be really appreciable since tried multiple things but as of now, I’m blocked because of this issue.

Regards,  
Ashmita Sinha

---

<div class="post-metadata">

**Author:** ![fiki574](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/fiki574/32/1675_2.png) [@fiki574](https://forum.flowable.org/u/fiki574)\
**Post date:** [September 29, 2020, 7:50am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/4 "2020-09-29T07:50:48Z")

</div>

Okay, the configuration looks fine and you have component scan enabled.

I’m not really sure what’s the problem here then, since you are obviously doing things correctly and correctly referencing the JavaDelegate implementing class.

Best bet would be to wait for either someone from the Flowable team, or someone more experienced with this case.

Give this a try and let us know if it goes well:

```
@Service("parseDescriptorsAndValidationsAndOrderList")
public class ParseDescriptorsAndValidationsAndOrderList implements JavaDelegate {
    private final Logger logger = LoggerFactory.getLogger(ParseDescriptorsAndValidationsAndOrderList.class);
    private MtaArchiveHandler mtaArchiveHandler;
	
    public ParseDescriptorsAndValidationsAndOrderList(MtaArchiveHandler mtaArchiveHandler) {
	    this.mtaArchiveHandler = mtaArchiveHandler;
    }

    @Override
    public void execute(DelegateExecution execution) {
        logger.info("first--> " + mtaArchiveHandler.toString());
        logger.info("hello from process instance " + execution.getProcessInstanceId() + " and event name " + execution.getId());
    }
}
```

---

<div class="post-metadata">

**Author:** ![aashi](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aashi](https://forum.flowable.org/u/aashi)\
**Post date:** [September 29, 2020, 7:54am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/5 "2020-09-29T07:54:42Z")

</div>

Thanks @fiki574,

I can try this, but I don’t think so I can use it as final solution. I’m just wondering why flowable:delegateExpression="${parseDescriptorsAndValidationsAndOrderList}" is giving error. Atleast this should have worked according to the documentation.

Regards,  
Ashmita Sinha

---

<div class="post-metadata">

**Author:** ![fiki574](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/fiki574/32/1675_2.png) [@fiki574](https://forum.flowable.org/u/fiki574)\
**Post date:** [September 29, 2020, 8:30am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/6 "2020-09-29T08:30:04Z")

</div>

Correct, according to the documentation, your implementation would be sufficient so I’m now also wondering why doesn’t it work.

---

<div class="post-metadata">

**Author:** ![aashi](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aashi](https://forum.flowable.org/u/aashi)\
**Post date:** [September 30, 2020, 3:32am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/7 "2020-09-30T03:32:37Z")

</div>

@wwitt @filiphr Any suggestions?

Regards,  
Ashmita Sinha

---

<div class="post-metadata">

**Author:** ![boyan-velinov](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/boyan-velinov/32/1475_2.png) [@boyan-velinov](https://forum.flowable.org/u/boyan-velinov)\
**Post date:** [September 30, 2020, 11:55am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/8 "2020-09-30T11:55:03Z")

</div>

@joram Can you comment

---

<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:** [September 30, 2020, 10:44pm UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/9 "2020-09-30T22:44:22Z")

</div>

The delegateExpression works indeed as you describe. If the delegateExpression doesn’t resolve, it means Flowable can’t access the Spring applicationContext.

Can you share your setup? Is this Spring Boot or regular Spring.?The SpringApplication seems to indicate it’s Spring Boot. But the fact you’re setting up a transaction manager manually seems to point at regular Spring?

More specifically, how are you setting up the ProcessEngineConfiguration? Using Spring it needs to be SpringProcessEngineConfiguration, which does the logic to get access to the applicationContext. Assuming that this is not Spring Boot of course, where this will happen automatically.

> [@boyan-velinov](#):
>
> @joram Can you comment

No need to ping me. We try to look at all forum questions regularly (but as everyone here, we’ve got work priorities, too).

---

<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:** [September 30, 2020, 10:53pm UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/10 "2020-09-30T22:53:02Z")

</div>

Ok, I’ve now seen that the forum had hidden part of the post for me. It is a regular Spring setup, i.e. non-Spring Boot.

When using ‘regular’ Spring, you also need to register the ProcessEngineFactoryBean. This will take the engineConfiguration and create a SpringExpressionManager ([https://github.com/flowable/flowable-engine/blob/master/modules/flowable-spring/src/main/java/org/flowable/spring/SpringExpressionManager.java](https://github.com/flowable/flowable-engine/blob/master/modules/flowable-spring/src/main/java/org/flowable/spring/SpringExpressionManager.java)) that has access to the expression manager. Meaning that your Bean(name = “ProcessEngine”) is incorrect.

The docs for such a setup are here: [https://flowable.com/open-source/docs/bpmn/ch05-Spring/#processenginefactorybean](https://flowable.com/open-source/docs/bpmn/ch05-Spring/#processenginefactorybean) (in old-style Spring xml, but the in java config the setup is the same)

---

<div class="post-metadata">

**Author:** ![aashi](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aashi](https://forum.flowable.org/u/aashi)\
**Post date:** [October 1, 2020, 3:43am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/12 "2020-10-01T03:43:02Z")

</div>

Thank you so much @joram, your comment really helped. I’ve gone through the documentation again, specific to spring boot this time. And in Spring Boot, only beans need to be created and flowable will do the configurations on its own.

As of now, I’m only using Datasource bean (which I’ve created) and added dependency “flowable-spring-boot-starter” and it’s working.

Regards,  
Ashmita Sinha

---

<div class="post-metadata">

**Author:** ![ramkumar](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@ramkumar](https://forum.flowable.org/u/ramkumar)\
**Post date:** [February 7, 2023, 10:18am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/13 "2023-02-07T10:18:59Z")

</div>

Hi everyone,  
I am unable to do any spring boot JPA fuctionality in javadelegate implemented class.getting null reference at runtime for both CustomRepositoryService and flowable RuntimeService.  
Please help.

i am using flowable 6.4.1 and my pom file looks below.

@Component  
public class A implements JavaDelegate {

@Autowired  
private RuntimeService runtimeService;

```
@Autowired
private CustomRepositoryService repositoryService;

```

}

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

4.0.0  
poc  
spring-boot-flowable  
war  
poc

```
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.7</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

```

```
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
    </dependency>
    <dependency>
        <groupId>com.oracle.ojdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>19.3.0.0</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-spring-boot-starter-rest</artifactId>
        <version>${flowable.version}</version>
    </dependency>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-spring-boot-starter-actuator</artifactId>
        <version>${flowable.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<properties>
    <flowable.version>6.4.1</flowable.version>
</properties>

```

---

<div class="post-metadata">

**Author:** ![ramkumar](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@ramkumar](https://forum.flowable.org/u/ramkumar)\
**Post date:** [February 8, 2023, 5:21am UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/14 "2023-02-08T05:21:14Z")

</div>

Hi @joram.Can you comment.

---

<div class="post-metadata">

**Author:** ![filiphr](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/filiphr/32/315_2.png) [@filiphr](https://forum.flowable.org/u/filiphr)\
**Post date:** [February 24, 2023, 1:10pm UTC](https://forum.flowable.org/t/autowired-not-working-in-javadelegate-for-servicetask/6849/15 "2023-02-24T13:10:22Z")

</div>

Hey @ramkumar,

Please do not post on old issues. I would suggest creating a dedicated post.

Cheers,  
Filip
