问题
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?
回答1:
I'm currently encountering the same problem with a signed uber-jar. It might not have something to do with auto-wiring but with context component scanning, in case you are using this in an XML configuration too. I've found an old Spring core ticket mentioning that. The ticket was closed with Resolution: Won't Fix though...
I have removed component scanning and replaced it with XML beans and it seems to be working. I still have problems with CXF web services generated at runtime using introspection (org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean). Each service needs around 2 minutes to be created. But at least some progress was made.
Something else worth noting: the application is slow with Java Web Start on Java 9 although it's super fast with 1.8
回答2:
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.
来源:https://stackoverflow.com/questions/47095448/spring-component-scan-autowire-in-signed-jar-files-is-slow