diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/domain/EntityScanPackages.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/domain/EntityScanPackages.java index 9c652a1513..0080a5f013 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/domain/EntityScanPackages.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/domain/EntityScanPackages.java @@ -155,7 +155,8 @@ public class EntityScanPackages { private Set getPackagesToScan(AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap( metadata.getAnnotationAttributes(EntityScan.class.getName())); - String[] basePackages = attributes.getStringArray("basePackages"); + String[] basePackages = attributes.getAliasedStringArray("basePackages", + EntityScan.class, metadata.getClassName()); Class[] basePackageClasses = attributes .getClassArray("basePackageClasses"); Set packagesToScan = new LinkedHashSet(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/domain/EntityScanPackagesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/domain/EntityScanPackagesTests.java index 0310919982..0dac3bec35 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/domain/EntityScanPackagesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/domain/EntityScanPackagesTests.java @@ -24,6 +24,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.AnnotationConfigurationException; @@ -112,6 +113,17 @@ public class EntityScanPackagesTests { assertThat(packages.getPackageNames()).containsExactly("a"); } + @Test + public void entityScanAnnotationWhenHasValueAttributeShouldSetupPackagesAsm() + throws Exception { + this.context = new AnnotationConfigApplicationContext(); + this.context.registerBeanDefinition("entityScanValueConfig", + new RootBeanDefinition(EntityScanValueConfig.class.getName())); + this.context.refresh(); + EntityScanPackages packages = EntityScanPackages.get(this.context); + assertThat(packages.getPackageNames()).containsExactly("a"); + } + @Test public void entityScanAnnotationWhenHasBasePackagesAttributeShouldSetupPackages() throws Exception { diff --git a/spring-boot/src/main/java/org/springframework/boot/orm/jpa/EntityScanRegistrar.java b/spring-boot/src/main/java/org/springframework/boot/orm/jpa/EntityScanRegistrar.java index ba7f04513c..21f401a4e2 100644 --- a/spring-boot/src/main/java/org/springframework/boot/orm/jpa/EntityScanRegistrar.java +++ b/spring-boot/src/main/java/org/springframework/boot/orm/jpa/EntityScanRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,7 +63,8 @@ class EntityScanRegistrar implements ImportBeanDefinitionRegistrar { private Set getPackagesToScan(AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes .fromMap(metadata.getAnnotationAttributes(EntityScan.class.getName())); - String[] basePackages = attributes.getStringArray("basePackages"); + String[] basePackages = attributes.getAliasedStringArray("basePackages", + EntityScan.class, metadata.getClassName()); Class[] basePackageClasses = attributes.getClassArray("basePackageClasses"); Set packagesToScan = new LinkedHashSet(); packagesToScan.addAll(Arrays.asList(basePackages)); diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java index f3c6b98ac7..78efa6d9a8 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java @@ -76,7 +76,8 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar { private Set getPackagesToScan(AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap( metadata.getAnnotationAttributes(ServletComponentScan.class.getName())); - String[] basePackages = attributes.getStringArray("basePackages"); + String[] basePackages = attributes.getAliasedStringArray("basePackages", + ServletComponentScan.class, metadata.getClassName()); Class[] basePackageClasses = attributes.getClassArray("basePackageClasses"); Set packagesToScan = new LinkedHashSet(); packagesToScan.addAll(Arrays.asList(basePackages)); diff --git a/spring-boot/src/test/java/org/springframework/boot/orm/jpa/EntityScanTests.java b/spring-boot/src/test/java/org/springframework/boot/orm/jpa/EntityScanTests.java index 8db0ad6ea3..f41931e6b1 100644 --- a/spring-boot/src/test/java/org/springframework/boot/orm/jpa/EntityScanTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/orm/jpa/EntityScanTests.java @@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -61,6 +62,15 @@ public class EntityScanTests { assertSetPackagesToScan("com.mycorp.entity"); } + @Test + public void simpleValueAsm() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + this.context.registerBeanDefinition("valueConfig", + new RootBeanDefinition(ValueConfig.class.getName())); + this.context.refresh(); + assertSetPackagesToScan("com.mycorp.entity"); + } + @Test public void needsEntityManageFactory() throws Exception { this.thrown.expect(IllegalStateException.class); diff --git a/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java b/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java index 6fb6194459..5b1b6d2c80 100644 --- a/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java @@ -21,6 +21,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.AnnotationConfigurationException; @@ -57,6 +58,18 @@ public class ServletComponentScanRegistrarTests { "com.example.bar"); } + @Test + public void packagesConfiguredWithValueAsm() { + this.context = new AnnotationConfigApplicationContext(); + this.context.registerBeanDefinition("valuePackages", + new RootBeanDefinition(ValuePackages.class.getName())); + this.context.refresh(); + ServletComponentRegisteringPostProcessor postProcessor = this.context + .getBean(ServletComponentRegisteringPostProcessor.class); + assertThat(postProcessor.getPackagesToScan()).contains("com.example.foo", + "com.example.bar"); + } + @Test public void packagesConfiguredWithBackPackages() { this.context = new AnnotationConfigApplicationContext(BasePackages.class);