1. 程式人生 > >【異常篇-spring】org.springframework.beans.factory.BeanNotOfRequiredTypeException

【異常篇-spring】org.springframework.beans.factory.BeanNotOfRequiredTypeException

報錯異常:

2018-03-01 13:44:18.091 [localhost-startStop-1] ERROR o.a.c.c.C.[.[.[/zsedu-score-query-webserver-webapp] -- StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scoreQueryController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'studentService' must be of type [com.nenglong.rrt.webserver.service.exam.StudentService], but was actually of type [com.nenglong.rrt.webserver.service.user.impl.StudentServiceImpl$$EnhancerBySpringCGLIB$$9f0ce91f]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(Common...............................
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'studentService' must be of type [com.xxx.webserver.service.exam.StudentService], but was actually of type [com.xxx.webserver.service.user.impl.StudentServiceImpl$$EnhancerBySpringCGLIB$$9f0ce91f]

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:364)

..............................

解決方法:

@Resource
private StudentService<Student, StudentForm> studentService;

改為

@Resource(name="studentService2")
	private StudentService<Student, StudentForm> studentService;

原理:預設按照名稱進行裝配,名稱可以通過name屬性進行指定,如果沒有指定name屬性,當註解寫在欄位上時,預設取欄位名進行按照名稱查詢。com.xxx.webserver.service.user包下也有一個studentService介面,取bean時未指定名稱導致到的..service.user包下的介面型別不匹配。

備註:...user包下的studentService實現類程式碼

@Service("studentService")
public class StudentServiceImpl implements studentService 
{
	.......
}