SpringUtils.getBean 空指针异常问题

因为这个项目license问题无法开源,更多技术支持与服务请加入我的知识星球。

今天在新的jeecg-boot里加入下面的代码

java 复制代码
/**
 * 部门经理处理类
 *
 * @author nbacheng
 * @date 2023-08-06
 */
@AllArgsConstructor
@Component("DepManagerHandler")
@DependsOn({"SpringContextUtils"})
public class DepManagerHandler {

	//IFlowThirdService flowThirdService = SpringContextUtils.getBean(IFlowThirdService.class);
	RuntimeService runtimeService = SpringContextUtils.getBean(RuntimeService.class);
	IFlowThirdService flowThirdService = SpringContextUtils.getBean(IFlowThirdService.class);

	public List<String> getUsers(DelegateExecution execution) {
		List<String> assignUserName = new ArrayList<String>();
		FlowElement flowElement = execution.getCurrentFlowElement();
        if (ObjectUtil.isNotEmpty(flowElement) && flowElement instanceof UserTask) {
            UserTask userTask = (UserTask) flowElement;
            if ( ObjectUtil.isNotEmpty(userTask.getCandidateGroups())) {
            	if(StringUtils.contains(userTask.getCandidateGroups().get(0),"DepManagerHandler")) {
            		 // 获取流程发起人
            		ProcessInstance processInstance = runtimeService
                            .createProcessInstanceQuery()
                            .processInstanceId(execution.getProcessInstanceId())
                            .singleResult();
                    String startUserId = processInstance.getStartUserId();
                    // 获取部门负责人列表
            		List<String> depIds = flowThirdService.getDepartIdsByUsername(startUserId);
            		List<String> DepHeadlist = new ArrayList<String>();
            		for(String depId: depIds) {
            			List<String> depList = flowThirdService.getDeptHeadByDepId(depId);
            			if(depList != null) {
            				DepHeadlist.addAll(depList);
            			}
            			
            		}
            		// 部门负责人列表去重
            		if(!DepHeadlist.isEmpty() ) {
            		    for (String str : DepHeadlist) {
            		        if (!assignUserName.contains(str)) {
            		        	assignUserName.add(str);
            		        }
            		    }
            		}
            	}
            }
        }    
        return assignUserName;
		
	}
}

结果报 下面错误

java 复制代码
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-06-23 10:17:27.770 [main] [1;31mERROR[0;39m [36morg.springframework.boot.SpringApplication:818[0;39m - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DepManagerHandler' defined in file [F:\codestudy\jeecg-boot-v3.6.3\jeecg-module-flowable\target\classes\org\jeecg\modules\flowable\listener\DepManagerHandler.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.jeecg.modules.flowable.listener.DepManagerHandler]: Constructor threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:291)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:929)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:591)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289)
	at org.jeecg.JeecgSystemApplication.main(JeecgSystemApplication.java:33)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.jeecg.modules.flowable.listener.DepManagerHandler]: Constructor threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:226)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:306)
	... 19 common frames omitted
Caused by: java.lang.NullPointerException: null
	at org.jeecg.common.util.SpringContextUtils.getBean(SpringContextUtils.java:99)
	at org.jeecg.modules.flowable.listener.DepManagerHandler.<init>(DepManagerHandler.java:31)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:213)
	... 21 common frames omitted

后来在start模块里增加下面的初始化,以便先初始化jeecg-module-flowable的服务组件

java 复制代码
<dependencies>
        <!-- SYSTEM 系统管理模块 -->
        <dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-system-biz</artifactId>
            <version>${jeecgboot.version}</version>
        </dependency>
        <!-- DEMO 示例模块 -->
        <dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-module-demo</artifactId>
            <version>${jeecgboot.version}</version>
        </dependency>
        <!--流程模块-->
		<dependency>
			<groupId>org.jeecgframework.boot</groupId>
			<artifactId>jeecg-module-flowable</artifactId>
			<version>${jeecgboot.version}</version>
		</dependency>

之后启动正常了。

相关推荐
世间万物皆对象3 分钟前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了32 分钟前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·40 分钟前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic1 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王1 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康1 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神2 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
qq_327342732 小时前
Java实现离线身份证号码OCR识别
java·开发语言
阿龟在奔跑3 小时前
引用类型的局部变量线程安全问题分析——以多线程对方法局部变量List类型对象实例的add、remove操作为例
java·jvm·安全·list
飞滕人生TYF3 小时前
m个数 生成n个数的所有组合 详解
java·递归