为什么显示我的bean没注入

来源:2-3 编写第一个Spring Boot应用

恒di

2020-11-28

报错信息:
org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@737996a0] to prepare test instance [com.deng.shop.dao.AreaDaoTest@20bd8be5]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.deng.shop.dao.AreaDaoTest’: Unsatisfied dependency expressed through field ‘areaDao’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.deng.shop.dao.AreaDao’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
spring-dao.xml文件有<context:component-scan basepackage=“com.deng.shop.dao”/>,
AreaDao类在package com.deng.shop.dao下,AreaDao接口添加了@Repository(“areaDao”)注解,
AreaDaoTest类代码:
public class AreaDaoTest extends BaseTest {

@Autowired
 private AreaDao areaDao;

@Test
public void testQueryArea(){
    List<Area> areaList = areaDao.queryArea();
    Assert.assertEquals(2,areaList.size());
}

}
为什么会注入不了AreaDao的bean呢。

写回答

2回答

大目

2020-12-05

//img.mukewang.com/szimg/5fcb9e4d090f0faa22701006.jpg

mybatis的接口扫描是用这个配置决定的,不是由上面的component-scan决定的。

你之前配置不对,改成如图即可(你的DAO所在的包)。

0
0

大目

2020-11-29

你的DAO是mybatis写的吗?

如果是的话,mybatis的bean用<context:component-scan basepackage=“com.deng.shop.dao”/> 管不了。

参考下我的整合吧:

https://gitee.com/itmuch/platform/blob/master/platform-parent/platform-war/src/main/resources/config/beans-mybatis.xml


这个应该还是框架整合的问题。




0
3
恒di
回复
大目
代码在这:https://github.com/dzh6386/university-shop1,谢谢大目老师
2020-12-03
共3条回复

Spring Cloud Alibaba微服务从入门到进阶

面向未来微服务:熟练掌握Spring Cloud Alibaba

3085 学习 · 1324 问题

查看课程