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

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

问题描述

我们使用带有 @AspectJ 样式注释和 的 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;
  }
}

就我而言,启动时间从 60 秒缩短到 15 秒.

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

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

I hope it will help someone and polar bears

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

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