Spring is taking about 5 to 10 seconds to configure it self, I am using XML for infrastructure beans, and component scanning with annotations for everything else.
Does Spring JavaConfig eliminate the need for component scanning and all the reflection that happens to auto-wire beans by type? Has anyone seen an improvement in start-up time with JavaConfig?
My main concern is to speed up integration tests.
If you continue using
@ComponentScan, obviously you’ll see no improvement since the same scanning will occur, you are just enabling it using different format.On the other hand if you define all your beans using
@Beanannotation and perform wiring manually by calling other@Beanmethods – well, you’ll avoid component scanning cost, but this will bring you back to the ’90, except that you traded XML for Java (kind of improvement). Also remember that Java configuration has some cost as well. Each@Configurationclass needs CGLIB proxy for reasons beyond the scope of this question.What I am typically doing is I use
@Beanfor infrastructure beans (that I don’t have control over) and@ComponentScanfor everything else. Are you sure it is the component scanning that causes the delay and not Hibernate or some other third-party library bootup time? If it is scanning, well, you are trading startup time for developer comfort (no need to manually declare/wire everything manually).Of course there are ways to reduce component scanning time, mainly be carefully filtering packages/class names so that you only scan relevant classes.