@Async导致获取Bean异常‘com.sun.proxy.$Proxy124‘

@Async导致获取Bean异常’com.sun.proxy.$Proxy124’

Request processing failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named ‘wechatPayServiceImpl’ is expected to be of type ‘net.fkzg.pay.biz.service.PaymentService’ but was actually of type ‘com.sun.proxy.$Proxy124’

public interface WxCommonService {}

public abstract class PaymentService {}

@Service
public class WechatPayServiceImpl extends PaymentService implements WxCommonService {}


上面是我定义的WechatPayServiceImplPaymentService service = SystemContext.getBean("wechatPayServiceImpl", PaymentService.class);
我是这样去获取的,但是报错了:

Request processing failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'wechatPayServiceImpl' is expected to be of type 'net.fkzg.pay.biz.service.PaymentService' but was actually of type 'com.sun.proxy.$Proxy124'

由于WechatPayServiceImpl中有使用@Async导致

在Spring中,当您使用AOP(面向切面编程)时,Spring会创建一个代理对象来管理方法的调用。您在使用@Service注解的类上使用@Async注解时,Spring会为该类创建一个代理对象,以便在调用异步方法时能够在后台执行。这就是为什么您在使用SystemContext.getBean(“wechatPayServiceImpl”, PaymentService.class)获取bean时得到了com.sun.proxy.$Proxy124类型的代理对象而不是WechatPayServiceImpl类型的实际对象。

解决方案

如果您确实需要使用WechatPayServiceImpl类型的实际对象,而不是代理对象,可以通过以下两种方法之一解决这个问题:

  1. 使用WechatPayServiceImpl类型的实际对象声明您的变量:
WechatPayServiceImpl service = SystemContext.getBean("wechatPayServiceImpl", WechatPayServiceImpl.class);
  1. 在调用SystemContext.getBean时,使用WechatPayServiceImpl的接口类型而不是父类PaymentService的类型:
WxCommonService service = SystemContext.getBean("wechatPayServiceImpl", WxCommonService.class);
  1. 把使用@Async注解的方法从WechatPayServiceImpl中移除,放到其他Server中去

.

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值