spring 注入失败报错

【乐企-工具篇】生成发票左上角二维码 【代码】【乐企-业务篇】生成发票左上角二维码。 阅读详情

报错代码片段:

Error creating bean with name 'itemServiceImpl': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException:
 Could not autowire field: private com.taotao.search.mapper.ItemMapper com.taotao.search.service.ItemServiceImpl.itemMapper;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
 No qualifying bean of type [com.taotao.search.mapper.ItemMapper] 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)}

 网上的方法都试过了,注解加了,mapper.xml的namespace什么的也检查了好几遍,都找不到错。

因为项目里放了一个*.*.mapper包和*.*.dao包,一度以为这两个包可能有冲突,仔细想想应该也不是这个原因。

经过再三的检查,偶然发现applicationContext-dao的配置文件里没有扫描mapper的包。添上之后问题解决。

附上工程目录和配置文件:

 

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.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 数据库连接池 -->
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:properties/*.properties" />
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>
	<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 给出需要扫描Dao接口包 -->
		<property name="basePackage" value="com.taotao.dao,com.taotao.search.dao,com.taotao.search.mapper" />
	</bean>
</beans>

注意配置文件里的最后一个<bean>,里面配置上扫描dao包和mapper包

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.taotao.dao,com.taotao.search.dao,com.taotao.search.mapper" />
</bean>

applicationContent-service.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.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 包扫描器,扫描带@Service注解的类,此处加逗号分开,写成com.taotao.search.*会把controller扫描进来,引发父子容器问题 -->
	<context:component-scan base-package="com.taotao.search.service"></context:component-scan>
	<!-- 单机版solr客户端 , 在这配置完成后就可以直接让spring注入了 SolrServer是个抽象类,httpSolrServerhe clientSolrServer是其子类,一个是单机版,一个是集群版-->
	<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
 		<constructor-arg name="baseURL" value="http://144.34.134.37:8080/solr"></constructor-arg>
	</bean>
</beans>

注意配置文件里包扫描器中扫描service包。

<!-- 包扫描器,扫描带@Service注解的类,此处加逗号分开,写成com.taotao.search.*会把controller扫描进来,引发父子容器问题 -->
    <context:component-scan base-package="com.taotao.search.service"></context:component-scan>

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: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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 配置包扫描器 -->    
    <context:component-scan base-package="com.taotao.search.controller"></context:component-scan>
    <!-- 配置注解驱动 -->
    <mvc:annotation-driven/>
</beans>        

这个配置文件是配置controller包的扫描,因为父子容器的关系,controller包的扫描要和dao包、service包的扫描分开。

web.xml

最后是web.xml。配置spring扫描上面dao包、service包的配置文件。配置springmvc扫描上面controller包的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="taotao" version="2.5">
	<display-name>taotao-search</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	
	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 解决post乱码 -->
	<filter>
		<filter-name>CharacterEncodingFilter</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>
		<!-- <init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param> -->
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>*</url-pattern>
	</filter-mapping>


	<!-- springmvc的前端控制器 -->
	<servlet>
		<servlet-name>taotao-search</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>taotao-search</servlet-name>
		<url-pattern>/search/*</url-pattern>
	</servlet-mapping>
	<!-- log4j配置文件位置 -->
	<context-param>
	    <param-name>log4jConfigLocation</param-name>
	    <param-value>classpath:log4j.properties</param-value>
	</context-param>
	
	<!-- 利用spring来使用log4j -->
	<listener>
	    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
</web-app>

总结:

*dao.xml文件配置数据库连接层的包扫描。一般是*.*.dao或*.*.mapper,多个包时用“,”分隔。

*service.xml文件配置服务层的包的扫描。一般是*.*.service包,多个时用“,”分隔。

springmvc.xml文件配置视图层的包的扫描。一般是*.*.controller包。

springmvc.xml为什么不用*.controller命名?

可能是因为springmvc.xml属于springmvc容器,dao.xml和service.xml属于spring容器吧!

web.xml文件则配置spring和springmvc分别扫描哪些配置文件

 

课程1:认识Asp.net Core Web API 本文是《.Net Core从零学习搭建权限管理系统》教程专栏的导航站(点击链接,跳转到专栏主页,欢迎订阅,持续更新…)专栏介绍:以实战为线索,基于.Net 7 + REST + Vue、前后端分离,不依赖任何第三方框架,从零一步一步讲解权限管理系统搭建。专栏适用于人群:Web后端开发人员Web API是一个很广泛的概念,Asp.net Core Web API是.NET框架用于创建RESTful风格的项目、用于轻松构建可供多个客户端访问的HTTP服务。1、支持RestFul风格编程; 阅读详情

相关推荐

别再浪费主板上的PCIE插槽了!手把手教你用VL805芯片打造高速USB3.0扩展坞

本文详细介绍了如何利用VL805芯片将主板闲置的PCIE插槽改造为高性能USB3.0扩展坞。从芯片选型、电路设计到焊接工艺和系统调试,提供了一套完整的实战指南,帮助用户充分释放硬件潜能,实现高达5Gbps的传输速度。

weixin_27134495的博客 331

解决:Injection of autowired dependencies failed报错问题

解决如下报错: Error creating bean with name ‘categoryManageController’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not au...

YSJ_Dumpling的博客 3万+

从入门到精通:dirmap 高级 Web 目录扫描实战指南

本文提供了一份从环境搭建到高级配置的dirmap Web目录扫描实战指南。详细解析了工具安装、核心配置文件dirmap.conf的深度调优,包括递归扫描、四种扫描模式(字典、爆破、爬虫、Fuzz)的实战应用,并演示了分阶段资产发现的完整工作流程,帮助安全人员高效发现隐藏目录与敏感文件。

rnn9storyteller的博客 482

Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.Be

关于报错Error creating bean with name ‘adServiceImpl’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:随便粘一点代码吧 org.springframework.

王伟的博客 2287

Xilinx Aurora 8B/10B IP核详解和仿真

Xilinx Aurora 8b/10b IP核的详解,以及使用与仿真示例,对IP核的配置和如何仿真有非常详细的说明,除了对IP核官方文档的内容进行了解析以外,还对GTX引脚的选择等比较容易混淆和不好理解的地方进行了说明。最后对IP核的仿真运行进行了STEP BY STEP 的说明,对仿真的结果也进行了简要的说明。 该文档对Xilinx FPGA和ZYNQ SOC下 AURORA 8B/10B IP核的使用提供了一个快速上手的指导。

weixin_43354598的博客 1万+

SerDes硬件设计原理介绍

接收端:外部差分串行数据------线性均衡器----(CDR从数据中恢复出采样时钟)-----解串器(串行信号转并行信号)-------8B/10B的解码器或解扰器-----FIFO-----FPGA接受差分并行数据。发送端: FPGA发送差分并行数据----FIFO------8B/10B编码器或者扰码器-----串行器(并行信号转串行信号)-----均衡器(滤波)--------TX串行信号差分外发。串行器:8bit的并行数据----2bit的并行数据----1bits的串行数据。

weixin_42304393的博客 1112

Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory...

今天使用springmvc框架增加一个功能,写完 Controller, Service, ServiceImpl, Dao, Mapper.xml 发现报一个常见的错误,主要报错代码如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxServiceImpl': Injection of autowired dependencies failed; nested exc

fate_fate的博客 1420

减小伺服电机共振的方法(以松下伺服为例)

嘿 ! 我是【小黄】 , 我们又见面啦 !!! 毕竟技术只有大家交流才会进步嘛 ! 这博客是对自己学习的一点点记录和总结 ,如果您对自动控制感兴趣的话 ,可以关注一下我的动态 , 我们一起学习呀 ! 我们都还年轻 ,我们还有无限可能 。 YES ! 减小伺服电机共振的方法(以松下伺服为例)1. 准备工作:1.1.1 如何推定共振点:1.1.2 测定实操:1.1.3 总结:2. 下期预告: 在我们调试完伺服参数以后机器人运动到某个特定位置会产生振动或异响,我们要做的就是消除这个异响保..

在下小黄的博客 1万+

【ERROR】No qualifying bean of type ‘com.alibaba.druid.pool.DruidDataSource‘ available

No qualifying bean of type 'com.alibaba.druid.pool.DruidDataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

weixin_44624036的博客 1627

【四足机器人项目实战】波士顿spot四足机器人机工程设计及实现分析

主要介绍了波士顿spot机器人的发展历史、四足机器人的硬件系统及电机执行器组成包括硬件系统、关节电机执行器,机载传感器机处理器及电气连接等。概述了四足机器人规划控制、感知、定位及局部地图的相关算法方向。

qq_35635374的博客 5003

华为od 面试八股文_前端_02_含答案

华为od 面试八股文_前端_02_含答案

MISAYAONE的博客 2002

1分钟解决:SpringBoot启动报错Injection of autowired dependencies failed; Could not resolve placeholder: 问题

问题现象: SpringBoot项目读取配置文件中参数给类中的变量赋值时,,报错: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'XXX'; |nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 't

xinbadar的博客 3万+

Qt入门教程【环境搭建篇】Qt6+Mingw+QMake+QtCreator

Qt Windows平台安装

小鱼酱的博客 2万+

空指针报错:spring全家桶中关于@Autowired注入失败问题

@Autowired注入失败

kk爱闹 489

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Spring Boot 2.x注入RedisTemplate失败。启动报错

文章目录报错内容错误原因解决办法手动写RedisConfig配置类使用lettuce(未实战) 报错内容 使用@Autowired注入RedisTemplate的时候,项目启动报错 Field redisTemplate in com.**.***.*** required a bean of type 'org.springframework.data.redis.core.RedisTempl...

Ricardo_MLi的博客 3005

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Spring Bean注入失败合集|3类高频报错+全场景解决方案(附避坑指南)

Spring Bean注入失败合集|3类高频报错+全场景解决方案(附避坑指南)

大家好,我是一名深耕 Java 后端多年的老程序员,目前专注于 SpringBoot / SpringCloud / 微服务 / 分布式 方向开发。 多年企业级项目实战经验,踩过无数线上坑、填过无数架构坑,对 Spring 全家桶、MyBa 389

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

spring继承父类注入失败,报错空指针的一个解决办法

众所周知,抽象出来一个BaseDao的好处是巨大的,可以减少很多后续的DAO的工程量,只需让其他的dao继承这个baseDAO即可,然额问题来了,这个baseDAO,我们要用到HibernateTemplate。 我们把HibernateTemplate注入后,像寻常一样运行,子类像寻常一样调用父类,然后gg,报错空指针,查了半天最终发现,是hibernate没注进去,为啥呢,父类被调用了啊? ...

待到春花烂漫时 1万+

spring项目使用EMQX,使用@Autowired注入失败报错空指针问题记录

spring项目使用EMQX,使用@Autowired注入失败报错空指针问题记录

chunhev5的博客 1329

Spring使用注解注入失败,已经有了@Service(注解),XML里也用了<context>,也导入了AOP 仍然报错NoSuchBean

加粗样式 网上一般的错误 要么缺了注解@Service、@Controller、@Repository 要么缺了Jar包——AOP 要么就XML里配置出错 <context:component-scan basepackage=“FactoryBean”></context:component-scan> 奇怪的是我这三项都做好了,仍然报错NoSuchBean name=“...

kerapa的博客 1016

myeclipse测试代码中测试thymeleaf模板时Spring注入Bean失败报错

一:报错信息 org.springframework.beans.factory.BeanCreationException: Error creating bean with name XXXXXX: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.

qq_36552690的博客 1475

spring因为代理而导致对象注入失败

在Spring框架过程中难免会遇到很多坑,入门者对象注入失败可能有以下原因: 1、要注入的对象没有交给spring托管,检查一下配置文件是否有对应的bean或是否添加了注解 2、使用注解配置的同学仔细检查以下注解名称和你注入时填写的是否有误 3、使用注解看一下配置文件有没有开启扫描注解的配置 注入失败还有其他原因,例如代理。看一下下面的案例: public...

探索家的博客 1161

Spring Boot 应用启动失败,报错信息显示 MessageNoticeUtil 类中的 nisbosMessageCenterService 字段需要注入一个类型为 NisbosMessage

若。

靡不有初,鲜克有终 71

学习中MyBatis发生的报错(持续更新)

数据库链接地址数据库链接地址。

2401_85072552的博客 268

批量导入数据(空指针异常,注入失败,已解决)

这段代码演示了Spring Boot的依赖注入机制。通过@Resource注解自动注入userMapper,但直接new insertUsers()会导致注入失败,因为Spring容器未介入管理。正确做法是使用SpringApplication.run()启动应用获取Spring上下文,再通过context.getBean()获取已注入依赖的实例。这体现了Spring依赖注入的核心思想——由容器自动管理对象依赖关系,而非手动创建。传统new方式会破坏这种机制,导致NullPointerException。两

weixin_61728385的博客 545

通过@Autowired自动注入ThreadPoolTaskExecutor自定义线程池报错

线程池注入

qq_37262021的博客 2631

Spring Boot启动报错终极排查指南:11个高频问题+解决方案,程序员必藏

「Spring Boot启动报错」是开发者高频痛点:依赖冲突抛出NoSuchMethodError、静态资源加载失败触发Whitelabel Error Page、循环依赖导致BeanCurrentlyInCreationException......本文深度解析20个经典报错场景,覆盖配置、Bean管理、安全拦截、日志冲突等核心模块。每个问题均提供: 1️⃣ 根因定位:结合日志片段快速锁定问题; 2️⃣ 一键修复:命令行工具+代码示例直击解决方案; 3️⃣ 长效避坑:从版本兼容到线程池优化,预防二次踩雷

一本大学生java学习技术分享与交流 4582
上一篇: Redis的windos客户端远程连接redis失败解决
下一篇: sprigboot中使用a标签实现静态资源下载
朽木兮
博客等级 码龄9年 2粉丝 19原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值