`

二十一、如何在Jbpm的各种Handler中访问spring的bean

 
阅读更多

 

 

 

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springmodules.workflow.jbpm31.JbpmFactoryLocator;

public class BaseAutowire {
   
    //自动为该类的子类的属性赋值,该属性必须有@Rource注解,值从spring容器里面查找,
    public BaseAutowire() {
        ((AutowireCapableBeanFactory) retrieveBeanFactory()).autowireBeanProperties(this,
            AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);

    }
   
    //获得spring容器
    protected BeanFactory retrieveBeanFactory() {
        BeanFactoryLocator factoryLocator = new JbpmFactoryLocator();
        BeanFactoryReference factory = factoryLocator.useBeanFactory(null);
        if (factory == null)
            throw new IllegalArgumentException("no beanFactory found under key=" + null);
        try {
            return factory.getFactory();
        } finally {
            factory.release();
        }
       
    }
}

 

现在,我们的任何Handler只需要继承BaseAutowire,就可以在属性上面使用 @Rource注解,从而获得spring的注入。

 


  • 大小: 52.5 KB
  • 大小: 138.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics