qq_29215513
xian云
2018-06-13 12:01
采纳率: 100%
浏览 1.1k
已采纳

ssm配置,总是提示说缺少bean,检查了VideoMapper.xml文件,其中并没有重复的id

我用mybatis生成了mapper文件,dao文件,还有bean文件。在调用mapper的时候报错找不到bean,我检查配置文件感觉没有问题,求大佬指点迷津。
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
System.out.println(ctx);
这样是可以获取到的,但是通过ctx获取bean的时候获取不到。
文件的组织结构如下:

图片说明

applicationContext.xml :

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->
    <!-- 数据源,事物控制...... -->
    <context:component-scan base-package="com.teachers.*">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

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

    <bean id = "pooledDataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value = "${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value = "${jdbc.driverClass}"></property>
        <property name="user" value = "${jdbc.user}"></property>
        <property name="password" value = "${jdbc.password}"></property>
    </bean>

<!-- ============================================================================================== --> 

    <!-- 配置和myBatis的整合 -->
    <bean id= "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定myBatis全局配置文件的位置 -->
        <property name="configLocation" value = "classpath:mybatis-config.xml"></property>
        <property name="dataSource" ref = "pooledDataSource"></property>
        <!-- 指定myBatis mapper文件的位置 -->
        <property name="mapperLocations" value = "classpath:com.teachers.mappers.*"></property>
    </bean>

    <!-- 配置扫描器,将myBatis接口的实现加入到IOC容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描所有的Dao接口的实现,加入到IOC容器中 -->
        <property name="basePackage" value = "classpath:com.teachers.dao"></property>
    </bean>

<!-- ================================================================================================== -->

    <!-- 事物控制的配置 -->
    <bean id = "transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <!-- 控制住数据源 -->
        <property name="dataSource" ref="pooledDataSource"></property>
    </bean>
    <!-- 开启基于注解的事务,使用xml配置形式的事务(必要主要的都是配置式) -->   
    <aop:config>
        <!-- 切入点表达式 -->
        <aop:pointcut expression="execution(* com.teachers.service..*(..))" id="txPoint"/>
        <!-- 配置事务增强 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>

    <!-- 配置事务增强,事务如何切入 -->
    <tx:advice id = "txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 所有方法都是事务方法 -->
            <tx:method name="*"/>

            <!-- 以get开始的所有方法 -->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!-- spring配置文件的核心关注点 
        1.数据源
        2.与myBatis的整合
        3.事务控制
    -->
</beans>

Junit测试代码:

package com.teachers.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.teachers.dao.VideoMapper;

/**
 * 测试Dao层的工作
 * @author Xian云
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:/applicationContext.xml"})
public class mapperTest {

    /**
     * 测试Video
     */
    @Autowired(required=true)
    VideoMapper video;

    @Test
    public void testCRUD() {
        System.out.println(video);
    }
}

错误信息:

 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.teachers.test.mapperTest': Unsatisfied dependency expressed through field 'video'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.teachers.dao.VideoMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:386)
    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:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.Iterator.forEachRemaining(Unknown Source)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.forEach(Unknown Source)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:83)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:74)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.teachers.dao.VideoMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

    ... 46 more


mybatis-config.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
  <configuration>
    <settings>
        <setting name = "mapUnderscoreToCamelCase" value = "true"/>
    </settings>
    <typeAliases>
        <package name="com.teachers.beans"/>
    </typeAliases>
</configuration> 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • qq_39537939
    看不懂的人 2018-06-13 14:02
    已采纳

    property name="basePackage" value = "com.teachers.dao" /property

    点赞 评论
  • qq_37165235
    浅歌余生 2018-06-13 12:42

    你的service层都有加注解吗?

    点赞 评论
  • qq_29215513
    xian云 2018-06-13 13:04

    web.xml 文件 :

    
     <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
        <!-- 配置spring配置文件的名称和位置 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classPath*:applicationContext.xml</param-value>
        </context-param>
    
        <!-- 启动IOC容器的ServletContextListener -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- 配置springMVC 前端控制器 -->
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:dispatcherServlet-servlet.xml</param-value>
            </init-param>
            <!--用来标记是否在项目启动时就加在此Servlet,0或正数表示容器在应用启动时就加载这个Servlet, 当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载.正数值越小启动优先值越高 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <!-- 拦截所有请求,千万注意是(/)而不是(/*) -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceRequestEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
            <init-param>
                <param-name>forceResponseEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    
        <filter>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>
    
    点赞 评论
  • qq_40422594
    qq_40422594 2018-06-14 13:14

    use demoysc32
    declare @myxml xml
    set @myxml='
    ''''''''''''
    ''''''''''''dongsheng
    ''''''''''''男
    ''''''''''''423545
    ''''''''''''
    ''''''''''''
    土豆

    123123


    choushuigou

    54543545

    '

    --获得所有student节点的数据,每一行显示一条people节点的数据

    /*select @myxml.query('.') as result from @myxml.nodes('/Peoples/people') as T(c) */
    --将这些数据显示为一个表格
    select T.c.value('(./Name)[1]','nvarchar(16)') as name,
    T.c.value('(./Sex)[1]','nvarchar(4)') as sex,
    T.c.value('(./QQ)[1]','nvarchar(16)') as QQ
    from @myxml.nodes('/Peoples/people') as T(c)

    
    
    点赞 评论
  • qq_40422594
    qq_40422594 2018-06-14 13:15
     use demoysc32 
    declare @myxml xml
    set @myxml='<Peoples>
                    <people>
                        <Name>dongsheng</Name>
                        <Sex>男</Sex>
                        <QQ>423545</QQ>
                    </people>
                    <people>
                        <Name>土豆</Name>
                        <Sex>男</Sex>
                        <QQ>123123</QQ>
                    </people>
                    <people>
                        <Name>choushuigou</Name>
                        <Sex>女</Sex>
                        <QQ>54543545</QQ>
                    </people>
                </Peoples>'
    
    --获得所有student节点的数据,每一行显示一条people节点的数据      
    /*select @myxml.query('.')  as result from @myxml.nodes('/Peoples/people') as  T(c) */
    --将这些数据显示为一个表格
    select  T.c.value('(./Name)[1]','nvarchar(16)') as name,
            T.c.value('(./Sex)[1]','nvarchar(4)') as sex,
            T.c.value('(./QQ)[1]','nvarchar(16)') as QQ
            from @myxml.nodes('/Peoples/people') as T(c)
    
    点赞 评论

相关推荐