10

In our application we are experiencing performance problems with component-scan. It is quite slow and its performance does not depend on number of classes in the scanned package.

Each line like this in our sping configuration file:

<context:component-scan base-package="foo.bar" />

adds 2 minutes to startup time of our application. Number of classes in the scanned package does not matter - we are experiencing the same delay both for packages with 10 and 1k classes.

Why performance of component-scan does not depend on the size of the scanned package?

We are using mixed approach for creating spring beans - we use both xml definitions and component scan. Can this be the reason for such a behavior?

Paperback Writer
  • 1,995
  • 2
  • 16
  • 27
  • I remember having that kind of problems when using AOP, and the solution was to set as deeply package as we can – RamonBoza Oct 11 '13 at 09:41
  • 1
    This can help http://stackoverflow.com/questions/5947713/spring-startup-performance-issues – Pratik Shelar Oct 11 '13 at 09:43
  • @RamonBoza We are already using as deep packages as possible. As I have written, we are experiencing the same slowdown for big and small packages. – Paperback Writer Oct 11 '13 at 09:44
  • @Pratik Thanks for the link, but it only explains how to solve the problem by removing component-scan and doing all configuration by xml. I would like to understand my problem and (if possible) solve it while stil using component-scan . – Paperback Writer Oct 14 '13 at 08:19

1 Answers1

3

What Spring does is to go through all the classes in the packages you give in the component-scan and if the class has Component, Repository or Service it registers a bean in the context. So the number of classes matters. You have to scan only packages witch contains annotated classes(scanning non annotated classes will take time too). Also you can use only one component scan tag and list all the packages. Using both xml and component scan should not be a problem as long as they don't duplicate.

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • Are you sure that Spring scans only classes in the specified package? One implementation, which comes to my mind is scanning ALL classes in the context, next picking only those which come from the specified package, next picking only those with the appropriate annotations. – Paperback Writer Oct 14 '13 at 08:15
  • You can try it... Remove some package in the component scan that contains package with annotated classes and check if they are available in the context. Also refer to http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s12.html (chapter 4.12.2 Auto-detecting components)"To autodetect these classes and register the corresponding beans requires the inclusion of the following element in XML where 'basePackage' would be a common parent package for the two classes (or alternatively a comma-separated list could be specified that included the parent package of each class)." – Evgeni Dimitrov Oct 14 '13 at 09:07