hft24dq
2013-07-04 16:11 阅读 286
已采纳

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条回答 默认 最新

相关推荐