0

从任务中调用服务的方法时,我在 Activiti Explorer 中使用 Spring 的自动装配功能时遇到问题。这个想法是让服务任务调用 Spring @Service bean 上的方法之一,以便使用 @Autowired JPARepository 持久化数据。

问题是,在执行服务任务后,由于 myService 中的 @Autowired 存储库未正确实例化,我得到一个空指针异常。

那么我的问题是,我怎样才能从任务服务中正确调用 Spring bean?

JavaDelegate 方法不适用于 Spring,我尝试采用“Expression”方法,如此处所建议,但无济于事。

这是 Service Task 运行方法的代码,它的运行方式如下:

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

//被调用的java类

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

    }

这是我的服务:

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
}

从我们的 web 应用程序调用时,同样的事情没有问题(将服务调用为 @ManagedProperty 工作正常),因此嵌入式项目的配置似乎没问题。

这是 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 托管项目

提前致谢,

加斯顿

4

3 回答 3

3

Did you try using the hash-sign instead of the dollar?

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

This works in my Spring+Activiti Configuration

于 2012-11-27T13:49:09.300 回答
2

这最终起作用了:

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

其中 applicationContextActiviti 是声明了 TestServiceDummy 服务的自定义 appContext 配置文件。我在任务调用的 JavaDelegate 类中使用它。

基本上,从 Activiti Explorer 我看不到 Spring Beans(除非你弄脏了你的手)所以我所做的是跳过 @Autowired 并使用旧的手动方法来处理 bean,然后从调用者类中加载它们.

把这个留在这里,以防它希望将来有时间旅行者:P

于 2012-12-02T14:43:22.310 回答
0

It seems that testServiceTask is not declared in spring config file.

于 2014-03-12T12:42:05.800 回答