cancel
Showing results for 
Search instead for 
Did you mean: 

How to access Spring beans

araghuraman
Champ in-the-making
Champ in-the-making
I am using org.activiti.spring.SpringProcessEngineConfiguration to configure Activiti within a Spring application.
I did a little test:
MySpringBeanClass bean = (MySpringBeanClass) execution.getVariable("mySpringBean")
and this returned null.
How can I access other Spring beans contained in the same application from within a Java Service Task
6 REPLIES 6

araghuraman
Champ in-the-making
Champ in-the-making
I found a workaround consisting of the two following steps, but I am not sure if its recommended:

1) I converted my plain Java Delegate POJO to a Spring Managed bean:

@Component("my.workflow.beans.WorkflowTestJavaTask") //make application context scan this package using <context:component-scan../>
public class WorkflowTestJavaTask implements JavaDelegate
{
   
    @Autowired
    @Qualifier("myOtherSpringBean")
    private MyOtherSpringBean otherSpringBean;

    @Override
    public void execute(DelegateExecution execution) throws Exception
    {
…..


2)
Refer to above bean in workflow process definition so:
..
<serviceTask id="servicetask1" name="Service Task" activiti:expression="#{my.workflow.beans.WorkflowTestJavaTask.execute(execution)}"></serviceTask>..

frederikherema1
Star Contributor
Star Contributor
How can I access other Spring beans contained in the same application from within a Java Service Task

All beans present in the application-context should be visible to the expression-resolver? Are you sure the auto wired beans are added to the same context as the activiti-engine is?

araghuraman
Champ in-the-making
Champ in-the-making
Hi,

Here is my configuration. Please tell me if I am doing anything wrong. Of late I also discovered that the activiti:elementVariable is also not being passed to a multiinstace callactivity task:


  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration" depends-on="bidness">
    <property name="dataSource" ref="activitiDS" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="jobExecutorActivate" value="true" />
  </bean>
 
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean" depends-on="processEngineConfiguration">
    <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" />
 
  <bean id="registerWorkflows" class="my.business.startup.RegisterWorkflows"
   depends-on="repositoryService,runtimeService,taskService,historyService,managementService"/>

sherlockq
Champ in-the-making
Champ in-the-making
I suppose the better approach is to obtain spring beans directly from spring context but not rely on activiti's resolver. Here is the reference: http://stackoverflow.com/questions/129207/getting-spring-application-context

shiurya
Champ in-the-making
Champ in-the-making
Hi,

1. Create an ApplicationContextProvider
public class ApplicationContextProvider implements ApplicationContextAware
{

  private static ApplicationContext applicationContext = null;
 
  public static ApplicationContext getApplicationContext()
    {        
        return applicationContext;   
    }
 
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
       this.applicationContext = applicationContext;
    }

}
2. Register it in activiti-context.xml
<bean id="applicationContextProvider" class="com.workflow.config.ApplicationContextProvider"></bean>

3. Now use in any JavaDelegate using ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
     to get the context.
4. If you want to use any spring bean in java delegate, register that bean too in activiti-context.xml.
say  bean id="agentService" class="com.workflow.runtime.service.AgentService"

5. Now you can use the bean agentService in your java delegate like
agentService = (AgentService) applicationContext.getBean("agentService");

Hope, it helps.
Thanks

jbarrez
Star Contributor
Star Contributor
Or simply use activiti:delegateExpression or activiti:expression instead of class.