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个回答

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

hft24dq
飞花一叶 按照你说的,重新梳理了一遍配置,包内都扫描到。今天报了一个新异常,http://www.iteye.com/problems/96374此贴结了吧先
接近 7 年之前 回复
zyn010101
zyn010101 <context:component-scan base-package="com.hft" /> 没有扫描到
接近 7 年之前 回复
hft24dq
飞花一叶 AccountMapper是MyBatis数据操作接口,按照mybaits的示例,是使用定义的 <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> 进行扫描的。 我仔细看了log,其中有这么一句话 DEBUG 2013-07-04 17:20:06,482org.springframework.test.context.support.AbstractGenericContextLoader: Loading ApplicationContext for locations [classpath*:applicationContext.xml]. DEBUG2013-07-0417:20:06,538org.springframework.beans.factory.support.AbstractBeanDefinitionReader: Loaded 0 bean definitions from location pattern [classpath*:applicationContext.xml] 好象说的是没有load到bean,难道是配置有问题?
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问