Merge pull request #27355 from saraswathy-krish

* pr/27355:
  Polish "Support comma-separated basePackage values in EntityScan"
  Support comma-separated basePackage values in EntityScan

Closes gh-27355
pull/27405/head
Madhura Bhave 3 years ago
commit e61b8707c1

@ -30,6 +30,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -141,10 +142,13 @@ public class EntityScanPackages {
.fromMap(metadata.getAnnotationAttributes(EntityScan.class.getName())); .fromMap(metadata.getAnnotationAttributes(EntityScan.class.getName()));
Set<String> packagesToScan = new LinkedHashSet<>(); Set<String> packagesToScan = new LinkedHashSet<>();
for (String basePackage : attributes.getStringArray("basePackages")) { for (String basePackage : attributes.getStringArray("basePackages")) {
addResolvedPackage(basePackage, packagesToScan); String[] tokenized = StringUtils.tokenizeToStringArray(
this.environment.resolvePlaceholders(basePackage),
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
Collections.addAll(packagesToScan, tokenized);
} }
for (Class<?> basePackageClass : attributes.getClassArray("basePackageClasses")) { for (Class<?> basePackageClass : attributes.getClassArray("basePackageClasses")) {
addResolvedPackage(ClassUtils.getPackageName(basePackageClass), packagesToScan); packagesToScan.add(this.environment.resolvePlaceholders(ClassUtils.getPackageName(basePackageClass)));
} }
if (packagesToScan.isEmpty()) { if (packagesToScan.isEmpty()) {
String packageName = ClassUtils.getPackageName(metadata.getClassName()); String packageName = ClassUtils.getPackageName(metadata.getClassName());
@ -154,10 +158,6 @@ public class EntityScanPackages {
return packagesToScan; return packagesToScan;
} }
private void addResolvedPackage(String packageName, Set<String> packagesToScan) {
packagesToScan.add(this.environment.resolvePlaceholders(packageName));
}
} }
static class EntityScanPackagesBeanDefinition extends GenericBeanDefinition { static class EntityScanPackagesBeanDefinition extends GenericBeanDefinition {

@ -119,6 +119,20 @@ class EntityScannerTests {
assertThat(annotationTypeFilter.getValue().getAnnotationType()).isEqualTo(Entity.class); assertThat(annotationTypeFilter.getValue().getAnnotationType()).isEqualTo(Entity.class);
} }
@Test
void scanShouldScanCommaSeparatedPackagesInPlaceholderPackage() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
TestPropertyValues.of(
"com.example.entity-package=org.springframework.boot.autoconfigure.domain.scan.a,org.springframework.boot.autoconfigure.domain.scan.b")
.applyTo(context);
context.register(ScanPlaceholderConfig.class);
context.refresh();
EntityScanner scanner = new EntityScanner(context);
Set<Class<?>> scanned = scanner.scan(Entity.class);
assertThat(scanned).containsOnly(EntityA.class, EntityB.class);
context.close();
}
private static class TestEntityScanner extends EntityScanner { private static class TestEntityScanner extends EntityScanner {
private final ClassPathScanningCandidateComponentProvider candidateComponentProvider; private final ClassPathScanningCandidateComponentProvider candidateComponentProvider;

Loading…
Cancel
Save