# 监听器注入Service报空指针的问题

业务处理过程中,需要在监听器中注入Service,但是常规操作会报空指针,现记录两种处理方法

  • 方法一,在监听器上面加入@Service注解,但是个别环境还是会报空指针,原因暂未知
package fun.gudu.modules.activiti.listener;

import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Service;

@Service("publicExecutionListener")
public class PublicExecutionListener implements ExecutionListener {

}
  • 方法二,利用IOC来注入Service
package gudu.fun.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtil implements ApplicationContextAware {
    /**
     * 当前IOC
     *
     */
    private static ApplicationContext applicationContext;
    /**
     * 设置applicationContext
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 从当前IOC获取bean
     */
    public static <T> T getObject(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }

    public static void showClass(){
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for(String beanDefinitionName : beanDefinitionNames){
            System.out.println(beanDefinitionName);
        }
    }
}
package fun.gudu.modules.activiti.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import fun.gudu.config.SpringUtil;
import fun.gudu.modules.system.entity.SysUserRole;
import fun.gudu.modules.system.service.impl.SysUserRoleServiceImpl;

@Slf4j
public class StartExecutionListener implements ExecutionListener {

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        log.info("--------StartExecutionListener Start-------");
        // Somecode ...
        // 使用重点,注意直接注入实现类,不要注入接口类
        SysUserRoleServiceImpl sysUserRoleService = SpringUtil.getObject(SysUserRoleServiceImpl.class);
        SysUserRole sysUserRole = sysUserRoleService.getOne(sysUserRoleLambdaQueryWrapper);
        // Somecode ...
        log.info("--------StartExecutionListener End-------");
    }
}

# Variable assigneeList is not found 或者 流程处理人和taskService.complete(id, map)时map里面的传值对不上

检查监听器或者拦截器等,将delegateExecution.setVariableLocal("assigneeList", stringList);改成delegateExecution.setVariable("assigneeList", stringList);

不要使用setVariableLocalgetVariableLocal方法

# To Be Continued!😎

Last Updated: 4/20/2023, 9:52:07 AM