淘淘商城系列——(1)使用spring和dubbo报错:Error creating bean with name 'com.gsyshop.service.ItemService'

1 篇文章 0 订阅
1 篇文章 0 订阅

此系列博客来记录做淘淘商城项目(学习黑马&传智视频)中遇到的难点,如有不正确地方请大家指出交流。

 


1、问题描述:因为项目使用的是SOA架构,因此项目是拆分为服务层(service\pojo\dao\interface)和表现层(web)两个工程,所以启动时先启动的servic工程再启动web工程,在启动service工程时报错:

2019-09-21 01:43:41 [ERROR] -ContextLoader.java(353) -Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.gsyshop.service.ItemService': Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.ExceptionInInitializerError
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
    ... 22 more
Caused by: java.lang.IllegalStateException: fail to create adaptive instance: java.lang.IllegalStateException: Can not create adaptive extenstion interface com.alibaba.dubbo.rpc.Protocol, cause: java.io.IOException: invalid constant type: 18
    at com.alibaba.dubbo.common.extension.ExtensionLoader.getAdaptiveExtension(ExtensionLoader.java:459)
    at com.alibaba.dubbo.config.ServiceConfig.<clinit>(ServiceConfig.java:60)
    ... 30 more
Caused by: java.lang.IllegalStateException: Can not create adaptive extenstion interface com.alibaba.dubbo.rpc.Protocol, cause: java.io.IOException: invalid constant type: 18
    at com.alibaba.dubbo.common.extension.ExtensionLoader.createAdaptiveExtension(ExtensionLoader.java:723)
    at com.alibaba.dubbo.common.extension.ExtensionLoader.getAdaptiveExtension(ExtensionLoader.java:455)
    ... 31 more
Caused by: java.lang.RuntimeException: java.io.IOException: invalid constant type: 18
    at javassist.CtClassType.getClassFile2(CtClassType.java:204)
    at javassist.CtClassType.subtypeOf(CtClassType.java:304)
    at javassist.compiler.MemberResolver.compareSignature(MemberResolver.java:248)
    at javassist.compiler.MemberResolver.lookupMethod(MemberResolver.java:120)
    at javassist.compiler.MemberResolver.lookupMethod(MemberResolver.java:97)
    at javassist.compiler.MemberCodeGen.atMethodCallCore(MemberCodeGen.java:562)
    at javassist.compiler.MemberCodeGen.atCallExpr(MemberCodeGen.java:524)
    at javassist.compiler.JvstCodeGen.atCallExpr(JvstCodeGen.java:244)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
    at javassist.compiler.MemberCodeGen.atCallExpr(MemberCodeGen.java:497)
    at javassist.compiler.JvstCodeGen.atCallExpr(JvstCodeGen.java:244)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
    at javassist.compiler.MemberCodeGen.atCallExpr(MemberCodeGen.java:497)
    at javassist.compiler.JvstCodeGen.atCallExpr(JvstCodeGen.java:244)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
    at javassist.compiler.MemberCodeGen.atCallExpr(MemberCodeGen.java:497)
    at javassist.compiler.JvstCodeGen.atCallExpr(JvstCodeGen.java:244)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
    at javassist.compiler.CodeGen.atBinExpr(CodeGen.java:964)
    at javassist.compiler.ast.BinExpr.accept(BinExpr.java:41)
    at javassist.compiler.JvstCodeGen.atMethodArgs(JvstCodeGen.java:358)
    at javassist.compiler.MemberCodeGen.atMethodCallCore(MemberCodeGen.java:556)
    at javassist.compiler.MemberCodeGen.atNewExpr(MemberCodeGen.java:302)
    at javassist.compiler.ast.NewExpr.accept(NewExpr.java:73)
    at javassist.compiler.CodeGen.compileExpr(CodeGen.java:230)
    at javassist.compiler.CodeGen.atThrowStmnt(CodeGen.java:628)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:365)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
    at javassist.compiler.CodeGen.atIfStmnt(CodeGen.java:391)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:355)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:351)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
    at javassist.compiler.CodeGen.atMethodBody(CodeGen.java:292)
    at javassist.compiler.CodeGen.atMethodDecl(CodeGen.java:274)
    at javassist.compiler.ast.MethodDecl.accept(MethodDecl.java:44)
    at javassist.compiler.Javac.compileMethod(Javac.java:169)
    at javassist.compiler.Javac.compile(Javac.java:95)
    at javassist.CtNewMethod.make(CtNewMethod.java:74)
    at javassist.CtNewMethod.make(CtNewMethod.java:45)
    at com.alibaba.dubbo.common.compiler.support.JavassistCompiler.doCompile(JavassistCompiler.java:119)
    at com.alibaba.dubbo.common.compiler.support.AbstractCompiler.compile(AbstractCompiler.java:59)
    at com.alibaba.dubbo.common.compiler.support.AdaptiveCompiler.compile(AdaptiveCompiler.java:46)
    at com.alibaba.dubbo.common.extension.ExtensionLoader.createAdaptiveExtensionClass(ExtensionLoader.java:739)
    at com.alibaba.dubbo.common.extension.ExtensionLoader.getAdaptiveExtensionClass(ExtensionLoader.java:732)
    at com.alibaba.dubbo.common.extension.ExtensionLoader.createAdaptiveExtension(ExtensionLoader.java:721)
    ... 32 more
Caused by: java.io.IOException: invalid constant type: 18
    at javassist.bytecode.ConstPool.readOne(ConstPool.java:1113)
    at javassist.bytecode.ConstPool.read(ConstPool.java:1056)
    at javassist.bytecode.ConstPool.<init>(ConstPool.java:150)
    at javassist.bytecode.ClassFile.read(ClassFile.java:765)
    at javassist.bytecode.ClassFile.<init>(ClassFile.java:109)
    at javassist.CtClassType.getClassFile2(CtClassType.java:191)
    ... 77 more
9月 21, 2019 1:43:41 上午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.gsyshop.service.ItemService': Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError

 定位错误原因如下:

Caused by: java.lang.IllegalStateException: Can not create adaptive extenstion interface com.alibaba.dubbo.rpc.Protocol, cause: java.io.IOException: invalid constant type: 18
Error creating bean with name 'com.gsyshop.service.ItemService': Instantiation of bean failed; 

2、项目环境描述:我是按照项目的讲解配置maven 其中主要的jar包括:spring4.2.4、mysql5.1.32、mybatis3.2.8、mybatis-spring-1.2.2.jar、dubbo-2.5.3.jar(此为修改后的正确版本dubbo-2.5.7.jar)、javassist-3.1.5-GA.jar(此为修改后的正确版本javassist-3.20.0-GA.jar)、zookeeper-3.4.7.jar、以及jdk12、tomcat7(使用maven的tomcat插件)

3、原因分析:这里感谢@Json-Huang 的回答:有可能是javassit版本有冲突或者用了低版本,可以检查下,如有多个版本删除低的或者升级高的版本。后来发现,dubbo2.5.3版本引用的javassist版本是javassist-3.1.5-GA.jar(在项目中maven的lib下右键选中dubbo的jar包,选择maven->Open POM 可以查看dubbo引用的javassist版本信息),猜测可能是javassist版本过低,所以想调高它的版本,但是又没有找到显式引用javassist的pom配置,只有在dubbo的pom里面写死的,所以尝试提升dubbo的版本为2.5.7,再次查看dubbo引用的javassist版本默认为3.20了,重新启动service工程成功,在启动web工程也成功,可以正常访问!(但是还不太明确为什么dubbo2.5.3和javassist3.1.5会报错上述错误,以后查找到原因再来补充此处。)

4、注意:

(1)有可能你修改pom的dubbo为2.5.7版本了,但是重新加载项目时出现了还是旧版本,如果你把本地旧版本的dubbojar包删除了,运行工程他会自动下载旧版本的dubbo,这说明你之前已经Run as->Maven Install了,你要把maven仓库com包下的之前的工程(可能有service、dao、web...的jar包或者war包)删除,重新对工程进行Run as->Maven Install,会在com下重新生成,再次运行即可解决。

(2)因为我的zookeeper时部署在Linux服务器中,启动service工程时可能报错:

[ERROR] -ZkClientWrapper.java(53) - [DUBBO] Timeout! zookeeper server can not be connected in : 30000ms!, dubbo version: 2.5.7, current host: 127.0.0.1

我的原因是Linux的防火墙没有关闭导致的,这也是其中一个常见的错误原因,解决方式,关闭防火墙,在Linux运行以下语句:

防火墙状态
#systemctl status firewalld.service
防火墙关闭
#systemctl stop firewalld.service 
永久关闭开机启动
#systemctl disable firewalld.service

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值