Java启动报错记录
错误现象起源
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.xz.unity.busi.question.service.QuestionImportService required a bean of type 'com.xz.questioncloud.provider.api.questions.QuestionDubboService' that could not be found.
Action:
Consider defining a bean of type 'com.xz.questioncloud.provider.api.questions.QuestionDubboService' in your configuration.
Process finished with exit code -1
根据上图显示找到引用的类 QuestionDubboService的地方
package com.xz.unity.busi.question.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.questioncloud.common.base.model.domain.question.Question;
import com.xz.questioncloud.provider.api.questions.QuestionDubboService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
/**
* @description:
* @author: HuangZheng
* @date 2021/1/7
**/
@Service
@RequiredArgsConstructor
public class QuestionImportService {
@DubboReference
private final QuestionDubboService questionDubboService;
private final SysUserService sysUserService;
public Page<Question> listPage(String subjectId, String questionType, Integer studyStage, String status, Integer pageNum, Integer pageSize) {
return questionDubboService.listPage(null, null, null, null, pageNum, pageSize);
}
}
并没有发现明显错误,直到看到 @RequiredArgsConstructor和@DubboReference注解
@RequiredArgsConstructor: 是Lombok插件,会生成一个包含常量 final,和标识了NotNull的变量的构造方法。生成的构造方法是私有的private
@Reference是dubbo的注解,也是注入,他一般注入的是分布式的远程服务的对象,需要dubbo配置使用。
首先加入@RequiredArgsConstructor注解是为为了初始化引入的对象,不然会报空指针异常这是构造函数的目的;QuestionDubboService因为添加了final修饰符所以由上面可知@RequiredArgsConstructor会为final生成构造函数。
但是QuestionDubboService是Dubbo的远程对象在jar包中引入的并不能初始化分配不了内存也就构造函数构造不出来,这个错没有显示的引出来可以如下显示出来理解原理
![](https://img-blog.csdnimg.cn/2021010717432171.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2h1bmFnemhlbmcxMjM0NTY=,size_16,color_FFFFFF,t_70)
所以千万不要对于远程的接口添加final修饰符用来初始化。他们有自己的初始化注解这里是@DubboReference所以正确的写法如下:
@Service
public class QuestionImportService {
@DubboReference
private QuestionDubboService questionDubboService;
public Page<Question> listPage(String subjectId, String questionType, Integer studyStage, String status, Integer pageNum, Integer pageSize) {
return questionDubboService.listPage(null, null, null, null, pageNum, pageSize);
}
}
以后还是想想注解有啥用再加上吧!