Spring启动性能问题 [英] Spring startup performance issues

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

问题描述

我正在尝试将Spring集成到一个包含数千个类的相当大的应用程序中,并且由于组件扫描,我在启动容器时遇到了很大的延迟。

I'm trying to integrate Spring in a pretty large application with thousands of classes, and i'm experiencing huge delays starting my container because of component-scanning.

我已经将base-package中指定的目录数量缩小到最小,以减少扫描不相关目录所浪费的时间,但初始化的类路径扫描部分仍需要大约1-2分钟。

I have already narrowed the number of directories specified in the "base-package", to the minimum in order to reduce the time wasted in scanning irrelevant directories, but the class-path scanning part of initialization still takes about 1-2 mins.

那么,有没有办法优化扫描过程?我已经考虑过将候选类路径存储在一个文件中然后让容器从文件中获取它们,而不是每次启动都扫描类路径,但是我真的不知道从哪里开始或者甚至可能。

So, is there a way to optimize the scanning process ? I've thought of storing the candidate classes path in a file and make the container then get them from the file instead of scanning the class-path with every startup, but i don't really know where to start or if that is even possible.

非常感谢任何建议。提前致谢。

Any advice is much appreciated. Thanks in advance.

编辑:从自动生成的xml文件加载bean定义,将Spring自举时间减少到9~10秒,这证实使用了反射api通过Spring进行组件类路径扫描是启动延迟的主要来源。

至于生成xml文件,这里是代码,因为它可能对有相同问题的人有帮助。

Edit: Loading bean definitions form an autogenerated xml file, reduced the Spring bootstrap time to 9~10 secs which confirms that the reflection api used by Spring for the components class-path scanning is the major source of startup delays.
As for generating the xml file here is the code, since it might be helpful for someone with the same issues.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;


public class ConfigurationWriter {

    public ArrayList<String> beanDefinitions = new ArrayList<String>();

    public ConfigurationWriter() {

        // the context loaded with old fashioned way (classpath scanning)
        ApplicationContext context = SpringContainerServiceImpl.getInstance().getContext();
        String[] tab = context.getBeanDefinitionNames();
        for (int i = 0; i < tab.length - 6; i++) {
            Class clazz = context.getType(tab[i]);
            String scope = context.isPrototype(tab[i]) ? "prototype" : "singleton";
            String s = "<bean id=\"" + tab[i] + "\" class=\"" + clazz.getName() + "\" scope=\"" + scope + "\"/>";
            beanDefinitions.add(s);
        }
        // Collections.addAll(beanDefinitions, tab);

    }

    @SuppressWarnings("restriction")
    public void generateConfiguration() throws FileNotFoundException {
        File xmlConfig = new File("D:\\dev\\svn\\...\\...\\src\\test\\resources\\springBoost.xml");
        PrintWriter printer = new PrintWriter(xmlConfig);

        generateHeader(printer);

        generateCorpse(printer);

        generateTail(printer);

        printer.checkError();

    }

    @SuppressWarnings("restriction")
    private void generateCorpse(PrintWriter printer) {

        for (String beanPath : beanDefinitions) {
            printer.println(beanPath);
        }

    }

    @SuppressWarnings("restriction")
    private void generateHeader(PrintWriter printer) {
        printer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        printer.println("<beans xmlns=\"http://www.springframework.org/schema/beans\"");
        printer.println("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        printer.println("xmlns:context=\"http://www.springframework.org/schema/context\"");
        printer.println("xsi:schemaLocation=\"");
        printer.println("http://www.springframework.org/schema/mvc");
        printer.println("http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd");
        printer.println("http://www.springframework.org/schema/beans");
        printer.println("http://www.springframework.org/schema/beans/spring-beans-3.0.xsd");
        printer.println("http://www.springframework.org/schema/context");
        printer.println("http://www.springframework.org/schema/context/spring-context-3.0.xsd\"");
        printer.println("default-lazy-init=\"true\">");
    }

    @SuppressWarnings("restriction")
    private void generateTail(PrintWriter printer) {
        // printer.println("<bean class=\"com.xxx.frmwrk.spring.processors.xxxBeanFactoryPostProcessor\"/>");
        printer.println("<bean class=\"com.xxx.frmwrk.spring.processors.xxxPostProcessor\"/>");
        printer.println("</beans>");
    }

}


推荐答案


问题:目录中有多少(%)的类是Spring Beans?

Question: How many (in %) of the classes in the directories are Spring Beans?

答案:我不太确定(这是一个非常大的项目),但从我看到的情况来看,我认为它是90%到100%,因为xml和属性文件被隔离在不同的位置)

Answer: I'm not really sure (it's a really big project) , but from what i saw i believe it's arround 90 to 100%, since xml and properties files are isolated in separate locations)

如果问题实际上是组件扫描而不是bean初始化过程本身(我非常怀疑),那么我能想象的唯一解决方案是使用Spring XML配置而不是组件扫描。 - (可以自动创建XML文件)。

If the problem is really the component scan and not the bean initializing process itself (and I highly doubt that), then the only solution I can imagine is to use Spring XML configuration instead of component scan. - (May you can create the XML file automatically).

但是如果你有很多课程并且90% - 其中100%是豆类,那么,扫描的减少文件的最大改进率为10%-0%。

But if you have many classes and 90% - 100% of them are Beans, then, the reduction of scanned files will have a maximal improvement of 10%-0%.

您应该尝试其他方法来加速初始化,可能使用延迟加载或任何延迟加载相关技术,或者(并不是一个笑话)使用更快的硬件(如果它不是一个独立的应用程序)。

You should try other ways to speed up your initialization, may using lazy loading or any lazy loading related techniques, or (and that is not a joke) use faster hardware (if it is not a stand alone application).

A生成Spring XML的简单方法是编写一个简单的spring应用程序,它像原始应用程序一样使用类路径扫描。在所有Bean初始化之后,它在Spring上下文中遍历Bean,检查bean是否属于重要包,并在文件中写入此Bean的XML Config。

A easy way to generate the Spring XML is to write a simple spring application that uses the class path scanning like your original application. After all Beans are initialize, it iterates through the Beans in the Spring Context, check if the bean belongs to the important package and write the XML Config for this bean in a file.

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

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