Spring Component Scan (@Autowire) in signed Jar Files is slow

Viewed 643

Years ago we had a problem with slow Spring component scans in standalone java applications so I asked in stackoverflow: Slow spring component scan . Years later I stumbled again about this problem and I think I found out why it is slow: It is because the jar files are signed. Currently we have about 170 jar files in our app. Our own and 3rd party. We sign all of them. Usually we use webstart and the performance for the component scan is ok. Starting our app with a huge classpath by just doing "java -cp mainclass" the compoennt scan takes minutes. If I just remove the MANIFEST.MF files from all jar files (which only contain the signing info), and run the application again the speed is fast as webstart. So it seems the signature check costs an awewful lot of time. I tried do deactivate Signature Scan in the Java console. No effect. Still slow. Are there java runtime parameters?

2 Answers

The solution is to create a candidate component index using spring-context-indexer. Add the spring-context-indexer as a compileOnly dependency in Gradle or register it as an annotation processor in your IDE.

See the documentation.

This will create a file called spring.components in the META-INF directory of your jar. Spring will automatically use this list of components instead of scanning every class of the entire jar.

Related