浏览器一直500报错 HTTP Status 500 - Servlet.init() for servlet DispatcherServlet threw exception
Error creating bean with name 'demoController': Unsatisfied dependency expressed through field 'demoService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.hjbnb.service.DemoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
@Controller
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping(value = "/show")
public String show() throws FileNotFoundException, MyException {
System.out.println("show running......");
//demoService.show1();
//demoService.show2();
//demoService.show3();
//demoService.show4();
demoService.show5();
return "index";
}
}
看看DemoService被扫描到了吗?如果没有被spring容器进行管理。是不能用@Autowired注解的。
找不到demoService bena 看下 demoServiceImpl上有没有加@Service注解 以及有没有被@ComponentScan扫描到
springboot的话,启动类丢最外层,也别加什么ComponentScan,基本启动就能被扫描到了
出现这个异常往往是没有找到Bean的实例而导致。
解决:首先查询service层是否全部添加@Service注解;如果没有问题就查看每一层之间的对应关系,Mapper层和xml之间是否能对应;查询xml中配置是否正确,字段是否映射完全;最后查看是否全部被启动类上的注解路径全部扫描到,查看创建的包名称是否有错误!