java類使用aop導(dǎo)致無(wú)法找到類對(duì)象

背景

?公司某貸業(yè)務(wù)線同學(xué)某天在針對(duì)aop方法進(jìn)行單測(cè)的時(shí)候遇到問(wèn)題然后有幸?guī)兔σ黄鹋挪閱?wèn)題,報(bào)錯(cuò)的信息翻譯過(guò)來(lái)就是在spring上下文中找不到指定類型的類對(duì)象,具體錯(cuò)誤信息如下。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [service.biz.sale.impl.PtXxxCoreServiceImpl] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency. 
Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, 
description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
  at factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
  at factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
  at factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)

?額外信息就是在針對(duì)某類增加aop前是沒(méi)有問(wèn)題,但是增加aop之后就會(huì)報(bào)如上錯(cuò)誤,所以基本上第一反應(yīng)就是aop影響了程序的正常啟動(dòng),只是具體原因不確定而已。


應(yīng)用分析

類的具體實(shí)現(xiàn)方式

  • 該類是需要進(jìn)行單測(cè)的類,該類的某方法被aop進(jìn)行了修飾。

  • PtXxxCoreServiceImpl的父類BaseSaleXxxService實(shí)現(xiàn)了接口BizSaleXxxService。

  • PtXxxCoreServiceImpl繼承自BaseSaleXxxService間接實(shí)現(xiàn)接口。

public abstract class BaseSaleXxxService implements BizSaleXxxService { }

public class PtXxxCoreServiceImpl extends BaseSaleXxxService { }



類的單測(cè)代碼

  • 單測(cè)的代碼,通過(guò)在xxxServiceFactory.getCoreService()在spring上線文獲取PtSaleCoreServiceImpl對(duì)象。

  • 針對(duì)該對(duì)象的updateXxxCredit進(jìn)行單測(cè),updateXxxCredit方法通過(guò)aop進(jìn)行切面。

  • xxxServiceFactory.getCoreService(XxxChannelEnum.PT.getChannel())獲取PtXxxCoreServiceImpl類型對(duì)象。

  • 獲取PtXxxCoreServiceImpl類型對(duì)象報(bào)No qualifying bean of type [PtXxxCoreServiceImpl] found for dependency錯(cuò)誤。

    public void testUpdateXXXCredit() throws Exception {
        try {
            xxxServiceFactory.getCoreService(
                    XxxChannelEnum.PT.getChannel()).updateXxxCredit(111701210L,
                    "200", DateUtil.getNowTimeInSec(),
                    SaleCreditXxxStatusEnum.SUCCESS.getCode());
        } catch (Exception e) {
            throw  e;
        }
    }



AOP的代碼

  • 對(duì)PtXxxCoreServiceImpl進(jìn)行了切面操作生成的代理的對(duì)象不再是PtXxxCoreServiceImpl。
  • 由于PtXxxCoreServiceImpl實(shí)現(xiàn)了BizSaleXxxService,猜測(cè)最后的代理的對(duì)象應(yīng)該是BizSaleXxxService類型。
@Aspect
public class MonitorXxxService {
    @Before(value = "execution(* service.biz.sale.impl.PtXxxCoreServiceImpl.updateXxxCredit(..))")
    public void salePtCreditMarvin(JoinPoint point) {
    }
}



原因分析

  • PtXxxCoreServiceImpl類實(shí)現(xiàn)某個(gè)接口(implements xxxInterface)導(dǎo)致走JDK的代理實(shí)現(xiàn)方式。
  • PtXxxCoreServiceImpl的代理對(duì)象類型不再是PtXxxCoreServiceImpl,個(gè)人猜測(cè)為接口xxxInterface類型。
  • 通過(guò)JDK生成的代理的類型和類PtXxxCoreServiceImpl不匹配導(dǎo)致報(bào)錯(cuò)該類型的對(duì)象找不到報(bào)錯(cuò)。



spring官網(wǎng)有一段話解釋Spring AOP的具體兩種實(shí)現(xiàn)以及何時(shí)用何種代理實(shí)現(xiàn)。

  • 如果代理對(duì)象的類實(shí)現(xiàn)了至少一個(gè)接口(implements interface),那么JDK代理方式會(huì)被使用。
  • 如果代理對(duì)象的類沒(méi)有實(shí)現(xiàn)任何接口(implements interface),那么CGLIB代理方式會(huì)被使用。
  • 如果你想強(qiáng)制使用CGLIB方式實(shí)現(xiàn)代理,設(shè)置 <aop:config> 標(biāo)簽的proxy-target-class屬性為true即可。
Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. 
(JDK dynamic proxies are preferred whenever you have a choice).

If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used. 
All of the interfaces implemented by the target type will be proxied. 
If the target object does not implement any interfaces then a CGLIB proxy will be created.

If you want to force the use of CGLIB proxying, set the value of the 
proxy-target-class attribute of the <aop:config> element to true

<aop:aspectj-autoproxy proxy-target-class="true"/>


解決方案

  • 設(shè)置<aop:aspectj-autoproxy proxy-target-class="true"/>使全局都走CGLIB代理方式。


參考

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容