Spring AOP启动时间慢

Yuv*_*mar 6 java performance aop spring spring-aop

我们使用带有@AspectJ样式注释的Spring(3.0.5)AOP <aop:aspectj-autoproxy/> .我们将它用于事务,审计,性能分析等.除了应用程序的启动时间随着更多代码的添加而不断增长之外,它的工作正常.

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

我的目标是让应用程序在5-10秒内启动,而不是现在的约45秒,所以任何提示都会非常感激.

Kar*_*ran 5

从您发布的内容看起来您使用加载时间编织会导致启动损失,因为系统必须在加载时编织所有类.如果您的主要关注点是启动时间,那么我建议您切换到编译时编织.您可以在spring文档(第6章,第8节)或AspectJ站点(http://www.eclipse.org/aspectj/docs.php)中找到有关如何执行此操作的说明.

使用AspectJ编译器切换到编译时编织是相对紧张的:

  1. <aop:aspectj-autoproxy/>从上下文文件中删除 表示法.
  2. 将aspectJ编译步骤添加到构建文件中.在AspectJ网站上你应该能够找到一个ant插件,codehaus有一个maven插件.以下是我们两者的示例.

对于Maven:

<plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>aspectj-maven-plugin</artifactId>
     <version>1.3</version>
     <configuration>
     <verbose>true</verbose>
      <source>1.6</source>
      <target>1.6</target>
      <complianceLevel>1.6</complianceLevel>
      <showWeaveInfo>true</showWeaveInfo>
      <aspectLibraries>
        <aspectLibrary>
                                <groupId>org.springframework</groupId>
                                <artifactId>spring-aspects</artifactId>
                            </aspectLibrary>
                        </aspectLibraries>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>   
                            </goals>
                        </execution>
                    </executions>
                </plugin>
Run Code Online (Sandbox Code Playgroud)

对于Ant

 <taskdef
            resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
            <classpath>
                <pathelement location="${lib.dir}/AspectJ_1.6.8/aspectjtools.jar"/>
            </classpath>
        </taskdef>

  <iajc aspectPath="${file.reference.spring-aspects.jar}; ${build.classes.dir}/path/to/custom/aspects"
              classpath="${lib.dir}/AspectJ_1.6.8/aspectjrt.jar; ${javac.classpath}"
              inpath="${build.classes.dir}"
              destDir="${build.classes.dir}"
              showWeaveInfo="true" />
Run Code Online (Sandbox Code Playgroud)


ale*_*ext 5

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

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

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

@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;
  }
}
Run Code Online (Sandbox Code Playgroud)

就我而言,启动时间从60s降至15s。

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