Using Spring Beans

cancel
Showing results for 
Search instead for 
Did you mean: 
msatti
Member II

Using Spring Beans

Hi all,

I'm fairly new to Activiti so apologies if this is basic.

I have a class implementing the JavaDelegate interface, but it also has an @Autowired bean inside it (it is a class annotated with @Service). I read that since I am autowiring, you can't specify it as an activiti:class inside the service task, you have to bind it using activiti:delegateExpression.

I have my implemented class as:

@Component("myDelegate")
class MyDelegate implements JavaDelegate {
    @Autowired
    MyCustomService myCustomService;

    ...
}‍‍‍‍‍‍‍

and I have defined the service task as:

<serviceTask id="servicetask1" name="Service Task 1" activiti:delegateExpression="${myDelegate}"></serviceTask>

However when I go to run the process, I get a runtime error saying:

org.activiti.engine.ActivitiException: Unknown property used in expression: ${myDelegate}

Is there something else I need to do?

If possible, I was hoping there was a solution that only used annotations rather than .xml bean definitions

1 Reply
msatti
Member II

Re: Using Spring Beans

The reason it didn't work it because I didn't set up my process engine correctly. The following got it working for me:

@Configuration
public class ProcessEngineConfig {
     
     @Value("classpath*:*.bpmn")
     private Resource resources[];

     @Bean
     public DataSource dataSource() {
          SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
          dataSource.setUsername("sa");
          dataSource.setPassword("");
          dataSource.setUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000");
          dataSource.setDriverClass(org.h2.Driver.class);
          return dataSource;
     }

     @Bean
     public PlatformTransactionManager transactionManager(DataSource dataSource) {
          DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
          transactionManager.setDataSource(dataSource);
          return transactionManager;
     }

     @Bean
     public SpringProcessEngineConfiguration processEngineConfiguration(DataSource dataSource, PlatformTransactionManager transactionManager) {
          SpringProcessEngineConfiguration engineConfiguration = new SpringProcessEngineConfiguration();
          engineConfiguration.setDataSource(dataSource);
          engineConfiguration.setTransactionManager(transactionManager);
          engineConfiguration.setDatabaseSchemaUpdate(SpringProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
          engineConfiguration.setAsyncExecutorActivate(false);
          engineConfiguration.setDeploymentResources(resources);
          return engineConfiguration;
     }

     @Bean
     public ProcessEngineFactoryBean processEngine(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
          ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
          processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration);
          return processEngineFactoryBean;
     }

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

     @Bean
     public RuntimeService runtimeService(ProcessEngine processEngine) {
          return processEngine.getRuntimeService();
     }

     @Bean
     public HistoryService historyService(ProcessEngine processEngine) {
          return processEngine.getHistoryService();
     }

     @Bean
     public ManagementService managementService(ProcessEngine processEngine) {
          return processEngine.getManagementService();
     }

     @Bean
     public TaskService taskService(ProcessEngine processEngine) {
          return processEngine.getTaskService();
     }
}