今天在整合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即可.(首字母小寫)