2012-11-18 51 views
0

我在使用Activiti Explorer中的自動裝配功能時,在從任務中調用服務的方法時遇到問題。這個想法是讓服務任務調用Spring @Service bean上的方法之一,以便使用@Autowired JPARepository來保存數據。在Activiti中使用自動佈線Spring beans的任務

問題是,在執行服務任務後,由於myService中的@Autowired存儲庫未正確實例化,我得到一個空指針異常。

那麼我的問題是我該如何從任務服務中正確調用Spring bean?

JavaDelegate方法does not work與春天,我試過去「表達式」的方法,建議here無濟於事。

下面是服務任務的運行方法的代碼,目前正在運行爲:

activiti:expression="${testServiceTask.doSomething()}" 

//這就是被稱爲

public class testServiceTask { 

@Autowired 
private TestServiceDummy serviceDummy; 

public void doSomething() { 
    serviceDummy.run(); // NPE here, the serviceDummy is null when called 
    } 
    // Getters and Setters for the testServiceDummy omitted for brevity 

    } 

這裏的Java類是我服務的:

public interface TestServiceDummy { 

public void createUser(); 

} 


@Service(value = "testServiceDummyImpl") 
@Transactional(readOnly = true) 
public class TestServiceDummyImpl implements TestServiceDummy { 

    @Autowired 
    private UserRepository userRepo; 

@Override 
public void createUser() { 
    User u = new User(); 
     userRepo.save(u); 
    } 

    // Getters and Setters for userRepo omitted for brevity 
} 

從我們的webapp調用相同的東西時沒有問題(將服務作爲@ManagedPr調用operty工作正常),所以嵌入式項目的配置似乎沒問題。

而這裏的Activiti Explorer的的applicationContext文件:

<?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:jpa="http://www.springframework.org/schema/data/jpa" 
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.1.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/data/jpa 
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
     http://www.springframework.org/schema/jdbc 
     http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.1.xsd"> 

<import resource="classpath*:/applicationContextCore.xml" /> 
<context:property-placeholder location="classpath*:jdbc.properties" /> 

<!-- Scan this classpath for annotated components that will be auto-registered 
    as Spring beans --> 
<context:annotation-config /> <!-- this should take care of the @Autowiring issue --> 

<!-- scan the embedded project's components --> 
    <context:component-scan base-package="my.project.*" /> 

<jpa:repositories base-package="my.project.repositories*" /> 

<bean 
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

<!-- Automatically translate hibernate/jpa exceptions into Spring's generic 
    DataAccessException hierarchy for those classes annotated with Repository --> 
<bean 
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

<bean id="testServiceDummy" class="edu.bedelias.services.TestServiceDummyImpl" /> 

<!-- JPA Entity Manager Factory --> 
<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /> 
    <property name="packagesToScan"> 
     <list> 
      <value>edu.bedelias.*</value> 
     </list> 
    </property> 
    <property name="jpaProperties"> 
     <props> 
      <!-- set HibernateJpaVendorAdapter's behavior: 'create' = build a new 
       DB on each run; 'update' = modify an existing database; 'create-drop' = 'create' 
       and also drops tables when Hibernate closes; 'validate' = makes no changes 
       to the database --> 
      <prop key="hibernate.hbm2ddl.auto">create-drop</prop> 
     </props> 
    </property> 
</bean> 

<bean id="hibernateJpaVendorAdapter" 
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
    <property name="showSql" value="true" /> 
    <property name="generateDdl" value="false" /> 
    <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> 
</bean> 

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
    destroy-method="close"> 
    <property name="driverClass" value="${jdbc.driverClass}" /> 
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> 
    <property name="user" value="${jdbc.user}" /> 
    <property name="password" value="${jdbc.password}" /> 
    <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> 
    <property name="maxStatements" value="${jdbc.maxStatements}" /> 
    <property name="minPoolSize" value="${jdbc.minPoolSize}" /> 
</bean> 

<!-- Transaction Manager is defined --> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

<!-- Hijack the current @Session scope annotation on each @Service and make 
    it last only for the duration of the thread --> 
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> 
    <property name="scopes"> 
     <map> 
      <entry key="session"> 
       <bean class="org.springframework.context.support.SimpleThreadScope" /> 
      </entry> 
     </map> 
    </property> 
</bean> 

<!-- Enable the configuration of transactional behavior based on annotations --> 
<tx:annotation-driven /> 

<bean id="dbProperties" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value="classpath:db.properties" /> 
    <!-- Allow other PropertyPlaceholderConfigurer to run as well --> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
</bean> 

<bean id="demoDataGenerator" class="org.activiti.explorer.demo.DemoDataGenerator"> 
    <property name="processEngine" ref="processEngine" /> 
</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="jobExecutorActivate" value="true" /> 
    <property name="customFormTypes"> 
     <list> 
      <ref bean="userFormType" /> 
     </list> 
    </property> 
</bean> 

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

<bean id="activitiLoginHandler" class="org.activiti.explorer.ui.login.DefaultLoginHandler"> 
    <property name="identityService" ref="identityService" /> 
</bean> 

<!-- Include the UI-related wiring. This UI context will be used in the 
    alfresco activiti admin UI --> 
<import resource="activiti-ui-context.xml" /> 

<!-- Custom form types --> 
<bean id="userFormType" class="org.activiti.explorer.form.UserFormType" /> 

如果有人想了解,該項目的URL是在這裏:提前 Google Code hosted project

感謝,

加斯頓

回答

2

這結束了工作:

ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("classpath:applicationContextActiviti.xml"); 
TestServiceDummy = (TestServiceDummy) cpx.getBean("testServiceDummy"); 

哪裏applicationContextActiviti與TestServiceDummy服務定製appContext配置文件中聲明。我在被任務調用的JavaDelegate類中使用它。

基本上,從Activiti Explorer中我沒有對Spring Beans的可見性(除非你得到your hands dirty),所以我做的是跳過@Autowired並使用舊的手動方法來bean,然後從來電者類。

就要離開這個位置的情況下,它希望今後的任何時間旅行者:P

+0

這對我來說根本不起作用。 –

3

您是否嘗試使用散列符號代替美元?

activiti:expression="#{testServiceTask.doSomething()}" 

此作品在我的春節+ Activiti的配置

+0

感謝您的建議,但似乎我的問題在於如何在Activiti中使用Spring。見答案。 – Gaston

0

似乎testServiceTask沒有在Spring配置文件中聲明。

+0

這是一種簡潔的答案,你不覺得嗎? –

相關問題