程序启动报错,调试如下

问题遇到的现象和发生背景

使用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的值有问题

img


定义bean时bookInfoMapper属性是一个接口,排查接口是否正常注入
1。接口名没有问题
2.检查配置的属性值
测试数据库连接没有问题
接口注入相关配置

img


mybatis核心文件相关配置

img


接下来应该怎么排查

不如把完整的报错贴出来

Control Service 层注解是否都正确呢

img

img

你应该贴 controller和service的代码

junit报错原因,beanid拼写错误
junit中可以正常获取id为bookInfoServiceImpl 的Bean

 @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.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)}
检查contreller和service层均开启了包扫描,注解使用正确

把这个加个“/”试试

img

或者把这个改为 实现类 BookInfoServiceImpl bookInfoServiceImpl

img

映射配置看看是否需要加上类型get,post,delete等

idea重新编译一下:build--->rebuild
无用的话,试试Flle -> invalidate caches/restart
invalidate and restart

@Service("bookInfoService") //这里指定bean id试试呢
public class BookInfoServiceImpl implements BookInfoService {