前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

背景

作者头像
彼岸舞
发布2023-01-10 13:31:10
5420
发布2023-01-10 13:31:10
举报

背景

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

过程

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

代码语言:javascript
复制
@Service
public class InstitutionService extends ServiceImpl<InstitutionMapper, Institution>{
}

  这样看应该是没问题的吧

使用方式

代码语言:javascript
复制
@Autowired
protected InstitutionService institutionService;

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

问题

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

代码语言:javascript
复制
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
复制
public interface IInstitutionService extends IService<Institution> {
}
代码语言:javascript
复制
@Service
public class InstitutionService extends ServiceImpl<InstitutionMapper, Institution> implements IInstitutionService{
}

使用时注入接口

代码语言:javascript
复制
@Autowired
protected IInstitutionService institutionService;

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 过程
  • 使用方式
  • 问题
  • 分析
  • 解决
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档