AnsweredAssumed Answered

Using spring @Autowire in ExecutionListener not working

Question asked by davidwaf on Jun 28, 2011
Latest reply on Mar 27, 2015 by divakar
Hi all,
I have a web app, that among other things, is using Activiti  for BPM. What i did is dropped these into the classpath:
activiti-engine-5.6.jar
activiti-spring-5.6.jar

Then, my spring-activi config is as follows
[size=85]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

   
   <!– Use annotations to configure our components –>
    <context:component-scan base-package="org.activiti" />

   <!– Turn on annotations for beans –>
    <context:annotation-config />

   <!– Configure datasource –>
    <bean id="dataSource" lazy-init="false"
      class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
        <property name="targetDataSource">
            <bean class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
                <property name="driverClass" value="com.mysql.jdbc.Driver" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </bean>
        </property>
    </bean>

   <!– Configure transaction manager –>
    <bean id="transactionManager" lazy-init="false"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

   <!– Use transaction manager annotations –>
    <tx:annotation-driven transaction-manager="transactionManager" />

   <!– Configure process engine –>
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"
      lazy-init="true">
        <property name="databaseType" value="mysql" />
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="jobExecutorActivate" value="false" />
    </bean>

   <!– Use process engine –>
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"
      lazy-init="true">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>

   <!– Use process engine service –>
    <bean id="repositoryService" factory-bean="processEngine"
      factory-method="getRepositoryService" lazy-init="true" />
    <bean id="runtimeService" factory-bean="processEngine"
      factory-method="getRuntimeService" lazy-init="true" />
    <bean id="taskService" factory-bean="processEngine"
      factory-method="getTaskService" lazy-init="true" />
    <bean id="historyService" factory-bean="processEngine"
      factory-method="getHistoryService" lazy-init="true" />
    <bean id="managementService" factory-bean="processEngine"
      factory-method="getManagementService" lazy-init="true" />
    <bean id="identityService" factory-bean="processEngine"
      factory-method="getIdentityService" lazy-init="true" />
    <bean id="formService" factory-bean="processEngine"
      factory-method="getFormService" lazy-init="true" />
               
    <bean id="propertyService" class="org.licosystems.web.lico.config.properties.PropertyServiceImpl"/>
    <bean id="mailService" class="org.licosystems.web.lico.util.MailService"/>
  

</beans>
[/size]

Now the above configuration allows me to inject the beans perfectly in Controllers anywhere in the app…and i use them, no problem. Now the last two beans: propertyService and mailService are meant to be used in ExecutionListener below:

[size=85]
public class ProcessStartListener implements ExecutionListener {

    private PropertyService propertyService;
    private MailService mailService;
    private FormService formService;

    @Autowired
    public void setFormService(FormService formService) {
        this.formService = formService;
    }

    @Autowired
    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }

    @Autowired
    public void setPropertyService(PropertyService propertyService) {
        this.propertyService = propertyService;
        System.out.println(" &&&&&&&&&&& PRoperty auto wiered: " + this.propertyService);
    }

    @Override
    public void notify(DelegateExecution execution) throws Exception {

    }
}
[/size]


But nope, it is not happening. No Autowiring. Is bean injection possible in ExecutionListeners ? Any step i could be missing?
Thanks

Outcomes