为什么显示我的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
mybatis的接口扫描是用这个配置决定的,不是由上面的component-scan决定的。
你之前配置不对,改成如图即可(你的DAO所在的包)。
00 -
大目
2020-11-29
你的DAO是mybatis写的吗?
如果是的话,mybatis的bean用<context:component-scan basepackage=“com.deng.shop.dao”/> 管不了。
参考下我的整合吧:
这个应该还是框架整合的问题。
032020-12-03
相似问题