如何在SSM中使用@Autow?它总是找不到映射器

  • 本文关键字:找不到 映射 @Autow SSM ssm
  • 更新时间 :
  • 英文 :


我正在用SSM(Java(编码系统,但是当我使用" maven build"调试时,它总是报告这样的错误:

org.springframework.beans.factory.beancreationexception:错误创建使用名称" categoryController"的BEAN:自动依赖性的注入失败;嵌套的异常是org.springframework.beans.factory.factory.beancreationexception:无法自动化场:private cn.neu.yealon.service.category.category.category.categoryservice cn.neu.yealon.yealon.controller.categoryCategoryCategoryCategoryServoryServoryServoryServoryServoryServoryServoreServiceServiceServiceServiceService;嵌套的异常是org.springframework.beans.factory.factory.beancreationexception:错误创建使用名称'categorysServiceImpl'的bean:自动依赖的注入失败;嵌套的例外是org.springframework.beans.factory.factory.beancreationexception:无法自动化场:private cn.neu.yealon.mapper.tbcategorymapper cn.yeal.yealon.service.service.impl.impl.categorysemplecateoryserserveicempl.categateoryservoryservorymapperorymappermapper >>>>>>>;;;;;;;;嵌套的例外是org.springframework.beans.factory.nosuchbeandefinitionException:依赖性:依赖性的bean nosuchbeandefinitionException:nosuchbeandefinitionException:nosuchbeandefinitionException:nosuchbeandefinitionexception:nosuchbeandefinition exception:nosuchbeandefinity exception:nosuchbeandefinity exception。依赖项注释: @org.springframework.beans.factory.annotation.autowired(必需= true(} 在.........

从报告的错误中,我认为该错误在映射层中,它与类别controller.java和categoryservice.java的业务无关。我纠正了一些 .java文件或.xml文件,但是错误从未更改。该文件将显示如下:

工作区类别在这里

categorycontroller.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();
    }
......
......
}

categoryservice.java:

......
......
/**
 * service interface
 * 
 * @author Yealon
 *
 */
public interface CategoryService
{
    /**
     * 返回全部列表
     * 
     * @return
     */
    public List<TbCategory> findAll();
......
......
}

类别serviceimpl.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>

我认为问题可能是在配置文件中,并且可能很小,对于熟练的人来说可能很容易,但这很重要。上面提供的信息有些冗余……谢谢您的帮助!

在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>

最新更新