NutzCN Logo
问答 Activiti中表达式中获取ioc管理的Bean失败
发布于 2769天前 作者 htgylzhq 2831 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

1. bpmn中相关的定义:

    <extensionElements>
      <activiti:executionListener event="start" delegateExpression="${processStartListener}"></activiti:executionListener>
      <activiti:executionListener event="end" delegateExpression="${processEndListener}"></activiti:executionListener>
    </extensionElements>

2. ProcessStartListener的定义:

@IocBean
public class ProcessStartListener implements ExecutionListener {

  @Override
  public void notify(DelegateExecution execution) throws Exception {
    System.out.println(execution.getProcessBusinessKey());
    System.out.println(execution.getProcessInstanceId());
  }

}

3. 报错信息

org.activiti.engine.ActivitiException: Unknown property used in expression: ${processStartListener}
	at org.activiti.engine.impl.el.JuelExpression.getValue(JuelExpression.java:53)
	at org.activiti.engine.impl.bpmn.listener.DelegateExpressionExecutionListener.notify(DelegateExpressionExecutionListener.java:45)
	at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:42)
	at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:97)
	at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:650)
	at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:643)
	at org.activiti.engine.impl.persistence.entity.ExecutionEntity.start(ExecutionEntity.java:388)
Caused by: org.activiti.engine.impl.javax.el.PropertyNotFoundException: Cannot resolve identifier 'processStartListener'
	at org.activiti.engine.impl.juel.AstIdentifier.eval(AstIdentifier.java:83)
	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:50)
	... 84 more
6 回复

这是全部报错信息了?

完整的报错信息:

[ERROR] 2017-05-24 19:20:13,563 org.activiti.engine.impl.interceptor.CommandContext.close(CommandContext.java:171) - Error while closing command context
org.activiti.engine.ActivitiException: Unknown property used in expression: ${processStartListener}
	at org.activiti.engine.impl.el.JuelExpression.getValue(JuelExpression.java:53)
	at org.activiti.engine.impl.bpmn.listener.DelegateExpressionExecutionListener.notify(DelegateExpressionExecutionListener.java:45)
	at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:42)
	at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:97)
	at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:650)
	at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:643)
	at org.activiti.engine.impl.persistence.entity.ExecutionEntity.start(ExecutionEntity.java:388)
	at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:110)
	at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:37)
	at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:24)
	at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:57)
	at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:37)
	at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:40)
	at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:35)
	at org.activiti.engine.impl.RuntimeServiceImpl.startProcessInstanceByKey(RuntimeServiceImpl.java:78)
	at cn.wizzer.app.web.modules.controllers.platform.cppcc.ProposalController.lambda$saveAndSubmit$0(ProposalController.java:213)
	at org.nutz.trans.Trans.exec(Trans.java:174)
	at org.nutz.trans.Trans.exec(Trans.java:132)
	at cn.wizzer.app.web.modules.controllers.platform.cppcc.ProposalController.saveAndSubmit(ProposalController.java:210)
	at cn.wizzer.app.web.modules.controllers.platform.cppcc.ProposalController$FM$saveAndSubmit$db3aa821a440bb7282479fc38ed4cf36.invoke(ProposalController.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:35)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.NutShiroProcessor.process(NutShiroProcessor.java:52)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:21)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
	at cn.wizzer.app.web.commons.filter.RouteFilter.doFilter(RouteFilter.java:30)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1613)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1239)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1561)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1141)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:118)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:564)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:122)
	at org.eclipse.jetty.util.thread.strategy.ExecutingExecutionStrategy.invoke(ExecutingExecutionStrategy.java:58)
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:201)
	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:133)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.activiti.engine.impl.javax.el.PropertyNotFoundException: Cannot resolve identifier 'processStartListener'
	at org.activiti.engine.impl.juel.AstIdentifier.eval(AstIdentifier.java:83)
	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:50)
	... 88 more

debug一下ActivitiNutIocBeansResolverFactory类的方法,看看有无调用过

没有进入 ActivitiNutIocBeansResolverFactory

我草草地实验了一下,按下面这种做法是可以的,这个是写在Setup里面的:

    public void initActiviti(NutConfig config) {
        Ioc ioc = config.getIoc();
        StandaloneProcessEngineConfiguration spec =
          (StandaloneProcessEngineConfiguration) ioc.get(ProcessEngineConfiguration.class, "processEngineSpec");
        /*Map<Object, Object> beans = new HashMap<>();
        beans.put("processStartListener", ioc.get(ProcessStartListener.class));
        beans.put("processEndListener", ioc.get(ProcessEndListener.class));
        beans.put("taskCreateListener", ioc.get(TaskCreateListener.class));
        spec.setBeans(beans);*/
        spec.setExpressionManager(new ExpressionManager() {
            @Override
            protected ELResolver createElResolver(VariableScope variableScope) {
                CompositeELResolver elResolver = (CompositeELResolver) super.createElResolver(variableScope);
                elResolver.add(new ELResolver() {
                    @Override
                    public Class<?> getCommonPropertyType(ELContext context, Object base) {
                        return Object.class;
                    }

                    @Override
                    public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
                        return null;
                    }

                    @Override
                    public Class<?> getType(ELContext context, Object base, Object property) {
                        return Object.class;
                    }

                    @Override
                    public Object getValue(ELContext context, Object base, Object property) {
                        Ioc ioc = Mvcs.getActionContext().getIoc();
                        if (ArrayUtils.contains(ioc.getNames(), property.toString())) {
                            context.setPropertyResolved(true);
                            return ioc.get(null, property.toString());
                        } else {
                            return null;
                        }
                    }

                    @Override
                    public boolean isReadOnly(ELContext context, Object base, Object property) {
                        return true;
                    }

                    @Override
                    public void setValue(ELContext context, Object base, Object property, Object value) {

                    }
                });
                return elResolver;
            }
        });

        // ProcessEngine processEngine = config.getIoc().get(ProcessEngine.class);
        ProcessEngine processEngine = spec.buildProcessEngine();
        DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService()
                .createDeployment().enableDuplicateFiltering();
        List<NutResource> nutResources = Scans.me().scan("/bpmn", ".*\\.bpmn");
        nutResources.forEach(nutResource -> {
            try {
                deploymentBuilder.addInputStream(nutResource.getName(), nutResource.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        deploymentBuilder.deploy();
        log.debug("Activiti Process Engine Inited: " + processEngine.getName());
    }

关键是向ProcessEngineConfiguration的ExpressionManager里面添加一个ELResolver,让它从ioc里面获取bean,就是这几行:

                    @Override
                    public Object getValue(ELContext context, Object base, Object property) {
                        Ioc ioc = Mvcs.getActionContext().getIoc();
                        if (ArrayUtils.contains(ioc.getNames(), property.toString())) {
                            context.setPropertyResolved(true);
                            return ioc.get(null, property.toString());
                        } else {
                            return null;
                        }
                    }
添加回复
请先登陆
回到顶部