spring boot报错如下:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘helloController’: Unsatisfied dependency expressed through field ‘msgService’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.example.dep.service.MsgService’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
问题原因,在service属性注入时没有找到bean,上代码
service的接口
public interface MsgService {
public List<Msg> findAll();
}
service的实体类
@Service("msgService")
public class MsgServiceImpl {
@Autowired
private MsgMapper msgMapper;
public List<Msg> findAll(){
List<Msg> msgList=msgMapper.findAll();
System.out.println("---------------");
return msgList;
}
}
原因,实体类没有实现接口,所以属性注入失败