Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Treat directly registered class as configuration 'lite' mode candidate
Prior to this commit, if a non-annotated [1] class was registered directly with an ApplicationContext -- for example, via AnnotatedBeanDefinitionReader, AnnotationConfigApplicationContext, or @ContextConfiguration -- it was not considered a configuration class in 'lite' mode unless @bean methods were declared locally. In other words, if the registered class didn't declare local @bean methods but rather extended a class that declared @bean methods, then the registered class was not parsed as a @configuration class in 'lite' mode, and the @bean methods were ignored. Whereas, a non-annotated class registered via @import is always considered to be a configuration class candidate. To address this discrepancy between @import'ed classes and classes registered directly with an ApplicationContext, this commit treats any class registered via AnnotatedBeanDefinitionReader as a @configuration class candidate with @bean 'lite' mode semantics. [1] In this context, "non-annotated" means a class not annotated (or meta-annotated) with @component, @componentscan, @import, or @ImportResource. Closes gh-30449
- Loading branch information
Showing
3 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
spring-context/src/test/java/org/springframework/context/annotation/BeanLiteModeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2002-2023 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.context.annotation; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link Bean @Bean} 'lite' mode features that are not covered | ||
* elsewhere in the test suite. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.0.10 | ||
* @see ConfigurationClassPostProcessorTests | ||
*/ | ||
class BeanLiteModeTests { | ||
|
||
@Test | ||
void beanMethodsAreFoundWhenInheritedAsInterfaceDefaultMethods() { | ||
assertBeansAreFound(InterfaceDefaultMethodsConfig.class); | ||
} | ||
|
||
@Test | ||
void beanMethodsAreFoundWhenDeclaredLocally() { | ||
assertBeansAreFound(BaseConfig.class); | ||
} | ||
|
||
@Test | ||
void beanMethodsAreFoundWhenDeclaredLocallyAndInSuperclass() { | ||
assertBeansAreFound(OverridingConfig.class, "foo", "xyz"); | ||
} | ||
|
||
@Test // gh-30449 | ||
void beanMethodsAreFoundWhenDeclaredOnlyInSuperclass() { | ||
assertBeansAreFound(ExtendedConfig.class, "foo", "xyz"); | ||
} | ||
|
||
private static void assertBeansAreFound(Class<?> configClass) { | ||
assertBeansAreFound(configClass, "foo", "bar"); | ||
} | ||
|
||
private static void assertBeansAreFound(Class<?> configClass, String expected1, String expected2) { | ||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configClass)) { | ||
String bean1 = context.getBean("bean1", String.class); | ||
String bean2 = context.getBean("bean2", String.class); | ||
|
||
assertThat(bean1).isEqualTo(expected1); | ||
assertThat(bean2).isEqualTo(expected2); | ||
} | ||
} | ||
|
||
|
||
interface ConfigInterface { | ||
|
||
@Bean | ||
default String bean1() { | ||
return "foo"; | ||
} | ||
|
||
@Bean | ||
default String bean2() { | ||
return "bar"; | ||
} | ||
} | ||
|
||
static class InterfaceDefaultMethodsConfig implements ConfigInterface { | ||
} | ||
|
||
static class BaseConfig { | ||
|
||
@Bean | ||
String bean1() { | ||
return "foo"; | ||
} | ||
|
||
@Bean | ||
String bean2() { | ||
return "bar"; | ||
} | ||
} | ||
|
||
static class OverridingConfig extends BaseConfig { | ||
|
||
@Bean | ||
@Override | ||
String bean2() { | ||
return "xyz"; | ||
} | ||
} | ||
|
||
static class ExtendedConfig extends OverridingConfig { | ||
} | ||
|
||
} |