首页 > 解决方案 > 如何在 SSM 中使用 @Autowired?它总是找不到映射器

问题描述

我正在用 SSM (java) 编写一个系统,但是当我用“maven build”调试它时,它总是报告这样的错误:

org.springframework.beans.factory.BeanCreationException:创建名为“categoryController”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 cn.neu.yealon.service.CategoryService cn.neu.yealon.controller.CategoryController.categoryService;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“categoryServiceImpl”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 cn.neu.yealon.mapper.TbCategoryMapper cn.neu.yealon.service.impl.CategoryServiceImpl.categoryMapper; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type of [cn.neu.yealon.mapper.TbCategoryMapper] found for dependency: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:@org.springframework.beans.factory.annotation.Autowired(required=true)} at .........

从报告的错误来看,我认为错误在映射器层,与 CategoryController.java 和 CategoryService.java 无关。我更正了一些 .java 文件或 .xml 文件,但错误从未改变。该文件将显示如下:

工作区类别在这里

类别控制器.java:

......
......
/**
 * controller
 * 
 * @author Yealon
 *
 */
@RestController
@RequestMapping("/category")
public class CategoryController
{

    @Autowired
    private CategoryService categoryService;

    /**
     * find all data
     * 
     * @return
     */
    @RequestMapping("/findAll")
    public List<TbCategory> findAll()
    {
        return categoryService.findAll();
    }
......
......
}

类别服务.java:

......
......
/**
 * service interface
 * 
 * @author Yealon
 *
 */
public interface CategoryService
{

    /**
     * 返回全部列表
     * 
     * @return
     */
    public List<TbCategory> findAll();
......
......
}

CategoryServiceImpl.java:

......
......
/**
 * 
 * 
 * @author Yealon
 *
 */
@Service
public class CategoryServiceImpl implements CategoryService
{

    @Autowired
    private TbCategoryMapper categoryMapper;

    /**
     * 查询全部
     */
    @Override
    public List<TbCategory> findAll()
    {
        return categoryMapper.selectByExample(null);
    }
......
......
}

TbCategoryMapper.java

package cn.neu.yealon.mapper;

import cn.neu.yealon.pojo.TbCategory;
import cn.neu.yealon.pojo.TbCategoryExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface TbCategoryMapper
{
    int countByExample(TbCategoryExample example);

    int deleteByExample(TbCategoryExample example);

......
......
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    id="WebApp_ID" version="4.0" metadata-complete="true">
    <display-name>evalSys</display-name>
    <welcome-file-list>
        <welcome-file>page/login.html</welcome-file>
    </welcome-file-list>



    <!-- 配置spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext-*.xml</param-value>
    </context-param>

    <!-- 配置监听器加载spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- 配置过滤器,解决post的乱码问题 -->
    <filter>
        <filter-name>encoding</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>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 配置SpringMVC -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <!-- 配置springmvc什么时候启动,参数必须为整数 -->
        <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
        <!-- 如果小于0,则在第一次请求进来的时候启动 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 所有的请求都进入springMVC -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


    <!-- spring security -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-security.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

springmvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder
        location="classpath:config/*.properties" />
    <context:component-scan
        base-package="cn.neu.yealon.*" />
    <mvc:annotation-driven>
        <mvc:message-converters
            register-defaults="true">
            <bean
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes"
                    value="application/json" />
                <property name="features">
                    <value>WriteMapNullValue</value>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <context:annotation-config />  
</beans>

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 加载配置文件 -->
    <context:property-placeholder location="classpath*:properties/*.properties" />



    <!-- 配置 数据源 -->
    <bean id="dataSource"
        class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        >
        <!-- 设置MyBatis核心配置文件 -->
        <property name="configLocation"
            value="classpath:mybatis/SqlMapConfig.xml" />
        <!-- 设置数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置Mapper扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 设置Mapper扫描包 -->
        <property name="basePackage" value="cn.neu.yealon.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
</beans>

SqlMapConfig.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>
    <!-- 别名 -->
    <typeAliases>
        <package name="cn.neu.yealon.pojo" />
    </typeAliases>
    <plugins>
        <!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库 -->
            <property name="dialect" value="mysql" />
        </plugin>
    </plugins>
</configuration>

我认为问题可能出在配置文件中,可能是一个小问题,对于熟练的人来说可能很容易,但意义重大。上面提供的信息有些冗余......谢谢你帮助我!

标签: ssm

解决方案


在 web.xml 中,“contextConfigLocation”出现了两次,这是不允许的。

这是错误的:

<!-- spring -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- spring security -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-security.xml</param-value>
</context-param>
...
...

这是正确的:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:spring/applicationContext-*.xml,
        classpath:spring/spring-security.xml
    </param-value>
</context-param>

推荐阅读