欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ssm整合无法注入dao层

程序员文章站 2022-05-06 20:37:42
...

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>