org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xxx'is defined

来源:互联网 发布:mt4行情软件 编辑:程序博客网 时间:2024/05/22 08:01

spring异常:bean没有被定义,也就是说,spring容器中没有这个bean

【org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'tPokerPlayingInRoomService' is defined】



通常解决思路:


1.看配置文件是否配置该bean或者是否配置开启注解扫描、配置要扫描的包

    (注解方式)applicationContext.xml配置文件中:如以下两个配置

 <context:annotation-config/>  <context:component-scan base-package="com.mym.*"></context:component-scan>
    (bean定义方式)applicationContext.xml配置文件中:如下配置

<bean id="a" class="com.mym.fun.service.A">   <!--一堆属性注入--></bean>


2.该bean是否存在,bean上是否有spring组件注解(如:@Component, @Repository, @Service, @Controller, etc)、bean是否处于可扫描的包下



这两种是解决一般由于疏忽导致的问题。


还一种情况,使用注解的方式进行声明bean。这两种检查都做了,还是出现这样的错误。


进一步解决

那么检查以下你的bean名,也就是类名,是否有双大写字母开头,如:

@Servicepublic class TPokerPlayingService {    。。。}
像这种情况的声明,如果你使用  tPokerPlayingService  这种一般习惯的  驼峰命名  对象方式使用这个bean,就会出现找不到这个bean的错误。因为spring容器的驼峰命名使得存在spring容器中的bean名称是 tpokerPlayingService 。

那么对于这样的问题,解决方式:

1.改类名

2.显示声明这个bean名称如:

@Service("tPokerPlayingService")public class TPokerPlayingService {    。。。}



阅读全文
0 0
原创粉丝点击