Introduce alias for 'value' attribute in @ImportResource
Issue: SPR-11393
This commit is contained in:
parent
e5dc6e964c
commit
d3b5aeb768
|
|
@ -94,6 +94,7 @@ import org.springframework.util.StringUtils;
|
|||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
* @since 3.0
|
||||
* @see ConfigurationClassBeanDefinitionReader
|
||||
*/
|
||||
|
|
@ -277,7 +278,7 @@ class ConfigurationClassParser {
|
|||
// Process any @ImportResource annotations
|
||||
if (sourceClass.getMetadata().isAnnotated(ImportResource.class.getName())) {
|
||||
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");
|
||||
for (String resource : resources) {
|
||||
String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* <p>By default, arguments to the {@link #value} attribute will be processed using a
|
||||
* {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader} if ending in
|
||||
* {@code ".groovy"}; otherwise, an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}
|
||||
* {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader GroovyBeanDefinitionReader}
|
||||
* 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}
|
||||
* attribute may be declared, allowing the user to choose a custom {@link BeanDefinitionReader}
|
||||
* implementation.
|
||||
|
|
@ -53,23 +55,34 @@ import org.springframework.beans.factory.support.BeanDefinitionReader;
|
|||
public @interface ImportResource {
|
||||
|
||||
/**
|
||||
* Resource locations from which to import.
|
||||
* <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.
|
||||
* Alias for {@link #locations}.
|
||||
* @see #locations
|
||||
* @see #reader
|
||||
*/
|
||||
String[] value();
|
||||
@AliasFor(attribute = "locations")
|
||||
String[] value() default {};
|
||||
|
||||
/**
|
||||
* {@link BeanDefinitionReader} implementation to use when processing resources
|
||||
* specified via the {@link #value} attribute.
|
||||
* Resource locations from which to import.
|
||||
* <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:
|
||||
* {@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
|
||||
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
|
||||
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader XmlBeanDefinitionReader}.
|
||||
* @see #value
|
||||
*/
|
||||
Class<? extends BeanDefinitionReader> reader() default BeanDefinitionReader.class;
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
* 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.Before;
|
||||
|
||||
import org.junit.Ignore;
|
||||
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.support.PropertiesBeanDefinitionReader;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
|
@ -45,6 +45,7 @@ import static org.junit.Assert.*;
|
|||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class ImportResourceTests {
|
||||
|
||||
|
|
@ -55,6 +56,7 @@ public class ImportResourceTests {
|
|||
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
|
||||
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class);
|
||||
assertEquals("myName", tb.getName());
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Ignore // TODO: SPR-6310
|
||||
|
|
@ -65,19 +67,23 @@ public class ImportResourceTests {
|
|||
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
|
||||
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class);
|
||||
assertEquals("myName", tb.getName());
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Ignore // TODO: SPR-6310
|
||||
@Test
|
||||
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"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImportXmlIsInheritedFromSuperclassDeclarations() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FirstLevelSubConfig.class);
|
||||
assertTrue(ctx.containsBean("xmlDeclaredBean"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -85,6 +91,7 @@ public class ImportResourceTests {
|
|||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SecondLevelSubConfig.class);
|
||||
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"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -92,6 +99,7 @@ public class ImportResourceTests {
|
|||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithAopNamespaceConfig.class);
|
||||
Object bean = ctx.getBean("proxiedXmlBean");
|
||||
assertTrue(AopUtils.isAopProxy(bean));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -101,6 +109,7 @@ public class ImportResourceTests {
|
|||
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
|
||||
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class);
|
||||
assertEquals("myName", tb.getName());
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Ignore // TODO: SPR-6327
|
||||
|
|
@ -109,6 +118,7 @@ public class ImportResourceTests {
|
|||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SubResourceConfig.class);
|
||||
assertTrue(ctx.containsBean("propertiesDeclaredBean"));
|
||||
assertTrue(ctx.containsBean("xmlDeclaredBean"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -120,6 +130,7 @@ public class ImportResourceTests {
|
|||
ctx.register(ImportXmlConfig.class);
|
||||
ctx.refresh();
|
||||
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -127,12 +138,14 @@ public class ImportResourceTests {
|
|||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class);
|
||||
String name = ctx.getBean("xmlBeanName", String.class);
|
||||
assertThat(name, equalTo("xml.declared"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImportNonXmlResource() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class);
|
||||
assertTrue(ctx.containsBean("propertiesDeclaredBean"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -190,7 +203,7 @@ public class ImportResourceTests {
|
|||
}
|
||||
|
||||
@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 {
|
||||
@Autowired TestBean xmlDeclaredBean;
|
||||
|
||||
|
|
@ -200,14 +213,12 @@ public class ImportResourceTests {
|
|||
}
|
||||
|
||||
@Configuration
|
||||
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context.properties",
|
||||
reader=PropertiesBeanDefinitionReader.class)
|
||||
@ImportResource(locations = "classpath:org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context.properties", reader = PropertiesBeanDefinitionReader.class)
|
||||
static class ImportNonXmlResourceConfig {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml",
|
||||
reader=XmlBeanDefinitionReader.class)
|
||||
@ImportResource(locations = "classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml", reader = XmlBeanDefinitionReader.class)
|
||||
static class SubResourceConfig extends ImportNonXmlResourceConfig {
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue