飞花一叶 2013-07-04 16:11
浏览 327
已采纳

spring/mybatis整合,junit测试报BeanCreationException

昨天想整合下springmvc和mybatis,用junit写了个测试,测试过程中,mybatis的mapper无法注入,报BeanCreationException,相关信息大家看下,帮小弟解决下

 

1 错误堆栈信息

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.hft.dal.mapper.AccountMapperTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.hft.dal.mapper.AccountMapper com.hft.dal.mapper.AccountMapperTest.accountMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.hft.dal.mapper.AccountMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

 

2 applicationContext.xml


    <context:component-scan base-package="com.hft" />
    <context:annotation-config />
    <tx:annotation-driven />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations">
            <list>
                <value>classpath*:com/hft/dal/mapper/*Mapper.xml</value>
            </list>
        </property>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hft.dal.mapper" />
        <property name="markerInterface" value="com.hft.dal.mapper.MarkerInterfaceMapper" />
    </bean>

 

3 AccountMapper类,MarkerInterfaceMapper是个空接口

public interface AccountMapper extends MarkerInterfaceMapper {
    public List<Account> getAllAccount();
    public void addAccount(Account account);
}

4 junittest类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
public class AccountMapperTest extends AbstractJUnit4SpringContextTests{
    @Autowired
    private AccountMapper accountMapper;
    @Test
    public void testGetAllAccount() {
        System.out.println(accountMapper.getAllAccount().size());
  }

 

5 所引入的的包



 
6 项目目录结构

 
 
 

  • 写回答

1条回答

  • zyn010101 2013-07-04 16:23
    关注

    AccountMapper 类没加注解,扫描不到,就注入失败了,[url]http://blog.csdn.net/ydwuli06/article/details/6993219[/url]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置