cancel
Showing results for 
Search instead for 
Did you mean: 

Spring bean cannot be resolved in activiti:delegateExpression

rohitjain
Champ in-the-making
Champ in-the-making
I've this Java based spring configuration file:


@Configuration
public class WorkflowConfig {

   @Bean
   public DataSource activitiDataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
      dataSource.setUrl("xxxx");
      dataSource.setUsername("xxxx");
      dataSource.setPassword("xxxx");
      return dataSource;
   }

   @Bean
   @DependsOn({ "activitiDataSource" })
   public PlatformTransactionManager txManager() {
      return new DataSourceTransactionManager(activitiDataSource());
   }

   @Bean
   @DependsOn({ "txManager" })
   public SpringProcessEngineConfiguration processEngineConfiguration() {
      SpringProcessEngineConfiguration processEngineConfig = new SpringProcessEngineConfiguration();
      processEngineConfig.setDataSource(activitiDataSource());
      processEngineConfig.setTransactionManager(txManager());
      processEngineConfig.setDatabaseSchemaUpdate("true");
      processEngineConfig.setDatabaseType("mysql");
      processEngineConfig.setJobExecutorActivate(true);
      processEngineConfig
            .setDeploymentResources(new ClassPathResource[] {
                           new ClassPathResource("invoiceWorkflow.bpmn")
                        });
      return processEngineConfig;
   }

   @Bean
   @DependsOn({ "processEngineConfiguration" })
   public ProcessEngine processEngine() {
      return processEngineConfiguration().buildProcessEngine();
   }

   @Bean
   public WorkflowService workflowService() {
      return new WorkflowService();
   }
   
   @Bean
   public FetchTicket fetchTicket() {
      return new FetchTicket();
   }
}


<blockcode>FetchTicket</blockcode> is my JavaDelegate class, as shown below:


public class FetchTicket implements JavaDelegate {

   @Autowired
   private TicketService ticketService;

   @Override
   public void execute(DelegateExecution execution) throws Exception {
            System.out.println(ticket);
   }
}


Since I've configured <blockcode>FetchTicket</blockcode> as a spring bean, I used it in expression as below in BPMN config file:


<definitions ….>
<process id="invoiceGenerationProcess" name="Invoice Generation Process"
      isExecutable="true">
      <startEvent id="start" name="Start" />
      <serviceTask id="showTickets" name="Show Tickets"
         activiti:delegateExpression="${fetchTicket}" />
      <sequenceFlow id="flow1" sourceRef="start" targetRef="showTickets" />
      <userTask id="generateInvoice" name="Generate Invoice" />
      <sequenceFlow id="flow2" sourceRef="showTickets"
         targetRef="generateInvoice" />
      <endEvent id="endevent1" name="End" />
      <sequenceFlow id="flow3" sourceRef="generateInvoice"
         targetRef="endevent1" />
   </process>
</definitions>


But the expression: <blockcode>${fetchTicket}</blockcode> is not getting resolved. I see that bean <blockcode>fetchTicket</blockcode> is created clearly in the logs:

<blockcode>
00:26:28.017 [main] DEBUG o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'fetchTicket' to bean named 'ticketService'
00:26:28.017 [main] DEBUG o.s.c.a.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'fetchTicket': public void com.redbus.activiti.servicetask.FetchTicket.afterPropertiesSet()
Ticket Service: com.redbus.service.impl.TicketServiceImpl@576e18c5
00:26:28.018 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'fetchTicket'
</blockcode>

Can you please help me out of this? And also it would be great if you can review, if my Activiti spring config is fine? I converted it to java based from the XML.
4 REPLIES 4

rohitjain
Champ in-the-making
Champ in-the-making
Oh, and I've this simple test class which I'm running, and getting those logs:

<code>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= { WorkflowConfig.class, WorkflowServiceTest.Config.class } )
public class WorkflowServiceTest {

@Configuration
public static class Config {

  @Bean
  public TicketService ticketService() {
   return new TicketServiceImpl();
  }
 
  @Bean
  public TicketDao ticketDao() {
   return new TicketDaoImpl();
  }
}

@Autowired
private WorkflowService workflowService;

@Autowired
private ProcessEngine processEngine;

@Test
public void testGetProcessInstance() {
  processEngine.getRuntimeService().startProcessInstanceByKey(
    "invoiceGenerationProcess", variables);
  ProcessInstance processInstance = workflowService.getProcessInstance();

  assertEquals("invoiceGenerationProcess", processInstance.getId());
}
}
</code>

trademak
Star Contributor
Star Contributor
Could you create a unit test showing the issue and create a JIRA issue? That makes it easier for use to fix it.

Best regards,

rohitjain
Champ in-the-making
Champ in-the-making
Well, I got it working by replacing <blockcode>ProcessEngine</blockcode> bean with <blockcode>ProcessEngineFactoryBean</blockcode> bean. And then I got the process engine using the <blockcode>getObject()</blockcode> method:

<code>
        @Bean
@DependsOn({ "processEngineConfiguration" })
public ProcessEngineFactoryBean processEngineFactoryBean() {
  ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
  factoryBean.setProcessEngineConfiguration(processEngineConfiguration());
 
  return factoryBean;
}

        @Bean
public HistoryService historyService() throws Exception {
  return processEngineFactoryBean().getObject().getHistoryService();
}
</code>

But to my surprise, the autowiring of <blockcode>ProcessEngine</blockcode> still worked. How? I didn't register that bean anymore now. And what could be the difference that this worked, and <blockcode>buildProcessEngine</blockcode> didn't?

Shall I still raise a JIRA issue? The test is anyway there in the first posting.

jbarrez
Star Contributor
Star Contributor
Spring knows enough about FactoryBeans to understand what's going on, thats why the wiring works.

However, your first config looks ok, must be something small, cause it should work that way