kaka
2017-05-11 13:10:38
未被Spring托管的Bean如何获取Spring管理的Bean
我在activiti的流程定义文件中,如下xml,配置了某一个节点的TaskListener,部署成功后,Activiti在每一步处理时,如果发现某节点配置了这个Listener,就会去初始化这个Listener,那么问题来了,Activiti初始化这个Listener的方式是通过new的方式去创建的,但是这个Listener中引用的service都是spring托管的,此时就不能简单的用@Autowird去注解了。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="www.suninfo.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1492496966242" name="" targetNamespace="www.suninfo.com" typeLanguage="http://www.w3.org/2001/XMLSchema"> <process activiti:candidateStarterGroups="353d855367dc423bb82b091ee55358df" id="destroyKnowledge" isClosed="false" isExecutable="true" name="注销知识流程" processType="None"> <startEvent activiti:initiator="applyUserId" id="startDestroyKonw" name="开始注销知识"> <extensionElements> <activiti:executionListener class="com.suninfo.event.StartListener" event="start"/> </extensionElements> </startEvent> <endEvent id="endDestroyKonw" name="结束注销知识"/> <userTask activiti:candidateGroups="4775a872f44241cdaf26ea08253c58c1" activiti:exclusive="true" id="auditKnow" name="审批注销"> <extensionElements> <activiti:taskListener class="com.suninfo.event.NoticeTaskListener" event="create"/> </extensionElements> </userTask>
1. 定义一个工具类SpringContextHolder
package com.suninfo.util.spring;/** * Created by kaka on 2017/4/14. */ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * spring工具类 * * @author kaka * @create 2017-04-14 11:05 **/ public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger LOGGER = LoggerFactory.getLogger(SpringContextHolder.class); /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } /** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { LOGGER.debug("从SpringContextHolder中取出Bean:" + name); assertContextInjected(); return (T) applicationContext.getBean(name); } /** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } /** * 清除SpringContextHolder中的ApplicationContext为Null. */ public static void clearHolder() { LOGGER.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); applicationContext = null; } /** * 实现ApplicationContextAware接口, 注入Context到静态变量中. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { // logger.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext); if (SpringContextHolder.applicationContext != null) { LOGGER.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext); } SpringContextHolder.applicationContext = applicationContext; // NOSONAR } /** * 实现DisposableBean接口, 在Context关闭时清理静态变量. */ @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } /** * 检查ApplicationContext不为空. */ private static void assertContextInjected() { if(applicationContext == null) { throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder."); } } }
2. 在spring的配置文件中初始化SpringContextHolder
<!--Spring工具类,用于没有通过spring托管的servcie初始化使用--> <bean id="springContextHolder" class="com.suninfo.util.spring.SpringContextHolder"></bean>
3. 在未被托管的service里调用
package com.suninfo.event;/** * Created by kaka on 2017/5/10. */ import com.suninfo.util.spring.SpringContextHolder; import com.suninfo.workflow.WorkFlowService; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.ExecutionListener; /** * * * @author kaka * @create 2017-05-10 10:38 **/ public class StartListener implements ExecutionListener{ private WorkFlowService workFlowService; public StartListener(){ workFlowService = SpringContextHolder.getBean("workflowService"); } @Override public void notify(DelegateExecution execution) throws Exception { System.out.println(); } }
评论