Introduce alias for 'value' attribute in @ImportResource

Issue: SPR-11393
This commit is contained in:
Sam Brannen 2015-06-03 21:20:34 -04:00
parent e5dc6e964c
commit d3b5aeb768
3 changed files with 46 additions and 21 deletions

View File

@ -94,6 +94,7 @@ import org.springframework.util.StringUtils;
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Phillip Webb * @author Phillip Webb
* @author Sam Brannen
* @since 3.0 * @since 3.0
* @see ConfigurationClassBeanDefinitionReader * @see ConfigurationClassBeanDefinitionReader
*/ */
@ -277,7 +278,7 @@ class ConfigurationClassParser {
// Process any @ImportResource annotations // Process any @ImportResource annotations
if (sourceClass.getMetadata().isAnnotated(ImportResource.class.getName())) { if (sourceClass.getMetadata().isAnnotated(ImportResource.class.getName())) {
AnnotationAttributes importResource = AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class); AnnotationAttributes importResource = AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
String[] resources = importResource.getStringArray("value"); String[] resources = importResource.getAliasedStringArray("locations", ImportResource.class, sourceClass);
Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader"); Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
for (String resource : resources) { for (String resource : resources) {
String resolvedResource = this.environment.resolveRequiredPlaceholders(resource); String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);

View File

@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.core.annotation.AliasFor;
/** /**
* Indicates one or more resources containing bean definitions to import. * Indicates one or more resources containing bean definitions to import.
@ -34,8 +35,9 @@ import org.springframework.beans.factory.support.BeanDefinitionReader;
* as namespaces is still necessary. * as namespaces is still necessary.
* *
* <p>By default, arguments to the {@link #value} attribute will be processed using a * <p>By default, arguments to the {@link #value} attribute will be processed using a
* {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader} if ending in * {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader GroovyBeanDefinitionReader}
* {@code ".groovy"}; otherwise, an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader} * if ending in {@code ".groovy"}; otherwise, an
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader XmlBeanDefinitionReader}
* will be used to parse Spring {@code <beans/>} XML files. Optionally, the {@link #reader} * will be used to parse Spring {@code <beans/>} XML files. Optionally, the {@link #reader}
* attribute may be declared, allowing the user to choose a custom {@link BeanDefinitionReader} * attribute may be declared, allowing the user to choose a custom {@link BeanDefinitionReader}
* implementation. * implementation.
@ -53,23 +55,34 @@ import org.springframework.beans.factory.support.BeanDefinitionReader;
public @interface ImportResource { public @interface ImportResource {
/** /**
* Resource locations from which to import. * Alias for {@link #locations}.
* <p>Supports resource-loading prefixes such as {@code classpath:}, {@code file:}, * @see #locations
* etc.
* <p>Consult the Javadoc for {@link #reader} for details on how resources
* will be processed.
* @see #reader * @see #reader
*/ */
String[] value(); @AliasFor(attribute = "locations")
String[] value() default {};
/** /**
* {@link BeanDefinitionReader} implementation to use when processing resources * Resource locations from which to import.
* specified via the {@link #value} attribute. * <p>Supports resource-loading prefixes such as {@code classpath:},
* {@code file:}, etc.
* <p>Consult the Javadoc for {@link #reader} for details on how resources
* will be processed.
* @since 4.2
* @see #value
* @see #reader
*/
@AliasFor(attribute = "value")
String[] locations() default {};
/**
* {@link BeanDefinitionReader} implementation to use when processing
* resources specified via the {@link #value} attribute.
* <p>By default, the reader will be adapted to the resource path specified: * <p>By default, the reader will be adapted to the resource path specified:
* {@code ".groovy"} files will be processed with a * {@code ".groovy"} files will be processed with a
* {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader}; * {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader GroovyBeanDefinitionReader};
* whereas, all other resources will be processed with an * whereas, all other resources will be processed with an
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}. * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader XmlBeanDefinitionReader}.
* @see #value * @see #value
*/ */
Class<? extends BeanDefinitionReader> reader() default BeanDefinitionReader.class; Class<? extends BeanDefinitionReader> reader() default BeanDefinitionReader.class;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.util.Collections;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -28,7 +29,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader; import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -45,6 +45,7 @@ import static org.junit.Assert.*;
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
*/ */
public class ImportResourceTests { public class ImportResourceTests {
@ -55,6 +56,7 @@ public class ImportResourceTests {
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class);
assertEquals("myName", tb.getName()); assertEquals("myName", tb.getName());
ctx.close();
} }
@Ignore // TODO: SPR-6310 @Ignore // TODO: SPR-6310
@ -65,19 +67,23 @@ public class ImportResourceTests {
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class);
assertEquals("myName", tb.getName()); assertEquals("myName", tb.getName());
ctx.close();
} }
@Ignore // TODO: SPR-6310 @Ignore // TODO: SPR-6310
@Test @Test
public void testImportXmlByConvention() { public void testImportXmlByConvention() {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlByConventionConfig.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ImportXmlByConventionConfig.class);
assertTrue("context does not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); assertTrue("context does not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
ctx.close();
} }
@Test @Test
public void testImportXmlIsInheritedFromSuperclassDeclarations() { public void testImportXmlIsInheritedFromSuperclassDeclarations() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FirstLevelSubConfig.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FirstLevelSubConfig.class);
assertTrue(ctx.containsBean("xmlDeclaredBean")); assertTrue(ctx.containsBean("xmlDeclaredBean"));
ctx.close();
} }
@Test @Test
@ -85,6 +91,7 @@ public class ImportResourceTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SecondLevelSubConfig.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SecondLevelSubConfig.class);
assertTrue("failed to pick up second-level-declared XML bean", ctx.containsBean("secondLevelXmlDeclaredBean")); assertTrue("failed to pick up second-level-declared XML bean", ctx.containsBean("secondLevelXmlDeclaredBean"));
assertTrue("failed to pick up parent-declared XML bean", ctx.containsBean("xmlDeclaredBean")); assertTrue("failed to pick up parent-declared XML bean", ctx.containsBean("xmlDeclaredBean"));
ctx.close();
} }
@Test @Test
@ -92,6 +99,7 @@ public class ImportResourceTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithAopNamespaceConfig.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithAopNamespaceConfig.class);
Object bean = ctx.getBean("proxiedXmlBean"); Object bean = ctx.getBean("proxiedXmlBean");
assertTrue(AopUtils.isAopProxy(bean)); assertTrue(AopUtils.isAopProxy(bean));
ctx.close();
} }
@Test @Test
@ -101,6 +109,7 @@ public class ImportResourceTests {
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class);
assertEquals("myName", tb.getName()); assertEquals("myName", tb.getName());
ctx.close();
} }
@Ignore // TODO: SPR-6327 @Ignore // TODO: SPR-6327
@ -109,6 +118,7 @@ public class ImportResourceTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SubResourceConfig.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SubResourceConfig.class);
assertTrue(ctx.containsBean("propertiesDeclaredBean")); assertTrue(ctx.containsBean("propertiesDeclaredBean"));
assertTrue(ctx.containsBean("xmlDeclaredBean")); assertTrue(ctx.containsBean("xmlDeclaredBean"));
ctx.close();
} }
@Test @Test
@ -120,6 +130,7 @@ public class ImportResourceTests {
ctx.register(ImportXmlConfig.class); ctx.register(ImportXmlConfig.class);
ctx.refresh(); ctx.refresh();
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
ctx.close();
} }
@Test @Test
@ -127,12 +138,14 @@ public class ImportResourceTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class);
String name = ctx.getBean("xmlBeanName", String.class); String name = ctx.getBean("xmlBeanName", String.class);
assertThat(name, equalTo("xml.declared")); assertThat(name, equalTo("xml.declared"));
ctx.close();
} }
@Test @Test
public void testImportNonXmlResource() { public void testImportNonXmlResource() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class);
assertTrue(ctx.containsBean("propertiesDeclaredBean")); assertTrue(ctx.containsBean("propertiesDeclaredBean"));
ctx.close();
} }
@ -190,7 +203,7 @@ public class ImportResourceTests {
} }
@Configuration @Configuration
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml") @ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml")
static class ImportXmlAutowiredConfig { static class ImportXmlAutowiredConfig {
@Autowired TestBean xmlDeclaredBean; @Autowired TestBean xmlDeclaredBean;
@ -200,14 +213,12 @@ public class ImportResourceTests {
} }
@Configuration @Configuration
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context.properties", @ImportResource(locations = "classpath:org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context.properties", reader = PropertiesBeanDefinitionReader.class)
reader=PropertiesBeanDefinitionReader.class)
static class ImportNonXmlResourceConfig { static class ImportNonXmlResourceConfig {
} }
@Configuration @Configuration
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml", @ImportResource(locations = "classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml", reader = XmlBeanDefinitionReader.class)
reader=XmlBeanDefinitionReader.class)
static class SubResourceConfig extends ImportNonXmlResourceConfig { static class SubResourceConfig extends ImportNonXmlResourceConfig {
} }