Jonathan
Jonathan

Reputation: 21

Unit tests very slow because spring classpath scanning scans multiple times the same package

Using Spring 3 with spring data, I have a package com.company.repository with all classes related to spring data repository (about 50 classes).

The component scan is declared as :

<context:component-scan base-package="com.company"/>

In my unit tests, the step to scan spring components is very long (about 10 seconds). After enabled the trace log, I noted that the package "com.company.repository" is scanned multiple times. In fact, the method ClassPathScanningCandidateComponentProvider.findCandidateComponents("com.company.repository") is called several times with the same value. I don't understand why I have this behaviour. And I don't override it by a custom implementation (or I don't find how to do that). And I don't understand why the is no cache in the default implementation ?

Someone have an idea why I have this behaviour ? Is it normal ?

In complement, the following is the stack related to my case :

ClassPathScanningCandidateComponentProvider.findCandidateComponents(String) line: 224   
RepositoryBeanDefinitionBuilder.detectCustomImplementation(BeanDefinitionRegistry, ResourceLoader) line: 154    
RepositoryBeanDefinitionBuilder.registerCustomImplementation(BeanDefinitionRegistry, ResourceLoader) line: 116  
RepositoryBeanDefinitionBuilder.build(BeanDefinitionRegistry, ResourceLoader) line: 97  
RepositoryBeanDefinitionParser.registerGenericRepositoryFactoryBean(RepositoryConfiguration<XmlRepositoryConfigurationSource>, ParserContext) line: 101 
RepositoryBeanDefinitionParser.parse(Element, ParserContext) line: 71   
JpaRepositoryNameSpaceHandler(NamespaceHandlerSupport).parse(Element, ParserContext) line: 73   
BeanDefinitionParserDelegate.parseCustomElement(Element, BeanDefinition) line: 1419 
BeanDefinitionParserDelegate.parseCustomElement(Element) line: 1409 
DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(Element,  BeanDefinitionParserDelegate) line: 184  
...
XmlBeanDefinitionReader.doLoadBeanDefinitions(InputSource, Resource) line: 390

Upvotes: 2

Views: 915

Answers (1)

duardito
duardito

Reputation: 107

Maybe your tests are configured to create and destroy your spring context after each test. Check your tests base configuration.

Upvotes: 1

Related Questions