java类使用aop导致无法找到类对象

背景

 公司某贷业务线同学某天在针对aop方法进行单测的时候遇到问题然后有幸帮忙一起排查问题,报错的信息翻译过来就是在spring上下文中找不到指定类型的类对象,具体错误信息如下。

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)

 额外信息就是在针对某类增加aop前是没有问题,但是增加aop之后就会报如上错误,所以基本上第一反应就是aop影响了程序的正常启动,只是具体原因不确定而已。


应用分析

类的具体实现方式

  • 该类是需要进行单测的类,该类的某方法被aop进行了修饰。

  • PtXxxCoreServiceImpl的父类BaseSaleXxxService实现了接口BizSaleXxxService。

  • PtXxxCoreServiceImpl继承自BaseSaleXxxService间接实现接口。

public abstract class BaseSaleXxxService implements BizSaleXxxService { }

public class PtXxxCoreServiceImpl extends BaseSaleXxxService { }



类的单测代码

  • 单测的代码,通过在xxxServiceFactory.getCoreService()在spring上线文获取PtSaleCoreServiceImpl对象。

  • 针对该对象的updateXxxCredit进行单测,updateXxxCredit方法通过aop进行切面。

  • xxxServiceFactory.getCoreService(XxxChannelEnum.PT.getChannel())获取PtXxxCoreServiceImpl类型对象。

  • 获取PtXxxCoreServiceImpl类型对象报No qualifying bean of type [PtXxxCoreServiceImpl] found for dependency错误。

    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的代码

  • 对PtXxxCoreServiceImpl进行了切面操作生成的代理的对象不再是PtXxxCoreServiceImpl。
  • 由于PtXxxCoreServiceImpl实现了BizSaleXxxService,猜测最后的代理的对象应该是BizSaleXxxService类型。
@Aspect
public class MonitorXxxService {
    @Before(value = "execution(* service.biz.sale.impl.PtXxxCoreServiceImpl.updateXxxCredit(..))")
    public void salePtCreditMarvin(JoinPoint point) {
    }
}



原因分析

  • PtXxxCoreServiceImpl类实现某个接口(implements xxxInterface)导致走JDK的代理实现方式。
  • PtXxxCoreServiceImpl的代理对象类型不再是PtXxxCoreServiceImpl,个人猜测为接口xxxInterface类型。
  • 通过JDK生成的代理的类型和类PtXxxCoreServiceImpl不匹配导致报错该类型的对象找不到报错。



spring官网有一段话解释Spring AOP的具体两种实现以及何时用何种代理实现。

  • 如果代理对象的类实现了至少一个接口(implements interface),那么JDK代理方式会被使用。
  • 如果代理对象的类没有实现任何接口(implements interface),那么CGLIB代理方式会被使用。
  • 如果你想强制使用CGLIB方式实现代理,设置 <aop:config> 标签的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"/>


解决方案

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


参考

猜你喜欢

转载自blog.csdn.net/weixin_33725239/article/details/87326290