cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to inject Beans in listeners

viral
Champ in-the-making
Champ in-the-making
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]
7 REPLIES 7

jbarrez
Star Contributor
Star Contributor
Did you set the required Spring scanner for @PersistenceContext support ?
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

viral
Champ in-the-making
Champ in-the-making
It is also not working after adding Spring scanner for @PersistenceContext. It is working w/o Spring scanner for @PersistenceContext when i am working JUNIT. But is not woking when i an relacing activiti.cfg.xml in rest application

Thanks,
Viral

jbarrez
Star Contributor
Star Contributor
How are you replacing the activiti.cfg.xml in the REST app? Do you change the spring config manually there?

viral
Champ in-the-making
Champ in-the-making
No, I am creating activiti-cfg.jar for my activiti.cfg.xml and then Relacing the activiti-cfg.jar .

jbarrez
Star Contributor
Star Contributor
A regular activiti.cfg.xml doesnt have Spring support, so thats why it doesnt work.
You need a 'real' spring-Activiti config to make it work.

viral
Champ in-the-making
Champ in-the-making
Thanks,
But I have configured that as given in user guide. So where do i get help for 'real' spring-Activiti.

jbarrez
Star Contributor
Star Contributor
To be more precise: the activiti-rest webapp uses a Spring appliation context to configure itself.
The engine however, is not configured in Spring. There is an activiti.cfg.jar placed on the classpath, containing a process engine configuration for which the engine is called 'default'.
The process engine is then referenced in the Spring context using the 'default' name.

If you want to tweak the activiti-rest webapp, and you want to use Spring you'll have to
- remove the activiti.cfg.jar or any other activiti.cfg.xml on the classpath
- add your process engine configuration to the applicationcontext (webapplicationtext.xml) of the activiti-rest webapp.