-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve docs for AnnotatedBeanDefinitionReader, @Configuration, and @ContextConfiguration regarding "annotated classes" #23638
Comments
The fact that annotated classes (other than
So we cannot change that, since it would be a breaking change. In addition, it can be quite useful to test a very limited combination of components. Having said that, however, the fact that a non-annotated class is automatically registered as a Spring bean is not documented. So I'll investigate that further. |
A valid use case for this behavior of So, in light of that, I'm changing the focus of this issue to improve the corresponding documentation. |
Hi @sbrannen The current
However, in fact, Also, undocumented behavior is that classes that have specified in I still think restricting
I agree allowing non-annotated classes to be injectable makes certain tests useful but very limited, and It would be beneficial to mention in documentation such that using non-annotated classes have limitation and favoring So, extreme case, this is possible: @ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {
MyConfigTest.MyConfig.class, // annotated class
RuntimeException.class, // non annotated class
String.class, // non annotated class
MyConfigTest.Foo.class // since its constructor is resolvable, can be listed here
// MyConfigTest.Bar.class // constructor injection fails, so cannot be here
})
class MyConfigTest {
@Autowired
MyConfig myConfig; // annotaed class. This might be useful for testing @Configuration itself
@Autowired
RuntimeException runtimeException; // non annotated class
@Autowired
String myString; // non annotated class
@Autowired
Foo foo; // non annotated class with String constructor
@Test
void check() {
assertThat(myConfig).isNotNull();
assertThat(myString).isNotNull();
assertThat(runtimeException).isNotNull();
assertThat(foo).isNotNull();
}
@Configuration
static class MyConfig {
}
static class Foo {
public Foo(String s) {
}
}
static class Bar {
public Bar(Integer i) {
}
}
} |
Updated deliverables are now being tracked in this issue's description. |
Original Description
I find this test interesting:
This works because while loading a context,
AnnotationConfigContextLoader
registers@ContextConfiguration
provided classes(RestTemplate
in this case) to bean definitions, so that they are available for injections.So, this is also possible:
I think this is not an intended usage of
@ContextConfiguration#classes
.Probably, by default, filter-out or validate those classes to be
@Configuration
classes.For the case of allowing non
@Configuration
classes (for example, lite-mode), probably provide an explicit option(new attribute) on@ContextConfiguration
. e.g.:@ContextConfiguration(classes=MyBean.class, liteMode=true)
This is what I found in real world code base:
So, would be nice not seeing such test class :)
Deliverables
Improve documentation for the following regarding "annotated classes".
AnnotationConfigRegistry
AnnotationConfigApplicationContext
AnnotationConfigWebApplicationContext
AnnotatedBeanDefinitionReader
@Configuration
@Import
AnnotationConfigContextLoader
AnnotationConfigContextLoaderUtils
@ContextConfiguration
testing.adoc
The text was updated successfully, but these errors were encountered: