cancel
Showing results for 
Search instead for 
Did you mean: 

Activiti Dependency Injection Support

surya46584
Champ in-the-making
Champ in-the-making
Hi :

I am a newbie to Activiti and I am trying to evaluate Activiti for our project needs. Our project architecture is based on typical dependency injection based architecture. We are using Google Guice and my question is does Activiti supports the core engine configuration done via any other DI than Spring?  I find lot of information on configuration of Activiti via Spring but unable to find any help on doing the same using any other DI framework. How can we customize so that activit:expressions can be resolved to use the beans/services injected/binded using Guice DI framework? any pointers would help.

regards,
surya
7 REPLIES 7

frederikherema1
Star Contributor
Star Contributor
You can "configure" the engine from within java-code as well, spring is not needed. You could use any DI-framework you want to build a process-engine configuration and build an engine from there. Start off using a "org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration" or JTA-variant.

If you want to hook in your DI into the expressions activiti resolves, you should override the org.activiti.engine.impl.el.ExpressionManager used for this. This is a setter-method on the process-engine configuration. A good example of this is the org.activiti.spring.SpringExpressionManager, which adds an additional ELResolver for resolving spring-beans. In your case, you'll need to use your DI th get the right instance.

surya46584
Champ in-the-making
Champ in-the-making
Thanks for the pointer. Will check it out.

regards,
surya

stefanw
Champ in-the-making
Champ in-the-making
Hi there,
i had the same problem, i would inject some buiseness-services managed by guice into the JavaDelegates. I cant get the ExpressionManager worked, the Methods in the custom ELResolver were not invoked Smiley Sad

A working Solution seems to be adding a DelegateInterceptor, requesting an injection of the JavaDelegages. Can u tell me if this is a way to do this, or is there a better solution? (caching the injected delegates to not injecting it twice will be added)

<code>
config.setDelegateInterceptor(new DelegateInterceptor() {
  @Override
  public void handleInvocation(DelegateInvocation invocation) throws Exception {
    Object target = invocation.getTarget();
    if (target instanceof JavaDelegate)
      injector.injectMembers(target);
   invocation.proceed();
  }
});

public class TestService implements JavaDelegate {
  @Inject
  public ProcessEngine processEngine;
  @Inject
  public MyBusinessService service;
  @Override
  public void execute(DelegateExecution execution) throws Exception {
    System.out.println("# Testtask executed " + processEngine);
    service.doSomething();
    }
}
</code>

(i use the newest version of activity: 5.12.1)

tank you.

thalestpires
Champ in-the-making
Champ in-the-making
Hi guys!

Is it possible to set a custom DelegateInterceptor for JavaDelegate calls?
How can I do that?

Regards,
Thales Pires

frederikherema1
Star Contributor
Star Contributor
The ProcessEngineConfigurationImpl has a method, called "setDelegateInterceptor". If you're using the activiti.cfg.xml OR spring, just set the "delegateInterceptor" to reference your custom interceptor. In case you're creating the engine in code, call that method (you might have to cast).

dmifs
Champ in-the-making
Champ in-the-making
Hi I saw the comment #4 above about injecting with Guice for Java Delegates.
Seems interesting but is it the right/recommended way?

thanks and regards

jbarrez
Star Contributor
Star Contributor
It is possible, yes, the same mechanism as for spring would work for Guice.