spring bean没有找到对应的类?

错误是

严重 [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.mybatis.spring.SqlSessionFactoryBean] for bean with name 'sqlSessionFactory' defined in class path resource [resources/ApplicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean

我试了在java中可以找到

org.mybatis.spring.SqlSessionFactoryBean

这个类

如何解决?

具体的spring配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    ">
    <context:component-scan base-package="cn.laiCorp.dolMusic.*"></context:component-scan>
    <context:annotation-config></context:annotation-config>
<!--    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>-->
<!--    <tx:annotation-driven></tx:annotation-driven>-->


    <context:property-placeholder location="classpath:/resources/jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="user" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:/resources/sqlMappingConfig.xml"></property>
        <property name="mapperLocations" value="classpath:/resources/mappers/*.xml"></property>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.laiCorp.dolMusic.Dao"></property>
    </bean>
</beans>
阅读 3.2k
3 个回答

应该是包有冲突 换了个版本的包就好了

<?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-4.1.xsd"
    default-lazy-init="false">
    <description>MyBatis配置</description>
    
    <!-- SessionFactory配置 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描entity目录, 省掉mybatis-config.xml里的手工配置 -->
        <property value="**.entity" name="typeAliasesPackage" />
        <property value="org.springframework.util.LinkedCaseInsensitiveMap" name="typeAliases" />
        <property value="classpath:mybatis-config.xml" name="configLocation" />
        <!-- 显式指定Mapper文件位置 -->
        <property value="classpath:${mybatis.mapper_location}" name="mapperLocations" />
    </bean>

    <!-- 扫描basePackage下所有 接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property value="**.dao" name="basePackage" />
        <property value="sqlSessionFactory" name="sqlSessionFactoryBeanName" />

    </bean>
</beans>
我试了在java中可以找到,org.mybatis.spring.SqlSessionFactoryBean

你怎么试的呢,在 IDE 里搜?搜得到不能代表运行时也能找到。比如在多模块的项目里,你搜的到只能说明当前 project 的某个 module 里有这个类,不能代表你运行(或依赖)的这个 module 里有个类……

检查下你运行的这个 module 里有没有呢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题