Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does performance of Spring component-scan depend on the size of the scanned package?

Tags:

java

spring

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?

like image 728
Paperback Writer Avatar asked Oct 11 '13 09:10

Paperback Writer


1 Answers

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.

like image 136
Evgeni Dimitrov Avatar answered Sep 17 '22 17:09

Evgeni Dimitrov