ssm整合无法注入dao层

错误信息
spring整合mybatis,在部署项目时,一直报错:dao无法注入ioc容器

Error creating bean with name ‘accountController’: Unsatisfied dependency expressed through field ‘accountService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘accountService’: Unsatisfied dependency expressed through field ‘accountDao’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.whut.dao.IAccountDao’ available: expected at least 1 bean which qualifies as autowire candidate.

spring的配置文件已经配置了sqlSessionFactory,注解扫描也已经开启,注解也加上了。查了一些资料都是说注解未开启

<!-- 配置SqlSession的工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 配置扫描dao的包 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.whut.dao"/>
    </bean>

最后发现原因在于忘了加载ApplicationContext.xml!,在web.xml配置spring监听器,并配置加载的文件,如下:

  <!--配置spring的监听器,默认只加载WEB-INFO目录下的文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--设置配置文件路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext.xml</param-value>
  </context-param>
发布了5 篇原创文章 · 获赞 5 · 访问量 113

猜你喜欢

转载自blog.csdn.net/qq_41840761/article/details/104057529