spring

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

2019-01-22  本文已影响93人  晴天哥_王志

背景

 公司某贷业务线同学某天在针对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影响了程序的正常启动,只是具体原因不确定而已。

应用分析

类的具体实现方式

public abstract class BaseSaleXxxService implements BizSaleXxxService { }

public class PtXxxCoreServiceImpl extends BaseSaleXxxService { }



类的单测代码

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

@Aspect
public class MonitorXxxService {
    @Before(value = "execution(* service.biz.sale.impl.PtXxxCoreServiceImpl.updateXxxCredit(..))")
    public void salePtCreditMarvin(JoinPoint point) {
    }
}



原因分析



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

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"/>

解决方案

参考

上一篇下一篇

猜你喜欢

热点阅读