问题遇到的现象和发生背景
使用ssm搭建项目,tomcat启动程序报错,页面提示404
用代码块功能插入代码,请勿粘贴截图
@Controller
@RequestMapping("/bookinfo")
public class BookInfoController {
@Autowired
private BookInfoService bookInfoService;
//查询所有书籍
@RequestMapping("allBookInfo")
public String queryAll(Model model){
List<BookInfo> bookInfos = bookInfoService.queryAll();
model.addAttribute(bookInfos);
return "allBook";
}
}
@Service
public class BookInfoServiceImpl implements BookInfoService {
@Autowired
private BookInfoMapper bookInfoMapper;
@Override
public int addBookInfo(BookInfo bookInfo) {
return bookInfoMapper.addBookInfo(bookInfo);
}
@Override
public int deleteBookInfoById(int id) {
return bookInfoMapper.deleteBookInfoById(id);
}
@Override
public int updateBookInfo(BookInfo bookInfo) {
return bookInfoMapper.updateBookInfo(bookInfo);
}
@Override
public BookInfo queryForOne(int id) {
return bookInfoMapper.queryForOne(id);
}
@Override
public List<BookInfo> queryAll() {
return bookInfoMapper.queryAll();
}
}
运行结果及报错内容
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookInfoController': Unsatisfied dependency expressed through field 'bookInfoService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cy.service.BookInfoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=BookInfoServiceImpl)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cy.service.BookInfoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=BookInfoServiceImpl)}
我的解答思路和尝试过的方法
发现 NoSuchBeanDefinitionException
1、检查定义bean的id和getBean的id是否一致
2.检查是否开启包扫描。
以上两点均无问题
3。使用junit单元测试,获取Spring容器后,通过getBean获取对象
功能代码如下:
public class MyTest {
@Test
public void test(){
ApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
BookInfoService bookInfoServiceImpl = (BookInfoService) classPathXmlApplicationContext.getBean(
"BookInfoServiceImpl");
for ( BookInfo bookInfo:bookInfoServiceImpl.queryAll()) {
System.out.println(bookInfo);
}
}
}
报错如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookInfoServiceImpl' defined in class path resource [spring-service.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
分析:
定义Bean时,属性名为sqlSessionFactory' or 'sqlSessionTemplat的值有问题
定义bean时bookInfoMapper属性是一个接口,排查接口是否正常注入
1。接口名没有问题
2.检查配置的属性值
测试数据库连接没有问题
接口注入相关配置
mybatis核心文件相关配置
接下来应该怎么排查