Injecting spring bean on context of flowable-task tomcat

I created a custom java service class and put it on my process like the documentation example and worked fine.
I tried to use this class to persist an JPA Entity, but, I couldn’t inject the spring bean on spring context.
turn 1-> deploy my custom jar on flowable-task/web-inf/lib and got “null pointer”
turn2-> modify the class ApplicationConfiguration to add my package on list of packages. error on tomcat deploy
my code below:
@Configuration
@EnableConfigurationProperties(FlowableTaskAppProperties.class)
@ComponentScan(basePackages = {
“org.flowable.ui.task.conf”,
“org.flowable.ui.task.repository”,
“org.flowable.ui.task.service”,
“org.flowable.ui.task.security”,
“org.flowable.ui.task.model.component”,
“org.flowable.ui.common.conf”,
“org.flowable.ui.common.repository”,
“org.flowable.ui.common.service”,
“org.flowable.ui.common.filter”,
“org.flowable.ui.common.security”,
“com.test.mycustompackage”
})
public class ApplicationConfiguration {

}

public class JPATask implements JavaDelegate {

@Autowired
GenericJPAService genericJPAService;

public void execute(DelegateExecution execution) {
    if (execution.hasVariable("nome")) {
        Produto p = new Produto();
        p.setNome(execution.getVariable("nome").toString());
        genericJPAService.create(p);
    }
}
}

@Repository
public class GenericJPAService {

@PersistenceContext
private EntityManager entityManager;

@Transactional
public Object create(Object entity) {
    entityManager.persist(entity);
    return entity;
}
}

<beans .......>
 <context:component-scan base-package="com.test.mycustompackage"/>

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg>
        <bean class="com.zaxxer.hikari.HikariConfig">
            <property name="autoCommit" value="false" />
            <property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource" />
            <property name="dataSourceProperties">
                <props>
                    <prop key="url">jdbc:postgresql://localhost:5433/flowable</prop>
                    <prop key="user">xxxx</prop>
                    <prop key="password">xxxx</prop>
                </props>
            </property>
        </bean>
    </constructor-arg>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceXmlLocation" value="classpath:/persistence.xml"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
            <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.PostgresDictionary"/>
        </bean>
    </property>
</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="jpaEntityManagerFactory" ref="entityManagerFactory"/>
    <property name="jpaHandleTransaction" value="false"/>
    <property name="jpaCloseEntityManager" value="false"/>
</bean>

<bean id="processEngine" class="org.flowable.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
</bean>

<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>

</beans>
1 Like

Hi, did you resolve this?
I’m reading tons of similar posts without a solution.

Convert JPATask as a bean using class annotation @Service or @Component and in your BPMN file instead of Class property use Delegate Expression property and key in the bean name. For e.g. ${jPATask}

This image highlights Delegate Expression syntax for a bean name logHelloTask

1 Like

Finally I had to register my beans into process engine configuration:

var processEngineConfiguration = new SpringProcessEngineConfiguration();
....
Map<Object, Object> beans = Map.ofEntries(
	Map.entry("rabbitTemplate", new RabbitTemplate()),  ....

processEngineConfiguration.setBeans(beans);

In this way other beans are available into services called using flowable expressions