No qualifying bean of type [com.bin.dao.SeckillDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, na

来源:4-8 DAO层单元测试编码和问题排查(上)

y1839

2016-09-24 18:04

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd  ">


<!-- 配置整合mybtais过程 -->

<!-- 1:配置数据库相关参数 -->

<context:property-placeholder location="classpath:jdbc.properties" />


<!-- 2:数据库的连接池 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<!-- 配置连接池属性 -->

<property name="driverClass" value="${driver}" />

<property name="jdbcUrl" value="${url}" />

<property name="user" value="${username}" />

<property name="password" value="${password}" />


<!-- c3p0连接池的私有属性 -->

<property name="maxPoolSize" value="30" />

<property name="minPoolSize" value="10" />

<!-- 关闭连接后不自动commit -->

<property name="autoCommitOnClose" value="false" />

<!-- 获取连接超时时间 -->

<property name="checkoutTimeout" value="1000" />

<!-- 当获取连接失败重试次数 -->

<property name="acquireRetryAttempts" value="2" />

</bean>


<!-- 3:配置SqlSessionFactory对象 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 注入数据库连接池 -->

<property name="dataSource" ref="dataSource" />

<!-- 配置Mybatis全局配置文件:mybatis-config.xml -->

<property name="configLocation" value="classpath:mybatis-config.xml" />

<!-- 扫描entity包 使用别名 -->

<property name="typeAliasesPackage" value="com.bin.entity" />

<!-- 扫描sql配置文件:mapper需要的xml文件 -->

<property name="mapperLocations" value="classpath:mapper/*.xml" />

</bean>


<!-- 4:配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!-- 注入sqlSessionFactory -->

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

<!-- 给出需要扫描Dao接口包 -->

<property name="basePackage" value="com.bin.dao" />

</bean>


</beans>

有人遇到跟我一样的错误码?


九月 24, 2016 6:03:48 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames

INFO: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]

九月 24, 2016 6:03:48 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners

INFO: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@152d950, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@391b0c, org.springframework.test.context.support.DirtiesContextTestExecutionListener@11eb50b, org.springframework.test.context.transaction.TransactionalTestExecutionListener@10014f0, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5e9e34]

九月 24, 2016 6:03:48 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.GenericApplicationContext@1c2f501: startup date [Sat Sep 24 18:03:48 CST 2016]; root of context hierarchy

九月 24, 2016 6:03:48 下午 org.springframework.test.context.TestContextManager prepareTestInstance

SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@391b0c] to prepare test instance [com.bin.dao.SeckillDaoTest@538773]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.bin.dao.SeckillDaoTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bin.dao.SeckillDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}

at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)

at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)

at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)

at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)

at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bin.dao.SeckillDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}

at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)

at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:457)

at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:435)

at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:559)

at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)

at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)

at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:305)

... 26 more

困惑了我一天一夜了、搞不定。

写回答 关注

4回答

  • qq_就安静_0
    2016-09-25 13:50:12
    已采纳

    spring和mybatis没整合好

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <property name="configLocation" value="classpath:mybatis-config.xml"/>
       <property name="typeAliasesPackage" value="org.seckill.entity"/>
       <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
       <property name="basePackage" value="org.seckill.dao"/>
    </bean>

  • 慕仙3095307
    2018-03-05 10:59:41

    注意mybatis的配置, 看看dao层的路径是不是正确的

    <!-- 配置 扫描器: dao层路径, 包下所有的映射接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
       <property name="basePackage" value="com.userLogin.dao"/>
    </bean>

  • web工作者
    2017-02-27 22:15:29

    已经解决了,是路径的问题。   <property name="basePackage" value="org.seckill.dao"/> 这个没错,但是spring-dao.xml 文件要放在跟目录下

  • web工作者
    2017-02-27 21:53:54

    解决了吗? 我也遇到这问题

Java高并发秒杀API之业务分析与DAO层

Java实现高并发秒杀API的第一门课,还等什么,赶快来加入吧

87424 学习 · 496 问题

查看课程

相似问题