Injection of resource dependencies failed解决办法总结

本文记录了一次在Spring Boot项目中遇到的资源注入失败的错误及其排查过程。错误涉及到了多个Bean创建失败,最终指向了mybatis配置文件解析失败的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天调试项目代码,出现的引resource的报错,查原因查了好长时间才找到,现在这里总结一下,以免以后忘掉以及给大家参考。

报错大致内容入下:

Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'merchantController': Injection of resource dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'merchantService': Injection of resource dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'merchantExtMapper' defined in file [D:\Source_huawei\ips-
common\target\classes\com\dmuoceanus\ips\common\dao\mysql\core\mappers\MerchantExtMapper.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [config/spring/spring-mybatis.xml]: Invocation of init method failed; nested exception is 
org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\Source_huawei\ips-
common\target\classes\com\dmuoceanus\ips\common\dao\mysql\core\mappers\MerchantFlowMapper.xml]'; nested exception is 
org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for 
com.dmuoceanus.ips.common.dao.mysql.core.mappers.MerchantFlowMapper.BaseResultMap

暂时还没找到解决方法,所以博主决定从springBoot底层源码开始一步一步学起,等到可以解决这个问题了,回来补充,并且将今后开发过程中遇到的问题以及解决方案记录在博客上

在Spring Boot应用启动过程中,如果出现`ConfigServletWebServerApplicationContext`上下文初始化异常,并且错误信息中包含`Failed to start bean 'webServerStartStop'`,这通常表明Web服务器在启动过程中遇到了问题。此类问题可能涉及多个方面,包括配置错误、端口冲突、SSL设置不当、或依赖项缺失等。 以下是一些常见的原因和对应的解决方案: ### 1. 端口冲突 如果应用尝试使用的端口已经被其他进程占用,会导致Web服务器无法启动。可以通过修改`application.properties`或`application.yml`文件中的`server.port`配置来更换端口。 ```properties server.port=8081 ``` ### 2. SSL配置错误 如果启用了HTTPS并配置了SSL证书,但配置不正确(如证书路径错误、密码错误等),会导致Web服务器启动失败。确保`application.properties`中的SSL配置正确。 ```properties server.ssl.key-store=classpath:keystore.jks server.ssl.key-store-password=your_password server.ssl.key-store-type=JKS server.ssl.key-alias=myalias ``` ### 3. 缺少必要的依赖项 如果项目中缺少嵌入式Web服务器的依赖(如Tomcat、Jetty或Undertow),会导致启动失败。例如,使用Tomcat时应确保`pom.xml`中包含以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> ``` ### 4. 自定义WebServerFactory配置错误 如果应用中存在自定义的`WebServerFactoryCustomizer`来调整Web服务器的行为,但配置错误(如非法的连接限制、无效的SSL设置等),也会导致启动失败。检查并调试此类配置类,确保其逻辑正确。 ### 5. 日志输出与堆栈跟踪分析 查看详细的堆栈跟踪日志是解决问题的关键。通常,`Failed to start bean 'webServerStartStop'`之后会伴随更具体的错误信息,例如绑定异常、SSL握手失败等。根据这些信息可以更精准地定位问题。 ### 6. 嵌入式服务器初始化失败 如果嵌入式服务器(如Tomcat)在初始化过程中遇到错误(如无法创建连接池、无法加载本地库等),也会导致Web服务器启动失败。检查Tomcat日志或Spring Boot日志,确认是否存在此类问题。 ### 7. 系统资源不足 在某些情况下,系统资源不足(如内存不足、文件描述符限制等)可能导致Web服务器无法启动。确保系统资源充足,并适当调整JVM参数以优化内存使用。 ### 8. Spring Boot版本兼容性问题 某些Spring Boot版本可能存在已知的Bug,尤其是在使用特定的嵌入式服务器或第三方库时。参考Spring Boot的官方文档和发行说明,确认当前版本是否已知存在问题,并考虑升级到最新稳定版本。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值