AnsweredAssumed Answered

Not able to inject Beans in listeners

Question asked by viral on Jun 24, 2011
Latest reply on Jun 28, 2011 by jbarrez
Hi,

     I have created one usertask with form. I have configured listener to that task. I have used Spring JPA. When i am submitting that form control comes to that listener but that listener not able to inject @PersistenceContext to EntityManager.

Can any one help how could i configure my spring context.

My context file is :

[size=50]<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">




<!–<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:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                         http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">–>

   <context:component-scan base-package="handler.spring" />
   <context:component-scan base-package="org.activiti.designer.test" />
   
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="org.h2.Driver" />
      <property name="url" value="jdbc:h2:tcp://localhost/activiti" />
      <property name="username" value="sa" />
      <property name="password" value="" />
      <property name="defaultAutoCommit" value="false" />
   </bean>
   
   
   <context:annotation-config />
   <context:spring-configured/>
   <!–<mvc:annotation-driven />–>
   <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false" />
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>

   <bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="persistenceXmlLocation">
         <value>classpath:/org/activiti/spring/test/jpa/custom-persistence.xml</value>
      </property>
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
            <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.H2Dictionary" />
         </bean>
      </property>
   </bean>

   <bean name="webScriptsRegistryCache" class="org.alfresco.repo.cache.NullCache" />
   <!–<bean id="aApprovalRequest" class="handler.ApprovalRequest"></bean>–>

   <bean id="approvalRequestBean" class="handler.spring.ApprovalRequestBean">
  </bean>


   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
      <property name="dataSource" ref="dataSource" />
      <property name="transactionManager" ref="transactionManager" />
      <property name="databaseSchemaUpdate" value="true" />
      <property name="jpaEntityManagerFactory" ref="entityManagerFactory" />
      <property name="jpaHandleTransaction" value="false" />
      <property name="jpaCloseEntityManager" value="false" />
      <property name="jobExecutorActivate" value="false" />
   </bean>

   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
      <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" />


</beans>
[/size]


Where as my listener is :
[size=50]public class ApprovalRequestBean implements ExecutionListener {

  @PersistenceContext
  private EntityManager entityManager;
 

//  @Autowired
//  private ApprovalRequestBean approvalRequestBean;
// 
  @Transactional
  public ApprovalRequest newRequest(String userName, String description) {
    ApprovalRequest lr = new ApprovalRequest();
    lr.setName(userName);
    lr.setDescription(description);
    lr.setStatus("Initiated");
   
    System.out.println("  0  "+entityManager);
   
    entityManager.persist(lr);
    return lr;
  }
 
  public ApprovalRequest getRequest(Long id) {
   return entityManager.find(ApprovalRequest.class, id);
  }

@Override
public void notify(DelegateExecution arg0) throws Exception {
   // TODO Auto-generated method stub
   System.out.println("____"+arg0.getVariables());
   newRequest((String)arg0.getVariables().get("Name"),(String)arg0.getVariables().get("Description"));
}
 
//  public List<ApprovalRequest> getLoanRequest() {
//      return entityManager.find(ApprovalRequest.class);
//     }
// 
}[/size]

Outcomes