Spring startup performance issues

大兔子大兔子 提交于 2019-11-28 03:40:46

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

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)

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).

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 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.

Auto discovery of annotated classes currently requires to scan all classes in the specified package(s) and can take a long time, a known problem of the current class loading mechanism.

Java 9 is going to help here with Jigsaw.

From the Java Platform Module System requirements by Mark Reinold, http://openjdk.java.net/projects/jigsaw/spec/reqs/ :

Efficient annotation detection — It must be possible to identify all of the class files in a module artifact in which a particular annotation is present without actually reading all of the class files. At run time it must be possible to identify all of the classes in a loaded module in which a particular annotation is present without enumerating all of the classes in the module, so long as the annotation was retained for run time. For efficiency it may be necessary to specify that only certain annotations need to be detectable in this manner. One potential approach is to augment a module’s definition with an index of the annotations that are present in the module, together with an indication of the elements to which each annotation applies. To limit the size of the index, only annotations which themselves are annotated with a new meta-annotation, say @Indexed, would be included.

abalogh

Not much you can do about the performance there, I guess you aren't concerned about the startup in production environment, but the startup time of your tests*. Two tips:

  • Review that your test-appcontext only uses the minimally required components of your app
  • instead of having a list of component-scan directives, use one, with a comma-separated value like this: base-package="com.package.one,com.package.two..."

The only thing that comes in my mind, beside reducing the directories to be scanned, is the use of lazy bean initialization. May this could help if you have a lot of beans

You could use Spring's Java-based container configuration instead of component scan.

In comparison to XML-based configuration the Java-based container configuration is type-safe.

But first of all you should check whether your component scan paths are specific enough so that they do not include classes of third party libraries.

I know it is an old question, and as you will see the situation was different at that time, but hopefully it can help others researching this issue as I did.

According to this answer to a different question, The @ComponentScan annotation now supports a lazyInit flag, which should help in reducing start-up time.

https://stackoverflow.com/a/29832836/4266381

Note: Your edit made it sound like switching to XML by itself was the magic. Yet, looking closer at the code, you had default-lazy-init="true". I wonder if that was the true reason.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!