我正在使用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:没有找到类型为[cn.neu.yealon.mapper.TbCategoryMapper]的合格Bean作为依赖项:至少应有1个有资格作为此依赖项的自动装配候选的bean。依赖注释:@ org.springframework.beans.factory.annotation.Autowired(required = true)} 在.........
从报告的错误中,我认为该错误位于映射器层中,与CategoryController.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();
......
......
}
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>
我认为问题可能出在配置文件中,可能只是一个小问题,对熟练的人来说可能很容易,但这具有重要意义。 上面提供的信息有些多余……谢谢您的帮助!
答案 0 :(得分:0)
在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>