基于注解 Spring Activiti ProcessEngine
Annotation based Spring Activiti ProcessEngine
我对 Spring 和 Activiti 很陌生。我正在使用嵌入式 Activiti 引擎开发基于注释的 Spring Web 应用程序。我实现了一些服务,SubscriptionService 就是其中之一。在一个过程中,我将该服务称为 bean,其中:
activiti:expression="${subscriptionService.getContacts(publisherCode, contactType)}"
服务:
@Service
@Transactional
public class DBSubscriptionService implements SubscriptionService {
...
}
我把测试模块和网络模块分开了。测试模块从 activiti.cfg.xml 和
加载上下文
@ContextConfiguration("classpath:activiti.cfg.xml")
它是:
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<property name="databaseType" value="h2" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
<property name="history" value="full" />
<property name="mailServerPort" value="2025" />
</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" />
<bean id="identityService" factory-bean="processEngine"
factory-method="getIdentityService" />
<bean id="formService" factory-bean="processEngine"
factory-method="getFormService" />
<context:component-scan base-package="cz.muni.fi.cep" />
<context:annotation-config />
在测试模块中,一切正常。但是在 web 模块中,当它应该调用 subscriptionService Bean 时它会抛出:
Unknown property used in expression: ${subscriptionService.getContacts(publisherCode, contactType)}
Caused by: org.activiti.engine.impl.javax.el.PropertyNotFoundException: Cannot resolve identifier 'subscriptionService'
at org.activiti.engine.impl.juel.AstIdentifier.eval(AstIdentifier.java:83)
at org.activiti.engine.impl.juel.AstMethod.invoke(AstMethod.java:79)
at org.activiti.engine.impl.juel.AstMethod.eval(AstMethod.java:75)
at org.activiti.engine.impl.juel.AstEval.eval(AstEval.java:50)
at org.activiti.engine.impl.juel.AstNode.getValue(AstNode.java:26)
at org.activiti.engine.impl.juel.TreeValueExpression.getValue(TreeValueExpression.java:114)
at org.activiti.engine.impl.delegate.ExpressionGetInvocation.invoke(ExpressionGetInvocation.java:33)
at org.activiti.engine.impl.delegate.DelegateInvocation.proceed(DelegateInvocation.java:37)
at org.activiti.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocation(DefaultDelegateInterceptor.java:25)
at org.activiti.engine.impl.el.JuelExpression.getValue(JuelExpression.java:48)
... 363 more
所以我明白问题是流程引擎没有使用正确的应用程序上下文,因为订阅服务自动连接到其他 类 就好了。但是我不知道怎么解决。
Web 模块完全基于注解:
@EnableAutoConfiguration
@Configuration
@EntityScan(basePackages = "cz.muni.fi.cep")
@ComponentScan(basePackages = "cz.muni.fi.cep")
public class App extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
...
}
和Activiti配置:
@Configuration
public class ActivitiConfig {
@Bean
public ProcessEngine processEngine(ProcessEngineConfigurationImpl pec, ApplicationContext applicationContext) throws Exception {
ProcessEngineFactoryBean pe = new ProcessEngineFactoryBean();
pe.setProcessEngineConfiguration(pec);
pe.setApplicationContext(applicationContext);
return pe.getObject();
}
@Bean
public ProcessEngineConfigurationImpl getProcessEngineConfiguration(
DataSource dataSource,
PlatformTransactionManager transactionManager,
ApplicationContext context) {
SpringProcessEngineConfiguration pec = new SpringProcessEngineConfiguration();
pec.setDataSource(dataSource);
pec.setDatabaseSchemaUpdate("true");
pec.setJobExecutorActivate(true);
pec.setHistory("full");
pec.setMailServerPort(2025);
pec.setDatabaseType("mysql");
pec.setTransactionManager(transactionManager);
pec.setApplicationContext(context);
return pec;
}
@Bean
public RuntimeService getRuntimeService(ProcessEngine processEngine) {
return processEngine.getRuntimeService();
}
...
}
此外,上下文创建的顺序可能与此有关。
一切正常,我相信可能是因为订阅服务没有被扫描(它属于包 cz.muni.fi.cep 吗?),或者 Spring 为订阅分配了另一个名称服务 bean(即 dbSubscriptionService
或 dBSubscriptionService
而不仅仅是 subscriptionService
)。
尝试用 @Service("subscriptionService")
替换 DBSubscriptionService
class 中的 @Service
,应该可以。
我对 Spring 和 Activiti 很陌生。我正在使用嵌入式 Activiti 引擎开发基于注释的 Spring Web 应用程序。我实现了一些服务,SubscriptionService 就是其中之一。在一个过程中,我将该服务称为 bean,其中:
activiti:expression="${subscriptionService.getContacts(publisherCode, contactType)}"
服务:
@Service
@Transactional
public class DBSubscriptionService implements SubscriptionService {
...
}
我把测试模块和网络模块分开了。测试模块从 activiti.cfg.xml 和
加载上下文@ContextConfiguration("classpath:activiti.cfg.xml")
它是:
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<property name="databaseType" value="h2" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
<property name="history" value="full" />
<property name="mailServerPort" value="2025" />
</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" />
<bean id="identityService" factory-bean="processEngine"
factory-method="getIdentityService" />
<bean id="formService" factory-bean="processEngine"
factory-method="getFormService" />
<context:component-scan base-package="cz.muni.fi.cep" />
<context:annotation-config />
在测试模块中,一切正常。但是在 web 模块中,当它应该调用 subscriptionService Bean 时它会抛出:
Unknown property used in expression: ${subscriptionService.getContacts(publisherCode, contactType)}
Caused by: org.activiti.engine.impl.javax.el.PropertyNotFoundException: Cannot resolve identifier 'subscriptionService'
at org.activiti.engine.impl.juel.AstIdentifier.eval(AstIdentifier.java:83)
at org.activiti.engine.impl.juel.AstMethod.invoke(AstMethod.java:79)
at org.activiti.engine.impl.juel.AstMethod.eval(AstMethod.java:75)
at org.activiti.engine.impl.juel.AstEval.eval(AstEval.java:50)
at org.activiti.engine.impl.juel.AstNode.getValue(AstNode.java:26)
at org.activiti.engine.impl.juel.TreeValueExpression.getValue(TreeValueExpression.java:114)
at org.activiti.engine.impl.delegate.ExpressionGetInvocation.invoke(ExpressionGetInvocation.java:33)
at org.activiti.engine.impl.delegate.DelegateInvocation.proceed(DelegateInvocation.java:37)
at org.activiti.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocation(DefaultDelegateInterceptor.java:25)
at org.activiti.engine.impl.el.JuelExpression.getValue(JuelExpression.java:48)
... 363 more
所以我明白问题是流程引擎没有使用正确的应用程序上下文,因为订阅服务自动连接到其他 类 就好了。但是我不知道怎么解决。
Web 模块完全基于注解:
@EnableAutoConfiguration
@Configuration
@EntityScan(basePackages = "cz.muni.fi.cep")
@ComponentScan(basePackages = "cz.muni.fi.cep")
public class App extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
...
}
和Activiti配置:
@Configuration
public class ActivitiConfig {
@Bean
public ProcessEngine processEngine(ProcessEngineConfigurationImpl pec, ApplicationContext applicationContext) throws Exception {
ProcessEngineFactoryBean pe = new ProcessEngineFactoryBean();
pe.setProcessEngineConfiguration(pec);
pe.setApplicationContext(applicationContext);
return pe.getObject();
}
@Bean
public ProcessEngineConfigurationImpl getProcessEngineConfiguration(
DataSource dataSource,
PlatformTransactionManager transactionManager,
ApplicationContext context) {
SpringProcessEngineConfiguration pec = new SpringProcessEngineConfiguration();
pec.setDataSource(dataSource);
pec.setDatabaseSchemaUpdate("true");
pec.setJobExecutorActivate(true);
pec.setHistory("full");
pec.setMailServerPort(2025);
pec.setDatabaseType("mysql");
pec.setTransactionManager(transactionManager);
pec.setApplicationContext(context);
return pec;
}
@Bean
public RuntimeService getRuntimeService(ProcessEngine processEngine) {
return processEngine.getRuntimeService();
}
...
}
此外,上下文创建的顺序可能与此有关。
一切正常,我相信可能是因为订阅服务没有被扫描(它属于包 cz.muni.fi.cep 吗?),或者 Spring 为订阅分配了另一个名称服务 bean(即 dbSubscriptionService
或 dBSubscriptionService
而不仅仅是 subscriptionService
)。
尝试用 @Service("subscriptionService")
替换 DBSubscriptionService
class 中的 @Service
,应该可以。