springboot dubbo @Reference Caused by: java.lang.NullPointerException: null 空指针异常
dubbo @Reference 空指针异常
错误代码摘要
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of @Reference dependencies is failed; nested exception is java.lang.NullPointerException
原因
报错时我也看了很多博客但始终解决不了问题,不是网上所长出现的原因,可以检查下是否是这个原因,因为我主动注入的bean,所以spring没有处理@Reference注解,导致引用的dubbo服务为空
注入bean
@Bean("PermissionsRealm")
public PermissionsRealm getPermissionsRealm() {
PermissionsRealm permissionsRealm = new PermissionsRealm();
return permissionsRealm;
}
PermissionsRealm中自动注入了UserService
@Autowired
private UserService userService;
UserService中使用了dubbo服务
@Reference(check = false)
GatewayService gatewayService;
处理方法
仔细检查下自己主动注入的bean是否有service中引入了dubbo服务,取消注入bean即可
public PermissionsRealm getPermissionsRealm() {
PermissionsRealm permissionsRealm = new PermissionsRealm();
return permissionsRealm;
}
修改后
2020-08-04 09:52:48 [restartedMain] INFO org.apache.coyote.http11.Http11NioProtocol -Starting ProtocolHandler ["http-nio-80"]
2020-08-04 09:52:48 [restartedMain] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer -Tomcat started on port(s): 80 (http) with context path '/'
2020-08-04 09:52:48 [restartedMain] INFO com.scsoft.SpringbootSesamApplication -Started SpringbootSesamApplication in 11.001 seconds (JVM running for 11.923)
思路就是这样,自己变通即可