简体   繁体   English

在不使用aspectj的情况下,使用cglib或jdk代理在spring aop中有哪些高性能?

[英]which is high performance in spring aop using cglib or jdk proxy without aspectj?

we have implemented the spring AOP into our application without using aspectj. 我们已经在不使用Aspectj的情况下将spring AOP实施到了我们的应用程序中。 We made the auto proxy as true to make it use CGLIB proxy. 为了使它使用CGLIB代理,我们将自动代理设置为true。

The reason we made it as proxy target class = 'true' to resolve the proxy error. 我们将其作为代理目标类='true'来解决代理错误的原因。 as a side effect the application becomes slow and taking longer time to execute. 副作用是应用程序变慢并且需要更长的时间来执行。

Is there a way out of this which will help us to keep performance intact and also to escape the proxy error. 有没有什么办法可以帮助我们保持性能不变并避免代理错误。

<!-- Aspects -->
    <bean id="loggingAspect" class="web.aspect.LogAspectAroundMethod" />
    <!-- PointCut -->
    <bean id="myLogPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="pattern" value=".*" />
    <property name="excludedPatterns">
    <list>
    <value>.*.isDaemon.*</value>
    </list>
    </property>
    </bean>

    <!-- Advisor Around-->
    <bean id="myLogAroundAdvisor" class="org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor">
      <property name="adviceBeanName" value="loggingAspect" />
      <property name="pointcut" ref="myLogPointCut" />
    </bean>

    <bean id="aprProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="proxyTargetClass" value="true"/>
    <property name="beanNames">
    <value>*Delegate,*Builder*,*Impl*,*Controller,*Handler,*Helper</value>
    </property>
    <property name="interceptorNames">
    <list>
    <value>myLogAroundAdvisor</value>
    </list>
    </property>
    </bean>

Advisor code follows:- 顾问代码如下:

Provided only the implementation part:- 仅提供实施部分:

    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;

    public class LogAspectAroundMethod implements MethodInterceptor{

        public Object invoke(MethodInvocation methodInvocation) throws Throwable {

               Object result = methodInvocation.proceed();
               return result;
        }

}

CGLIB应该更快,JDK代理内部使用反射(请参见Spring的JdkDynamicAopProxy),CGLIB代理直接通过生成的代码调用目标方法

What is slow? 什么慢? Start-up performance or performance later when everything is already set up? 启动性能还是以后一切都设置好后的性能? Maybe there are performance issues with JdkRegexpMethodPointcut , I do not know. 我不知道JdkRegexpMethodPointcut可能存在性能问题。 Maybe it is the high number of created proxies, you seem to match a lot of classes and methods. 也许是由于创建代理的数量很高,您似乎匹配了许多类和方法。 Furthermore, your advice themselves may be slow. 此外,您的建议本身可能很慢。 You are not showing them here. 您未在此处显示它们。 What happens if your advice just calls proceed() and does nothing else? 如果您的建议仅调用proceed()而没有执行其他操作,会发生什么? Do you even need around() advice or would before() or after() suffice? 您甚至需要around()建议还是before()after()足够? Many open questions. 许多悬而未决的问题。 Try to debug differentially, ie change one thing at a time. 尝试进行差异调试,即一次更改一件事。 First trivialise the advice as suggested in order to find out about their performance impact. 首先,按照建议对建议进行琐碎处理,以了解它们对性能的影响。 Also consider to use normal AspectJ matching syntax instead of Regex matching, maybe that is slow. 还可以考虑使用常规的AspectJ匹配语法而不是Regex匹配,这可能很慢。 Then try matching fewer classes and measure the impact. 然后尝试匹配较少的类并衡量影响。

Last but not least, even if you do not want to hear it, consider AspectJ which does not need any proxies. 最后但并非最不重要的一点是,即使您不想听,也可以考虑不需要任何代理的AspectJ。 Especially with compile-time weaving you can also minimise the impact on start-up time. 特别是使用编译时编织,还可以最大程度地减少对启动时间的影响。

One problem I see is the amount of proxies created combined with the use of JdkRegexpMethodPointcut . 我看到的一个问题是与JdkRegexpMethodPointcut结合使用创建的代理JdkRegexpMethodPointcut

When determining if an advice needs to be executed there are 2 parts in play. 在确定是否需要执行建议时,有两个部分在起作用。 First there is the static part of the pointcut. 首先是切入点的静态部分。 This is basically the point where the proxy is created and is, in this case, based on the name. 这基本上是创建代理的点,在这种情况下,基于名称。 You seem to have quite some beans which need the advice applied. 您似乎有很多需要应用建议的bean。

Second there is the dynamic part which is executed for each method call on the proxy. 第二个是动态部分,它对代理上的每个方法调用执行 In your case that is a JDK based regular expression which is executed to determine if the method name matches. 在您的情况下,这是基于JDK的正则表达式,执行该表达式可确定方法名称是否匹配。 Now the JDK regexp impementation is known to be slower then other implementations. 现在,已知JDK regexp实现比其他实现要慢。

I would expect the amount of proxies combined with the regexp is slowing your application down. 我希望与regexp结合使用的代理数量会使您的应用程序变慢。

I would strongly urge you to try and convince your company and allow the use of AspectJ, why would there be a risk in using a framework that is used by millions of developers? 我强烈敦促您尝试说服您的公司并允许使用AspectJ,为什么使用数百万开发人员使用的框架会有风险? There is more risk in using the AOP alliance thing (what you are using now) as that is used by far less people then AspectJ. 使用AOP联盟的东西(您现在正在使用的东西)存在更大的风险,因为与AspectJ相比,使用该东西的人要少得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM