Spring AOP启动时间慢 [英] Spring AOP slow startup time

查看:357
本文介绍了Spring AOP启动时间慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用带有@AspectJ样式注释和<aop:aspectj-autoproxy/> 的Spring(3.0.5)AOP.我们将它用于事务,审计,概要分析等.它工作正常,但是随着添加的代码越来越多,应用程序的启动时间不断增长.

We're using Spring (3.0.5) AOP with @AspectJ style annotations and <aop:aspectj-autoproxy/> . We use it for transactions, auditing, profiling etc. It works fine except that the startup time of the application is continuously growing as more code is being added.

我进行了一些分析,发现大部分时间都用在Spring容器初始化期间,更具体地说org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(String, ObjectFactory)-大约需要35秒. org.springframework.aop.support.AopUtils.canApply(Pointcut, Class, boolean)-大约需要15秒.

I have done some profiling and found that most of the time is spent during Spring container initialization, more specifically org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(String, ObjectFactory) - takes about 35 sec. org.springframework.aop.support.AopUtils.canApply(Pointcut, Class, boolean) - takes about 15 sec.

我的目标是使应用程序在5-10秒内启动,而不是现在的45秒,因此任何提示将不胜感激.

My goal is for the application to start in 5-10 seconds and not ~45 sec as it does now, so any tips would be much appreciated.

推荐答案

我遇到了同样的问题,事实证明,Spring AOP自动代理在启动时使用bcel加载类花费了很多时间(没有缓存,因此再次加载并尝试找出适用的建议时,再次使用相同的类(如java.lang.Object ...). 通过编写更细粒度的切入点(例如,在@within中使用),可以在某种程度上进行改进,但是我发现,如果所有切入点都使用@annotation编写,则解决方案效果会更好.

I had the same issue, it turns out Spring AOP auto-proxying spends a LOT of time at startup loading classes with bcel (without caching, so loading again and again same classes like java.lang.Object...) when trying to figure out which advices apply. It can be somewhat improved by writing more fine-grained Point cuts (use within, @within for example) but I've found a solution that worked better if all your pointcuts are written with @annotation.

1)使用以下命令停用自动代理:spring.aop.auto = false

1) Desactivate auto-proxy with: spring.aop.auto=false

2)编写一个AnnotationAwareAspectJAutoProxyCreator的自定义子类,以根据您自己的条件过滤要装饰的bean,例如,这个基于包和注释:

2) Write a custom subclass of AnnotationAwareAspectJAutoProxyCreator to filter beans to be decorated according to your own criteria, for example this one is based on package and annotations :

@Override
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName,   TargetSource targetSource) {
  if (beanClass != null && isInPackages(beansPackages, beanClass.getName()) &&   hasAspectAnnotation(beanClass)) {
    return super.getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
  } else {
    return DO_NOT_PROXY;
  }
}

在我的情况下,启动时间从60s减少到15s.

In my case startup time down from 60s to 15s.

我希望它能帮助某人和北极熊

I hope it will help someone and polar bears

这篇关于Spring AOP启动时间慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆