How can I access a spring bean in Activiti JavaDelegate?

后端 未结 3 575

I\'m trying to get a simple Spring example to work with Activiti 5.5, and having some trouble. I\'m using the process engine configured with activiti under %activiti_home%/a

相关标签:
3条回答
  • 2020-12-18 08:57

    One of my project uses Activiti with spring. I think that JavaDelagate can be the problem. You can call from activiti's service task every spring bean this way:

    bean definition:

    <bean id="exampleBean" class="org.bpmn.examples.ExampleBean"/>
    

    activiti xml:

    <serviceTask id="servicetask" name="Example" activiti:expression="${exampleBean.doSomething()}"></serviceTask>
    

    You can also pass parameters to the functions for example process variables:

    <serviceTask id="servicetask" name="Example" activiti:expression="${exampleBean.doSomething(processVariable)}"></serviceTask>
    

    I always use service tasks this way, and haven't got problem with singleton beans. Hope it helps. Please take a comment, if I didn't understand your problem.

    UPDATE:

    My project uses activiti like an embedded workflow engine. Activiti uses the same applicationContext with my webapp.

    My process engine configuration:

    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
            <property name="databaseType" value="mssql" />
            <property name="dataSource" ref="dataSource" />
            <property name="transactionManager" ref="transactionManager" />
            <property name="databaseSchemaUpdate" value="true" />
            <property name="jobExecutorActivate" value="true" />
            <property name="deploymentResources" value="classpath*:/diagrams/*.bpmn20.xml" />           
        </bean> 
    
    
        <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
            <property name="processEngineConfiguration" ref="processEngineConfiguration" />
        </bean>
    
    0 讨论(0)
  • My guess ist that activiti will indeed always create a new instance because activiti is not aware of the fact that it should retrieve an instance from the spring container.

    If you haven't checked this resource yet:

    http://www.activiti.org/userguide/index.html#springintegration

    maybe it's what you need (i.e. ProcessEngineFactoryBean)

    0 讨论(0)
  • 2020-12-18 09:02

    I'm using @Autowired

    to bring in my dependencies. Since the JavaDelegate is not instantiated by Spring, I call

    applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
    

    in the constructor of my Delegate's Superclass, which injects all dependencies into the Delegate. You might wonder where to get the applicationContext from, http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html provides you with the answer.

    0 讨论(0)
提交回复
热议问题