利用spring产生repositoryService 等服务对象遇到的问题

本文转自http://yy629.iteye.com/blog/671192

 

jbmp4.3 整合spring, 配置文件只注入processEngine 就没问题。 
但是如果注入repositoryService , taskService等就会出现 Is there an unresolvable circular reference? 

Xml代码   收藏代码
  1. <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper" />  
  2. <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" />  



但注入repositoryService: 

Xml代码   收藏代码
  1. <bean id="repositoryService" factory-bean="processEngine"  
  2. factory-method="getRepositoryService" />  


就报如下错误 

Error creating bean with name 'repositoryService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public final org.jbpm.api.RepositoryService org.jbpm.pvm.internal.processengine.SpringProcessEngine$$EnhancerByCGLIB$$66ea6eb4.getRepositoryService()] threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'repositoryService': Requested bean is currently in creation: Is there an unresolvable circular reference? 

分析原因是SpringProcessEngine.getRepositoryService会调用getBean的, 出现循环引用的错误 

Java代码   收藏代码
  1. // SpringProcessEngine中的方法, getRepositoryService,getTaskService,等都是最终调用这个方法的  
  2. public <T> T get(Class<T> type) {  
  3.   String[] names = applicationContext.getBeanNamesForType(type);  
  4.     
  5.   if (names.length >= 1) {  
  6.       
  7.     if (names.length > 1 && log.isWarnEnabled()) {  
  8.       log.warn("Multiple beans for type " + type + " found. Returning the first result.");  
  9.     }  
  10.       
  11.     return (T) applicationContext.getBean(names[0]); // 会出现循环引用错误  
  12.   }  
  13.   
  14.   return super.get(type);  
  15. }  



解决方案就是 

方法1就是 

Xml代码   收藏代码
  1. <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper" />   
  2. <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" />  
  3.   
  4. <bean id="processEngineWireContext" factory-bean="processEngine" factory-method="getProcessEngineWireContext" />  
  5.   
  6. <bean id="repositoryService" factory-bean="processEngineWireContext" factory-method="get">  
  7.   <constructor-arg>  
  8.    <value type="java.lang.Class">org.jbpm.api.RepositoryService</value>  
  9.   </constructor-arg>  
  10. </bean>  



方法2就是在写个工厂类, 通过processEngineWireContext构造出一个工厂类, 工厂类中有getRepositoryService,getTaskService等方法 

你可能感兴趣的:(jbpm,reference,is,An,Circular,There,unresolvable)