Java报错 | (一) because it is a JDK dynamic proxy that implements:

1,024 阅读3分钟

微信公众号:秀基宝。如有问题,请后台留言,反正我也不会听。

前言

我们做项目迁移时候,不免会遇到各种各样的问题,特别是不同库、同文件名、同表之类,会导致连启动都会失败,那么今天我们来一探究竟其中一个常见问题

问题

2022-05-25 09:40:04,044  INFO DruidDataSource:930 - {dataSource-2} inited
2022-05-25 09:40:13,398  INFO DruidDataSource:930 - {dataSource-3} inited
2022-05-25 09:40:15,374  WARN AnnotationConfigEmbeddedWebApplicationContext:550 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'projectService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appProjectServiceImpl': Unsatisfied dependency expressed through field 'proRelationService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proRelationServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'projectMapper' is expected to be of type 'com.mapper.AppProjectMapper' but was actually of type 'com.sun.proxy.$Proxy160'
2022-05-25 09:40:15,377  INFO DruidDataSource:1825 - {dataSource-3} closed
2022-05-25 09:40:15,379  INFO DruidDataSource:1825 - {dataSource-2} closed
2022-05-25 09:40:15,379  WARN CommonAnnotationBeanPostProcessor:162 - Invocation of destroy method failed on bean with name 'scheduledTask': java.lang.NullPointerException
2022-05-25 09:40:15,387  INFO DruidDataSource:1825 - {dataSource-1} closed
2022-05-25 09:40:15,387  INFO AnnotationConfigApplicationContext:987 - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@53b43413: startup date [Wed May 25 09:39:44 CST 2022]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@12bbf8b1
2022-05-25 09:40:15,395  INFO AutoConfigurationReportLoggingInitializer:101 - 

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

Description:

The bean 'projectMapper' could not be injected as a 'com.system.mapper.AppProjectMapper' because it is a JDK dynamic proxy that implements:
	com.test.system.core.Mapper


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.

上面包名不用注意,已经被我删除修改过。

发现

从上可以看得出,我们一个projectMapper报错,因为主要配置基于JDK的代理还是基于类的动态代理的配置,这种错误提示需要设置基于类的代理才行。

解决

1、网上常见办法

proxy-target-class=true或者transactional

2、修改命名

private AppProjectMapper projectMapper;
private ProjectMapper projectMapper;

在两个类类似情况下,假如你命名一样的话,jdk会默认去找下面那个类,最终发现找不到对应方法,也就报错了

你可以看上面报错信息 image.png 你现在应该知道了吧,该问题在哪个类,还能定义到那个bean出现问题。

后言

因为jdk动态代理对类进行包装后,原生类隐藏了(->target),冒出来一个只代表接口实现的新生类;即接口申明的方法,新生类才会有。 如果你执意要以此新生类注入至其他bean实例,那么在runtime过程中,可能因为你调用的方法未在接口中声明而抛出异常;

遇到问题不要慌,慢慢来肯定会发现问题所在。我是人见人爱的爱玩基金的秀总,如果你觉得对你有帮助,请点个赞吧

本人开发的玩基金小工具

个人博客:
名称:纯洁的麦田
链接:[http://www.idearyou.cn/]
描述:争取哪一天做上架构师
公众号:纯洁的麦田

网址:[xiu.idearyou.cn]
谷歌插件搜:秀基宝
小程序:秀基宝
复制代码
复制代码
复制代码

后语

如果本文对你哪怕有一丁点帮助,请帮忙点好看。你的好看是我坚持写作的动力。 另外,关注免费学习。

The bean 'projectMapper' could not be injected as a 'com.vivo.qa.test.system.pqm.mapper.AppProjectMapper' because it is a JDK dynamic proxy that implements: com.vivo.qa.test.system.core.Mapper