Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场

背景

作者头像
彼岸舞
发布于 2023-01-10 05:31:10
发布于 2023-01-10 05:31:10
61900
代码可运行
举报
运行总次数:0
代码可运行

背景

  今天在写一个数据处理程序的时候, 我打算优化一下我的程序, 本来是直接用Mapper层进行单行记录保存的, 也就是调用的Mapper的insert函数

过程

  然后我就写了一个Service, 但是我没有写接口, 是直接写了一个具体的实现类

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Service
public class InstitutionService extends ServiceImpl<InstitutionMapper, Institution>{
}

  这样看应该是没问题的吧

使用方式

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Autowired
protected InstitutionService institutionService;

  直接在其他类中注入使用, 我打算直接用它的saveBatch函数

问题

  然后就出现了标题中的一幕, 启动项目报错了

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
2023-01-09 10:45:22,854 WARN (AbstractApplicationContext.java:559)- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexController': Unsatisfied dependency expressed through field 'organService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'organService': Unsatisfied dependency expressed through field 'institutionService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'institutionService' is expected to be of type 'com.example.parsedata.service.InstitutionService' but was actually of type 'com.sun.proxy.$Proxy74'
2023-01-09 10:45:22,855 INFO (DruidDataSource.java:2003)- {dataSource-1} closing ...
2023-01-09 10:45:22,858 INFO (DruidDataSource.java:2075)- {dataSource-1} closed
2023-01-09 10:45:22,860 INFO (DirectJDKLog.java:173)- Stopping service [Tomcat]
2023-01-09 10:45:22,869 INFO (ConditionEvaluationReportLoggingListener.java:136)- 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-01-09 10:45:22,871 ERROR (LoggingFailureAnalysisReporter.java:40)- 

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'institutionService' could not be injected as a 'com.example.parsedata.service.InstitutionService' because it is a JDK dynamic proxy that implements:


Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

分析

这里就可以分析一下问题原因了, 其实Description和Action描述的已经很清楚了

Description

这个Bean institutionService 在进行自动装配的时候不能找到 InstitutionService这个类, 因为使用的是JDK动态代理实现的

Action

考虑作为一个接口的实现, 或者强制使用CGLIB动态代理

应为Spring默认使用的是JDK的动态代理, 而JDK动态代理是基于接口实现的, 而在生成了代理类之后, 因为注入的不是接口, 而是实现类, 所以无法注入

解决

创建一个接口给现在的类实现

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public interface IInstitutionService extends IService<Institution> {
}
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Service
public class InstitutionService extends ServiceImpl<InstitutionMapper, Institution> implements IInstitutionService{
}

使用时注入接口

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Autowired
protected IInstitutionService institutionService;

再次启动, 就OK了, 完美解决

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-01-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【Java报错已解决】org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean wit
🎬 鸽芷咕:个人主页 🔥 个人专栏: 《C++干货基地》《粉丝福利》
鸽芷咕
2025/05/26
3580
解决:Field xxMapper in xx.service.impl.xxServiceImpl
1、启动 SpringBoot项目报错,使用的是Springboot、Spring、Mybatis连接Mysql数据库,启动SpringBoot项目报错,错误如下所示:
别先生
2020/03/19
2.2K0
问题排查之'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' that could not be found.- Bean metho
今天将一个SpringBoot项目的配置参数从原有的.yml文件迁移到Apollo后,启动报错“Bean method 'rocketMQTemplate' in 'RocketMQAutoConfiguration' not loaded because @ConditionalOnBean (types: org.apache.rocketmq.client.producer.DefaultMQProducer; SearchStrategy: all) did not find any beans of type org.apache.rocketmq.client.producer.DefaultMQProducer”。花了两个小时才最终搞清楚,原因是缺少了配置项 spring.rocketmq.producer.group 从而导致无法成功创建RocketMQAutoConfiguration这个Bean,从而导致一连串对此有依赖的Bean无法创建成功。
翎野君
2023/05/12
1.7K0
问题排查之'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' that could not be found.- Bean metho
万字长文带你彻底吃透Spring循环依赖,堪称全网最全(文末福利)
作者:冰河 博客:https://binghe.gitcode.host 文章汇总:https://binghe.gitcode.host/md/all/all.html 源码地址:https://github.com/binghe001/spring-annotation-book/tree/master/spring-annotation-chapter-20
冰河
2023/12/04
3.2K0
万字长文带你彻底吃透Spring循环依赖,堪称全网最全(文末福利)
SpringBoot项目报错解决:“Error starting ApplicationContext. To display the conditions report re-run …”
SpringBoot项目报错:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
全栈程序员站长
2022/09/09
9.3K1
SpringBoot项目报错解决:“Error starting ApplicationContext. To display the conditions report re-run …”
记一次线上偶现的循环依赖问题
前情回顾 一探 Spring 的循环依赖,源码详细分析 → 真的非要三级缓存吗 中讲到了循环依赖问题 同样说明了 Spring 只能解决 setter 方式的循环依赖,不能解决构造方法的循环依赖 重点介绍了 Spring 是如何解决 setter 方式的循环依赖,感兴趣的可以去看下 二探 既然 Spring 不能解决构造方法的循环依赖,那么它是如何甄别构造方法循环依赖的了? 所以进行了二探:再探循环依赖 → Spring 是如何判定原型循环依赖和构造方法循环依赖的? 从源码的角度讲述了 Spring 是如何
程序猿DD
2022/03/24
1.1K0
SpringBoot从1.5.4升级到2.7.2问题总结
编译不报错,启动报错,在springboot1.3版本中会默认提供一个RestTemplate的实例Bean,当在springboot1.4以及以后的版本中,需要手动创建一个RestTemplate的配置,这里将会导致循环依赖
猫头虎
2024/04/08
5850
SpringBoot从1.5.4升级到2.7.2问题总结
FeignClient注解及参数问题---SpringCloud微服务
在用分布式架构SpringBoot的SpringCloud技术开发过程中,FeignClient 是一个常用的注解,且很重要的功能。
程序大视界
2020/07/21
1.8K0
springboot2.0 常见问题找不到
第一种:启动类注解上有@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
用户5899361
2020/12/07
7680
Java异常宝典
在开发过程中,我们会遇到各种各样的问题,这边博主给大家收集总结了一下,有需要的可以收藏哦~
林老师带你学编程
2020/04/30
1.2K0
required a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘ that could not b
Eureka 错误收集: [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘org.springfra
青山师
2023/05/05
9380
required a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘ that could not b
Error creating bean with name ‘attrAttrgroupRelationController‘
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'attrAttrgroupRelationController':  Unsatisfied dependency expressed through field 'attrAttrgroupRelationService';  nested exception is org.springframework.
一个风轻云淡
2022/11/15
1K0
Error creating bean with name ‘attrAttrgroupRelationController‘
Error creating bean with name ‘eurekaClientConfigBean’: Singleton bean creation not allowed!
首先说明一下这是一个Spring boot 集成Quartz做任务调度的项目,版本信息就不贴了,因为和本文最终的解决方案没有什么关系。
全栈程序员站长
2022/07/20
1.3K0
spring之通过注解方式配置Bean(二)
上一节讲到了基本的基于注解的配置Bean,但是每个Bean之间是没有关联的,现在我们想实现下面的功能。
西西嘛呦
2020/08/26
3740
spring之通过注解方式配置Bean(二)
Spring系列第10篇:primary可以解决什么问题?
上面代码很简单,@1:定义了一个接口IService,@2和@3创建了两个类都实现了IService接口。
路人甲Java
2020/02/26
8180
Caused by: java.lang.NoClassDefFoundError: org/mybatis/logging/LoggerFactory
项目原来使用mybaties, 在整合mybaties-plus时. 出现错误 java.lang.NoClassDefFoundError: org/mybatis/logging/LoggerFactory
时间静止不是简史
2022/04/02
3.9K0
MyBatis Plus + Activity 整合报错
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexConroller': Unsatisfied dependency expressed through field 'miaoService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyExcep
二十三年蝉
2018/10/11
2K2
MyBatis Plus + Activity 整合报错
一文告诉你Spring是如何利用"三级缓存"巧妙解决Bean的循环依赖问题的【享学Spring】
循环依赖:就是N个类循环(嵌套)引用。 通俗的讲就是N个Bean互相引用对方,最终形成闭环。用一副经典的图示可以表示成这样(A、B、C都代表对象,虚线代表引用关系):
YourBatman
2019/09/03
53.2K12
一文告诉你Spring是如何利用"三级缓存"巧妙解决Bean的循环依赖问题的【享学Spring】
三探循环依赖 → 记一次线上偶现的循环依赖问题
Spring 的循环依赖,源码详细分析 → 真的非要三级缓存吗 中讲到了循环依赖问题
青石路
2022/05/10
8810
三探循环依赖 → 记一次线上偶现的循环依赖问题
解决循环依赖问题:优雅处理依赖关系的技巧
在软件开发中,依赖是不可避免的。我们经常需要在应用程序的不同组件之间建立依赖关系,以实现功能的模块化和复用。然而,有时候依赖关系可能变得复杂,甚至导致循环依赖的问题。在本文中,我们将通过项目中实际遇到的异常探讨一些解决循环依赖问题的技巧,帮助你在开发过程中优雅地处理依赖关系。
修己xj
2023/08/25
1K0
解决循环依赖问题:优雅处理依赖关系的技巧
推荐阅读
相关推荐
【Java报错已解决】org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean wit
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档