I am coding a system with SSM (java), but while my debuging it with "maven build", it always report bug like this:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.neu.yealon.service.CategoryService cn.neu.yealon.controller.CategoryController.categoryService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.neu.yealon.mapper.TbCategoryMapper cn.neu.yealon.service.impl.CategoryServiceImpl.categoryMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.neu.yealon.mapper.TbCategoryMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)} at .........

From the bug reported, I think the bug is in the mapper layer, it is none of business of CategoryController.java and CategoryService.java. I correct some .java file or .xml file, but the bug never changed. The file will be displayed as follows:

workspace category is here

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

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>

I think the problem may be in a configuration file, and may be a small point, and may be easy for a skillful people,but it is of great significance. The infomation provided above is of some redundance......thank you for helping me!

1 Answers

0
YealonK On

In web.xml, "contextConfigLocation" appears twice, which is not allowed.

This is WRONG:

<!-- 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>
...
...

And this is Right:

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

Related Questions