Refine AliasFor usage
This commit makes sure to use `getAliasedStringArray` rather than `getStringArray` as the latter does not work with ASM. While this will probably be fixed in the core framework, this commit also adds dedicated tests with ASM to ensure that the code works as expected. Closes gh-6337
This commit is contained in:
parent
d4011687e4
commit
0356be7b95
|
@ -155,7 +155,8 @@ public class EntityScanPackages {
|
|||
private Set<String> 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<String> packagesToScan = new LinkedHashSet<String>();
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<String> 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<String> packagesToScan = new LinkedHashSet<String>();
|
||||
packagesToScan.addAll(Arrays.asList(basePackages));
|
||||
|
|
|
@ -76,7 +76,8 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
|
|||
private Set<String> 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<String> packagesToScan = new LinkedHashSet<String>();
|
||||
packagesToScan.addAll(Arrays.asList(basePackages));
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue