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 项目目录结构

解决方案

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

时间: 2024-07-29 16:35:49

spring/mybatis整合,junit测试报BeanCreationException的相关文章

struts spring mybatis 整合出问题

问题描述 struts spring mybatis 整合出问题 报错: IOException parsing XML document from ServletContext resource [/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/applicationContext.xml] 配置文件appl

mybatis spring 整合 junit测试。 事务不起作用,不提交。删除无效???

问题描述 mybatis spring 整合 junit测试. 事务不起作用,不提交.删除无效??? applicationContext.xml 中的数据库和 sessionFactory以及事务配置 Xml代码 <bean id="bssDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connec

spring配置多数据源t junit测试报错

问题描述 spring配置多数据源t junit测试报错 Error creating bean with name 'seentao.workflow.engine.EngineTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: publi

spring4.14+hibernate4.3整合,web运行正常,junit测试报错;

问题描述 spring4.14+hibernate4.3整合,web运行正常,junit测试报错: 之前web启动的时候也报这个错误,然后在web.xml里面加了这个filter,就好了. 可是现在用junit测试,没办法读取web.xml里面的配置,所以还报了这个错, 别问我为什么要junit测试,就是想知道 junit测试的时候这种情况怎么解决. 下面附代码 SpringOpenSessionInViewFilterorg.springframework.orm.hibernate4.sup

java spring junit-java junit测试报错找不到自己定义的xml文件 跪求各位高手帮忙解决一下

问题描述 java junit测试报错找不到自己定义的xml文件 跪求各位高手帮忙解决一下 eclipse下junit测试时遇到如下错误,错误内容如下: at jp.co.dgic.eclipse.jdt.internal.junit.runner.DJUnitRunner.main(DJUnitRunner.java:49) caused by:org.springframework.beans.factory. BeanDefinitionStoreException:IoException

test-spring4+mybatis3整合 junit测试的问题

问题描述 spring4+mybatis3整合 junit测试的问题 applicationContext.xml <context:component-scan base-package=""com""/> <bean id=""propertyConfigurer"" class=""org.springframework.beans.factory.config.PropertyPl

junit测试报错,求大神救命,控制台输出如下,

问题描述 junit测试报错,求大神救命,控制台输出如下, 0 2015-07-09 09:04:52182 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - #org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:316)# -

studio-Android Studio中使用JUnit测试报错(ClassNotFoundException)

问题描述 Android Studio中使用JUnit测试报错(ClassNotFoundException) 在Android Studio中编写测试类public class MyAppTest extends ActivityInstrumentationTestCase2报错: Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter Caused by: java

求一份Struts2+Spring+mybatis整合的增删改查及分页和多条件查询的源代码

问题描述 求一份Struts2+Spring+mybatis整合的增删改查及分页和多条件查询的源代码 小白求一份Struts2+Spring+mybatis整合的增删改查及分页和多条件查询的源代码,用jQuery实现jsp页面 解决方案 SpringMVC+Spring+mybatis是否需要呢?笔者之前写了一些相关的文章,也有源代码,你可以参考下:http://blog.csdn.net/evankaka/article/details/49452201http://blog.csdn.net