cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing Spring Beans from a ServiceTask

frank41
Champ in-the-making
Champ in-the-making
Hi all !
I'd need to access Spring beans from a Java Service Task. I have tried with field injection:

public class SpringServiceTask implements JavaDelegate {
  
    private DataSource ds;
  
    public void setDataSource(DataSource ds) {
         this.ds= ds;
  
    }
    public void execute(DelegateExecution execution) throws Exception {
       System.out.println("DataSource "+ds); // NULL!
    }
}
However the injection does not happens and this the execute method prints null.
Even if I've added the datasource reference in the applicationContext.xml, nothing is injected:
<bean id="reviewApproved" class="com.sample.ReviewApproved">
        <property name="ds" ref="dataSource"/>
</bean>
Any help ?
Thanks a lot
Frank
1 ACCEPTED ANSWER

trademak
Star Contributor
Star Contributor
Hi,

Like Ronald already said in an earlier post. To inject Spring beans in a service task class, the service task class also must be a Spring bean.
So you have to configure a service task with an expression that points to a Spring bean. In the Spring config you can then inject the datasource property into the Spring bean.
You can find an example in the userguide.

Best regards,

View answer in original post

8 REPLIES 8

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
Your service task must be a spring bean as well then…

frank41
Champ in-the-making
Champ in-the-making
Hello,
thanks for your reply Ronald. Actually I have declared the Service Class in my applicationContext.xml
<bean id="springServiceTask" class="com.sample.SpringServiceTask">
        <property name="ds" ref="dataSource"/>
</bean>
(sorry in the original message I've cut and paste another Service task declaration)
The matter, I guess, is that it does not get invoked through the Spring Context (but using a Singleton pattern)…..

nlitterat
Champ in-the-making
Champ in-the-making
How do you try to activate the bean? can you the code that call the bean?
From the look of it the spring definition seems OK
Make sure the datasource is defined correctly

frank41
Champ in-the-making
Champ in-the-making
How do you try to activate the bean? can you the code that call the bean?
From the look of it the spring definition seems OK
Make sure the datasource is defined correctly
Hello,
my ServiceTask are defined in the BPMN20 file as:
<serviceTask id="servicetask1" name="Service Task" activiti:class="com.sample.MyServiceTask"></serviceTask>
This is the Service Task implementation:
public class MyServiceTask implements JavaDelegate {


private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
  this.dataSource = dataSource;
}

public void execute(DelegateExecution execution) throws Exception {

  System.out.println("Datasource is "+dataSource); // NULL!!
  
}

}
For example, here I'm using a Datasource which is injected using setter field injection. However it prints out null. (Same for other beans defined into applicationContext.xml)

The Datasource is defined in applicationContext.xml
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/activiti?autoReconnect=true" />
    <property name="username" value="activiti" />
    <property name="password" value="activiti" />
    <property name="defaultAutoCommit" value="false" />
  </bean>
Last thing: I'm trying to execute this process from within the process-explorer application, so I'm using the applicationContext.xml inside it.
Hope anybody can sort out which is the issue.
Thanks
Frank

trademak
Star Contributor
Star Contributor
Hi,

Like Ronald already said in an earlier post. To inject Spring beans in a service task class, the service task class also must be a Spring bean.
So you have to configure a service task with an expression that points to a Spring bean. In the Spring config you can then inject the datasource property into the Spring bean.
You can find an example in the userguide.

Best regards,

frank41
Champ in-the-making
Champ in-the-making
Hi,

Like Ronald already said in an earlier post. To inject Spring beans in a service task class, the service task class also must be a Spring bean.
So you have to configure a service task with an expression that points to a Spring bean. In the Spring config you can then inject the datasource property into the Spring bean.
You can find an example in the userguide.

Best regards,
Ah, got it, sorry. In other words not hardwiring a Java class into the Service Task, but using an expression that resolves to a Spring Bean. Thanks!
Frank

Can you please tell how you have achieved this.I'm also using the same

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
Ah, got it, sorry. In other words not hardwiring a Java class into the Service Task, but using an expression that resolves to a Spring Bean. Thanks!
Frank

Yes… 🙂