NoSuchBeanDefinitionException: No bean named 'tDubboServiceImpl' is defined 的解决办法

问题概述

关于这儿问题是在微服务开发过程中遇到的,通过本地远程调用dubbo接口,用xml实例化bean时报 “ No bean named 'tDubboServiceImpl' is defined ”。

部分截图如下:

具体内容如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.huazai.b2c.aiyou.service.TDubboService': Cannot resolve reference to bean 'tDubboServiceImpl' while setting bean property 'ref'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'tDubboServiceImpl' is defined
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	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.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'tDubboServiceImpl' is defined
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
	... 24 more

解决办法

在一个程序接口中,有一个 Interface 接口,并且在服务提供者那边有个类 ServiceImpl 实现了这个接口,并且需要创建这个实现类的一个对象服务提供给消费者。在提供者方使用列如:


	<bean id="tDubboServiceImpl"
		class="com.huazai.b2c.aiyou.service.impl.TDubboServiceImpl" />

这句创建了被提供的那个 tDubboServiceImpl,再用 ref="tDubboServiceImpl" 指明你要提供的就是 bean id="tDubboServiceImpl" 的那个对象,在服务端用

	<dubbo:service
		interface="com.huazai.b2c.aiyou.service.TDubboService"
		ref="tDubboServiceImpl" />

来接收,使用的时候通过 Interface 这个共同的接口类型来引用,这样就能拿到 service 并且使用了

总的一句话:

这个问题在于没有那个服务提供的实现类的Bean的引用,没有它的对象,在指明 ref 时并不能对应一个确实存在的对象,这个 ref 指定引用是空的,所以才导致 “ Cannot resolve reference to bean 'tDubboServiceImpl' while setting bean property 'ref'; ”。

完整示例内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<!-- 配置service扫描包 -->
	<context:component-scan base-package="*" />
	<!-- 缺省的 tDubboServiceImpl -->
	<bean id="tDubboServiceImpl" class="classpath.TDubboServiceImpl" />

	<!-- 使用Dubbo发布服务 -->
	<dubbo:application name="aiyou-manager" />
	<dubbo:registry protocol="zookeeper"
		address="***.***.***.***:21801" />
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 声明需要暴露的服务端口 -->
	<dubbo:service interface="classpth.TDubboService"
		ref="tDubboServiceImpl" />

</beans>

 好了,关于 NoSuchBeanDefinitionException: No bean named 'tDubboServiceImpl' is defined 的解决办法 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者: 华    仔
联系作者: [email protected]
来        源: CSDN (Chinese Software Developer Network)
原        文: https://blog.csdn.net/Hello_World_QWP/article/details/90344499
版权声明: 本文为博主原创文章,请在转载时务必注明博文出处!
发布了318 篇原创文章 · 获赞 637 · 访问量 144万+

猜你喜欢

转载自blog.csdn.net/Hello_World_QWP/article/details/90344499
今日推荐