星淘商城day4快结束的时候因故中断了两周,返校继续跟进学习时发现多了如下错误,在启动taotao-content的时候报错如下:
ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.taotao.content.service.ContentService': Cannot resolve reference to bean 'contentServiceImpl' while setting bean property 'ref'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'contentServiceImpl' is defined
在百度定位问题后,发现该类型报错的意思是spring容器注入错误,出现该问题的原因集中为两类。
一:注解
- 在实现类中漏加注解。例如,dao层 @Repository ,或者service层 @Service,web层 @Controller
- 检查后确认注解已添加,但实际上注解的导包错误。例如,service层 @Service导包时不小心选了非spring框架里的包
二:配置文件出错
若注解没错,则继续检查配置文件,重点检查配置的路径,大小写字母为代表的名称写错等问题。常见注解错误有:
- spring配置文件中接口或扫描的包名写错
- dubbo的服务提供者的配置文件中接口错误,导致找不到接口
- dubbo的服务提供者的配置文件中ref引用名称出错
我的错误原因:
经过我反反复复仔仔细细的检查,发现我的@Service导入的包错误,实际的导包为:
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
注意第二行,要不是本人火眼金睛这个错误我还真是没有想到,说明在写@Service导包疏忽了,以为默认的包就是spring框架的包,结果导入了dubbo下的一个Service包,纠正后正确的导包应该为:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
注意第二行的前后差别。