diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java index 8385b9ef34e..8f019f10ff5 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java @@ -286,8 +286,8 @@ class ConfigurationClassBeanDefinitionReader { } if (logger.isTraceEnabled()) { - logger.trace(String.format("Registering bean definition for @Bean method %s.%s()", - configClass.getMetadata().getClassName(), beanName)); + logger.trace("Registering bean definition for @Bean method %s.%s()" + .formatted(configClass.getMetadata().getClassName(), beanName)); } this.registry.registerBeanDefinition(beanName, beanDefToRegister); } @@ -344,9 +344,8 @@ class ConfigurationClassBeanDefinitionReader { "@Bean definition illegally overridden by existing bean definition: " + existingBeanDef); } if (logger.isDebugEnabled()) { - logger.debug(String.format("Skipping bean definition for %s: a definition for bean '%s' " + - "already exists. This top-level bean definition is considered as an override.", - beanMethod, beanName)); + logger.debug("Skipping bean definition for %s: a definition for bean '%s' already exists. " + + "This top-level bean definition is considered as an override.".formatted(beanMethod, beanName)); } return true; } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ImportResource.java b/spring-context/src/main/java/org/springframework/context/annotation/ImportResource.java index da1cb97b8e4..3bdf24bac1c 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ImportResource.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ImportResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -29,14 +29,15 @@ import org.springframework.core.annotation.AliasFor; * Indicates one or more resources containing bean definitions to import. * *
Like {@link Import @Import}, this annotation provides functionality similar to
- * the {@code
By default, arguments to the {@link #value} attribute will be processed using a + *
By default, arguments to the {@link #locations() locations} or {@link #value() value}
+ * attribute will be processed using a
* {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader GroovyBeanDefinitionReader}
- * if ending in {@code ".groovy"}; otherwise, an
+ * for resource locations ending in {@code ".groovy"}; otherwise, an
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader XmlBeanDefinitionReader}
* will be used to parse Spring {@code
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 GroovyBeanDefinitionReader};
- * whereas, all other resources will be processed with an
- * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader XmlBeanDefinitionReader}.
+ * {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
+ * GroovyBeanDefinitionReader}; whereas, all other resources will be processed
+ * with an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader
+ * XmlBeanDefinitionReader}.
+ * @see #locations
* @see #value
*/
Class extends BeanDefinitionReader> reader() default BeanDefinitionReader.class;
diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportResourceTests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportResourceTests.java
index 565b58e9d80..b3811088ee7 100644
--- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportResourceTests.java
+++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportResourceTests.java
@@ -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 @@
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
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;
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.