|
|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2024 the original author or authors. |
|
|
|
|
* Copyright 2002-2025 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. |
|
|
|
|
@ -16,8 +16,6 @@
@@ -16,8 +16,6 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.context.annotation.configuration; |
|
|
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
|
|
import org.aspectj.lang.annotation.Before; |
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
@ -30,13 +28,12 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
@@ -30,13 +28,12 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
import org.springframework.context.annotation.ImportResource; |
|
|
|
|
import org.springframework.core.env.MapPropertySource; |
|
|
|
|
import org.springframework.core.env.PropertySource; |
|
|
|
|
import org.springframework.core.testfixture.env.MockPropertySource; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Integration tests for {@link ImportResource} support. |
|
|
|
|
* Integration tests for {@link ImportResource @ImportResource} support. |
|
|
|
|
* |
|
|
|
|
* @author Chris Beams |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
@ -45,81 +42,81 @@ import static org.assertj.core.api.Assertions.assertThat;
@@ -45,81 +42,81 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
class ImportResourceTests { |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void importXml() { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlConfig.class); |
|
|
|
|
assertThat(ctx.containsBean("javaDeclaredBean")).as("did not contain java-declared bean").isTrue(); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).as("did not contain xml-declared bean").isTrue(); |
|
|
|
|
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); |
|
|
|
|
assertThat(tb.getName()).isEqualTo("myName"); |
|
|
|
|
ctx.close(); |
|
|
|
|
void importResource() { |
|
|
|
|
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlConfig.class)) { |
|
|
|
|
assertThat(ctx.containsBean("javaDeclaredBean")).as("did not contain java-declared bean").isTrue(); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).as("did not contain xml-declared bean").isTrue(); |
|
|
|
|
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); |
|
|
|
|
assertThat(tb.getName()).isEqualTo("myName"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void importXmlIsInheritedFromSuperclassDeclarations() { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FirstLevelSubConfig.class); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).isTrue(); |
|
|
|
|
ctx.close(); |
|
|
|
|
void importResourceIsInheritedFromSuperclassDeclarations() { |
|
|
|
|
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FirstLevelSubConfig.class)) { |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).isTrue(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void importXmlIsMergedFromSuperclassDeclarations() { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SecondLevelSubConfig.class); |
|
|
|
|
assertThat(ctx.containsBean("secondLevelXmlDeclaredBean")).as("failed to pick up second-level-declared XML bean").isTrue(); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).as("failed to pick up parent-declared XML bean").isTrue(); |
|
|
|
|
ctx.close(); |
|
|
|
|
void importResourceIsMergedFromSuperclassDeclarations() { |
|
|
|
|
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SecondLevelSubConfig.class)) { |
|
|
|
|
assertThat(ctx.containsBean("secondLevelXmlDeclaredBean")).as("failed to pick up second-level-declared XML bean").isTrue(); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).as("failed to pick up parent-declared XML bean").isTrue(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void importXmlWithNamespaceConfig() { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithAopNamespaceConfig.class); |
|
|
|
|
Object bean = ctx.getBean("proxiedXmlBean"); |
|
|
|
|
assertThat(AopUtils.isAopProxy(bean)).isTrue(); |
|
|
|
|
ctx.close(); |
|
|
|
|
void importResourceWithNamespaceConfig() { |
|
|
|
|
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithAopNamespaceConfig.class)) { |
|
|
|
|
Object bean = ctx.getBean("proxiedXmlBean"); |
|
|
|
|
assertThat(AopUtils.isAopProxy(bean)).isTrue(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void importXmlWithOtherConfigurationClass() { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithConfigurationClass.class); |
|
|
|
|
assertThat(ctx.containsBean("javaDeclaredBean")).as("did not contain java-declared bean").isTrue(); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).as("did not contain xml-declared bean").isTrue(); |
|
|
|
|
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); |
|
|
|
|
assertThat(tb.getName()).isEqualTo("myName"); |
|
|
|
|
ctx.close(); |
|
|
|
|
void importResourceWithOtherConfigurationClass() { |
|
|
|
|
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithConfigurationClass.class)) { |
|
|
|
|
assertThat(ctx.containsBean("javaDeclaredBean")).as("did not contain java-declared bean").isTrue(); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).as("did not contain xml-declared bean").isTrue(); |
|
|
|
|
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); |
|
|
|
|
assertThat(tb.getName()).isEqualTo("myName"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void importWithPlaceholder() { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
|
|
|
PropertySource<?> propertySource = new MapPropertySource("test", |
|
|
|
|
Collections.<String, Object> singletonMap("test", "springframework")); |
|
|
|
|
ctx.getEnvironment().getPropertySources().addFirst(propertySource); |
|
|
|
|
ctx.register(ImportXmlConfig.class); |
|
|
|
|
ctx.refresh(); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).as("did not contain xml-declared bean").isTrue(); |
|
|
|
|
ctx.close(); |
|
|
|
|
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) { |
|
|
|
|
ctx.getEnvironment().getPropertySources().addFirst(new MockPropertySource("test").withProperty("test", "springframework")); |
|
|
|
|
ctx.register(ImportXmlConfig.class); |
|
|
|
|
ctx.refresh(); |
|
|
|
|
assertThat(ctx.containsBean("xmlDeclaredBean")).as("did not contain xml-declared bean").isTrue(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void importXmlWithAutowiredConfig() { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class); |
|
|
|
|
String name = ctx.getBean("xmlBeanName", String.class); |
|
|
|
|
assertThat(name).isEqualTo("xml.declared"); |
|
|
|
|
ctx.close(); |
|
|
|
|
void importResourceWithAutowiredConfig() { |
|
|
|
|
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class)) { |
|
|
|
|
String name = ctx.getBean("xmlBeanName", String.class); |
|
|
|
|
assertThat(name).isEqualTo("xml.declared"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void importNonXmlResource() { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class); |
|
|
|
|
assertThat(ctx.containsBean("propertiesDeclaredBean")).isTrue(); |
|
|
|
|
ctx.close(); |
|
|
|
|
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class)) { |
|
|
|
|
assertThat(ctx.containsBean("propertiesDeclaredBean")).isTrue(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml") |
|
|
|
|
static class ImportXmlConfig { |
|
|
|
|
|
|
|
|
|
@Value("${name}") |
|
|
|
|
private String name; |
|
|
|
|
|
|
|
|
|
@Bean public TestBean javaDeclaredBean() { |
|
|
|
|
return new TestBean(this.name); |
|
|
|
|
} |
|
|
|
|
@ -146,6 +143,7 @@ class ImportResourceTests {
@@ -146,6 +143,7 @@ class ImportResourceTests {
|
|
|
|
|
|
|
|
|
|
@Aspect |
|
|
|
|
static class AnAspect { |
|
|
|
|
|
|
|
|
|
@Before("execution(* org.springframework.beans.testfixture.beans.TestBean.*(..))") |
|
|
|
|
public void advice() { } |
|
|
|
|
} |
|
|
|
|
@ -158,16 +156,19 @@ class ImportResourceTests {
@@ -158,16 +156,19 @@ class ImportResourceTests {
|
|
|
|
|
@Configuration |
|
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml") |
|
|
|
|
static class ImportXmlAutowiredConfig { |
|
|
|
|
@Autowired TestBean xmlDeclaredBean; |
|
|
|
|
|
|
|
|
|
@Bean public String xmlBeanName() { |
|
|
|
|
@Autowired |
|
|
|
|
TestBean xmlDeclaredBean; |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
public String xmlBeanName() { |
|
|
|
|
return xmlDeclaredBean.getName(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
|
@Configuration |
|
|
|
|
@ImportResource(locations = "classpath:org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context.properties", |
|
|
|
|
@ImportResource(locations = "org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig.properties", |
|
|
|
|
reader = org.springframework.beans.factory.support.PropertiesBeanDefinitionReader.class) |
|
|
|
|
static class ImportNonXmlResourceConfig { |
|
|
|
|
} |
|
|
|
|
|