今天在整合spring和mybatis两个框架时,发现了一个很奇怪的问题:
dao层中的接口名会影响在spring中注入时的属性的引用类型的名字.
举个例子:
假如dao层中的接口名为IStudentDao,映射文件名为IStudentDao.xml ,此时在beans.xml(spring的配置文件)文件中为service注入dao时,没有影响,程序可以正常跑起来.
各个截图如下:
图一. 工程目录截图
图二. spring配置文件中注入dao
发现这时程序是没有问题的,可以跑起来.但是若只稍微的改变一下,只将IStudentDao接口改为StudentDao,然后将配置文件中需要修改的修改掉,这时在上图中的ref属性若写为StudentDao,会报错抛出异常:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'services' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'StudentDao' while setting bean property 'dao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'StudentDao' is defined
解决方法是将ref属性改为 studentDao即可.(首字母小写)