【异常篇-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 
{
	.......
}

猜你喜欢

转载自blog.csdn.net/choushi300/article/details/79411367