# Spring beans not injected into Flowable context

**URL:** <https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122>\
**Category:** Flowable Engine\
**Created:** [June 11, 2018, 2:47am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122 "2018-06-11T02:47:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jan](https://forum.flowable.org/u/jan)\
**Post date:** [June 11, 2018, 2:47am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/1 "2018-06-11T02:47:18Z")

</div>

Hello, I’m trying to add Flowable into my project and I’m facing an issue when my Spring beans are not automatically added into Flowable context, therefore I cannot call them from by processes. From documentation and tests I understood this should work out of the box. Could you please advise if I’m doing something wrong / missing some configuration?  
I’m using Flowable 6.3.1 and Spring Boot 2.0.2.

Flowable dependencies Pom:

```auto
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter-rest</artifactId>
            <version>${flowable.version}</version>
        </dependency>
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-http</artifactId>
            <version>${flowable.version}</version>
        </dependency>

```

My custom configuration class:

```auto
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
	@Override
	public void configure(SpringProcessEngineConfiguration config) {
             // custom form types here...
        }
}

```

Example of interface and bean, that does not get picked up by Flowable:

```auto
public interface PropertyService {
	String getValue(String property);
}

@Slf4j
@Component("propertyService")
public class PropertyServiceImpl implements PropertyService {

	private final Environment environment;

	@Autowired
	public PropertyServiceImpl(Environment environment) {
		this.environment = environment;
	}

	@Override
	public String getValue(String property) {
		String value = environment.getProperty(property);
		return value == null ? null : value.trim();
	}
}

```

This way I’d expect `propertyService` to be available in process expressions and script tasks (I’m using Groovy), however I’m always getting exception on missing property or bean.

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![tijs](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@tijs](https://forum.flowable.org/u/tijs)\
**Post date:** [June 13, 2018, 7:28am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/2 "2018-06-13T07:28:48Z")

</div>

Hi,

Make sure that the package where you implemented the PropertyService is component scanned by Spring. Or you can create a @Bean method in the FlowableConfig class you created for example.

Best regards,

Tijs

---

<div class="post-metadata">

**Author:** ![jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jan](https://forum.flowable.org/u/jan)\
**Post date:** [June 13, 2018, 10:56pm UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/3 "2018-06-13T22:56:13Z")

</div>

Hi Tijs,

Thanks for your reply. The package is component scanned, I’ve also tried creating the service using @Bean factory as you suggested and the property service is still not accessible in the process…  
The bean is 100% created as I can autowire it anywhere else and is also returned by Actuator in bean listing.

Is there any other catch or configuration I’m missing?

Thanks a lot! Kind Regards,  
Jan

---

<div class="post-metadata">

**Author:** ![adymlincoln](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@adymlincoln](https://forum.flowable.org/u/adymlincoln)\
**Post date:** [October 26, 2018, 6:11pm UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/4 "2018-10-26T18:11:54Z")

</div>

Hi, Did you ever get an answer to this question? I’m trying to do the same thing, and my beans are always null inside the JavaDelegate. tia, adym

---

<div class="post-metadata">

**Author:** ![jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jan](https://forum.flowable.org/u/jan)\
**Post date:** [October 29, 2018, 10:45am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/5 "2018-10-29T10:45:23Z")

</div>

Hi, didn’t make it work out of the box. My hacky solution was a bean post processor scanning all beans from certain packages and manually adding these to engine context.

---

<div class="post-metadata">

**Author:** ![adymlincoln](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@adymlincoln](https://forum.flowable.org/u/adymlincoln)\
**Post date:** [October 29, 2018, 11:34am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/6 "2018-10-29T11:34:53Z")

</div>

Hi, I did get this to work, but I HAD to declare the beans within the Process Engine Configuration XML Element :

```
<bean 
    id="beanApproveTransaction" 
    class="com.lmig.ci.billing.tasks.ApproveTransaction">
    <property name="emailService" ref="emailService"/>
</bean>

<bean 
    id="beanEmailTask" 
    class="com.lmig.ci.billing.tasks.EmailTask">
    <property name="emailService" ref="emailService"/>
</bean>

<bean id="processEngineConfiguration"
    class="org.flowable.spring.SpringProcessEngineConfiguration">

    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="asyncExecutorActivate" value="false" />

    <!-- 
    <property name="deploymentResources" value="classpath*:/xml/*.bpmn20.xml" />
     -->

    <property name="mailServerHost" value="smtpin.lmig.com" />
    <property name="mailServerPort" value="25" />

    <!--
    BEANS : Spring Beans integrated with the Flowable Engine 
            <entry key="beanEmailService" value-ref="emailService" />
    -->
    <property name="beans">
        <map>
            <entry key="beanApproveTransaction" value-ref="beanApproveTransaction" />
            <entry key="beanEmailTask" value-ref="beanEmailTask" />
        </map>
    </property>
</bean>

```

hth,

adym

---

<div class="post-metadata">

**Author:** ![wberges](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/wberges/32/940_2.png) [@wberges](https://forum.flowable.org/u/wberges)\
**Post date:** [April 17, 2019, 7:35am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/7 "2019-04-17T07:35:30Z")

</div>

Hello,

The same for me.  
Impossible to retrieve Flowable beans (ProcessEngine, TaskService…) in my JavaDelegate class (declared as @Service) called by a Service Task.  
For example, @Autowired TaskService defined in my Spring configuration file (declared as @Configuration) returns a TaskService object when it returns null in the JavaDelegate.  
And no way to find an example on the Flowable forum or anywhere else on the net. ☹  
_Remark: I use flowable embedded in a spring boot application._

Thanks for your help  
Best Regards  
William

---

<div class="post-metadata">

**Author:** ![ericmandm](https://avatars.discourse-cdn.com/v4/letter/e/4bbf92/32.png) [@ericmandm](https://forum.flowable.org/u/ericmandm)\
**Post date:** [May 21, 2019, 8:14pm UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/8 "2019-05-21T20:14:01Z")

</div>

Hello, I have the same issue. Is there an annotations based work-around for this?  
I’d really like to inject the resource that the delegate needs rather than some other hack.  
Thanks,  
Eric

---

<div class="post-metadata">

**Author:** ![ericmandm](https://avatars.discourse-cdn.com/v4/letter/e/4bbf92/32.png) [@ericmandm](https://forum.flowable.org/u/ericmandm)\
**Post date:** [May 24, 2019, 1:26pm UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/9 "2019-05-24T13:26:44Z")

</div>

Here is the work-around I found in a related post somewhere…

Define the beans in the application class

```
@SpringBootApplication
public class DeckServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeckServiceApplication.class, args);
    }
    @Bean
    public MotionServerClient getMotionServerClient(@Value("${devices.motion.host}") String host, @Value("${devices.motion.port}") int port) {
        MotionServerClient motionServerClient = new MotionServerClient(host, port);
        return motionServerClient;
    }
    @Bean
    public BarcodeServerClient getBarcodeServerClient(@Value("${devices.barcode.host}") String host, @Value("${devices.barcode.port}") int port) {
        BarcodeServerClient barcodeServerClient = new BarcodeServerClient(host, port);
        return barcodeServerClient;
    }
}

```

Create an initializer component that saves the beans to a static utility using a @PostConstruct method

```
@Component
public final class DeviceClientFactoryInitializer {
    @Autowired
    private MotionServerClient motionServerClient;
    @Autowired
    private BarcodeServerClient barcodeServerClient;
    @PostConstruct
    public void init() {
        DeviceClientUtil.setMotionServerClient(motionServerClient);
        DeviceClientUtil.setBarcodeServerClient(barcodeServerClient);
    }
}

```

Access the beans in the Flowable referenced listeners or executions using the static utility methods

```
public class DeviceClientUtil {
    private static MotionServerClient motionServerClient;
    private static BarcodeServerClient barcodeServerClient;
    
    private DeviceClientUtil() {}

    public static MotionServerClient getMotionServerClient() {
        return motionServerClient;
    }
    public static BarcodeServerClient getBarcodeServerClient() {
        return barcodeServerClient;
    }
    public static void setMotionServerClient(MotionServerClient client) {
        DeviceClientUtil.motionServerClient = client;
    }
    public static void setBarcodeServerClient(BarcodeServerClient client) {
        DeviceClientUtil.barcodeServerClient = client;
    }
}
```

---

<div class="post-metadata">

**Author:** ![buksvdl](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@buksvdl](https://forum.flowable.org/u/buksvdl)\
**Post date:** [June 26, 2019, 11:22am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/10 "2019-06-26T11:22:41Z")

</div>

```
private ManagementService getManagementService() {
    return (ManagementService) AppContext.getApplicationContext().getBean(ManagementService.class);
}
```

---

<div class="post-metadata">

**Author:** ![christine](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/christine/32/1132_2.png) [@christine](https://forum.flowable.org/u/christine)\
**Post date:** [July 10, 2019, 9:20am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/11 "2019-07-10T09:20:52Z")

</div>

If the bean is a singleton, you can make the field static and inject it in the usual way.

---

<div class="post-metadata">

**Author:** ![jwashington](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@jwashington](https://forum.flowable.org/u/jwashington)\
**Post date:** [July 19, 2019, 5:40pm UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/12 "2019-07-19T17:40:09Z")

</div>

I know this thread is a little old, but for the benefit of those who sailed into here via Google and have similar issue, here the solution that worked for me. If you want your service delegate to be Spring managed and injected into the workflow, you can do the following steps:

1. Create service delegate bean in Spring:

@Component  
public class JavaSpringManagedDelegate implements JavaDelegate {

@Override  
public void execute(DelegateExecution execution) {

```
  //delegate code

```

}

}

Through the magic of component scanning and naming convention, Spring will create a bean named “javaSpringManagedDelegate”.

1. This bean instance can be used in Java service activity by setting the delegateExpression attribute to “${javaSpringManagedDelegate”, i.e. use the field named **Delegate Expression:** in the modeler.

When flowable executes this activity, it will pull this bean from Spring’s application context and call the execute method.

---

<div class="post-metadata">

**Author:** ![akki](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@akki](https://forum.flowable.org/u/akki)\
**Post date:** [September 18, 2019, 6:52am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/14 "2019-09-18T06:52:29Z")

</div>

@jwashington can you please elaborate last step a little on how to use the `javaSpringManagedDelegate` bean to solve the current issue

---

<div class="post-metadata">

**Author:** ![jonathanlevis](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/jonathanlevis/32/1348_2.png) [@jonathanlevis](https://forum.flowable.org/u/jonathanlevis)\
**Post date:** [September 18, 2019, 7:23pm UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/15 "2019-09-18T19:23:20Z")

</div>

This also worked for me.

Once I loaded the bean that was implementing JavaDelegate into my application context, I was able to access it from flowable and even load other autowired beans in the same class (which were previously since the java delegate was not spring aware once it came out of the process engine).

---

<div class="post-metadata">

**Author:** ![jwashington](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@jwashington](https://forum.flowable.org/u/jwashington)\
**Post date:** [September 19, 2019, 12:53am UTC](https://forum.flowable.org/t/spring-beans-not-injected-into-flowable-context/2122/16 "2019-09-19T00:53:00Z")

</div>

The issue raised in this thread was how to Spring inject flowable objects (RuntimeService, etc.) into a JavaDelegate that is called by the workflow. The solution I proposed creates a JavaDelegate as a Spring component which can be referenced from the modeler using the delegateExpression attribute as the bean name (in the example the name of the bean is javaSpringManagedDelegate via the Spring naming conventions). Since the delegate is managed by Spring, it is easy to inject other Flowable Spring objects such as the runtimeService, repositoryService, etc through the usual Spring mechanisms such as autowiring or constructor injection.
