Repository navigation
Closed
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
Description
Rob Winch opened SPR-10660 and commented
@Enable annotations have no meaning unless combined with @Configuration. It would be useful to allow configurations with only @Enable annotations.
It would be ideal if all the following tests pass (each test is marked with a comment with its current behavior):
package sample;
... imports omitted ...
public class EnableWithNoConfigurationAnnotationTests {
private ConfigurableApplicationContext context;
@After
public void closeContext() {
if(context != null) {
context.close();
}
}
// succeeds
@Test
public void enableWebMvcWithConfigurationAnnotation() {
assertLoadsWebMvc(EnableWebMvcWithConfigurationAnnotationConfig.class);
}
@EnableWebMvc
@Configuration
static class EnableWebMvcWithConfigurationAnnotationConfig {}
// fails
@Test
public void enableWebMvcNoConfigurationAnnotation() {
assertLoadsWebMvc(EnableWebMvcNoConfigurationAnnotationConfig.class);
}
@EnableWebMvc
static class EnableWebMvcNoConfigurationAnnotationConfig {}
// fails
@Test
public void enableWebMvcNoConfigurationClasspathScanning() {
assertLoadsWebMvc(EnableWebMvcWithClasspathScanningConfig.class);
}
@Configuration
@ComponentScan(basePackageClasses=ScannedEnableWebMvcNoConfigurationAnnotationConfig.class)
static class EnableWebMvcWithClasspathScanningConfig {}
private void assertLoadsWebMvc(Class<?>... configs) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletConfig(new MockServletConfig());
context.setServletContext(new MockServletContext());
context.register(configs);
context.refresh();
context.start();
// verify RequestMappingHandlerMapping is found
context.getBean(RequestMappingHandlerMapping.class);
this.context = context;
}
}
package package sample.scanning;
... imports omitted...
@EnableWebMvc
public class ScannedEnableWebMvcNoConfigurationAnnotationConfig {
}
Issue Links:
- Consider @Import classes as lite @Configuration [SPR-10533] #15163 Consider
@Importclasses as lite@Configuration("duplicates")
Referenced from: commits d18b3f0
Metadata
Metadata
Assignees
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
Activity
spring-projects-issues commentedon Jan 18, 2016
Rob Winch commented
Juergen Hoeller Any chance we could look into this for the next release?
spring-projects-issues commentedon Jan 20, 2016
Juergen Hoeller commented
Rob Winch, this effectively duplicates #15163... and therefore works since 4.0 already. Have you been trying this against 3.2.x?
spring-projects-issues commentedon Jan 20, 2016
Rob Winch commented
Thanks Juergen Hoeller! I hadn't realized it was fixed as part of another issue. I was bumping the issue due to some feedback on StackOverflow. I responded to the comment that this should be resolved and am awaiting any response. Thanks again!
spring-projects-issues commentedon Jul 7, 2017
Rob Winch commented
Juergen Hoeller I just noticed that this was not actually resolved completely. The
enableWebMvcNoConfigurationClasspathScanningstill fails.spring-projects-issues commentedon Jul 14, 2017
Juergen Hoeller commented
I'm afraid this works as designed: Classpath scanning requires a match for an include filter, by default one of our
@Componentderived stereotypes. Since@EnableWebMvcon its own does not constitute such a stereotype, scanning indeed only works with an@Configuration(or any other@Component) declaration next to it, in contrast to an explicitly registered component where no such identification by stereotype is required.spring-projects-issues commentedon Jul 14, 2017
Rob Winch commented
Juergen Hoeller Thanks for the response. Does it make sense to explicitly add
@Componentto@Enable*annotations?spring-projects-issues commentedon Jul 14, 2017
Juergen Hoeller commented
I'm afraid it doesn't.. those are really orthogonal concerns. Through custom include/exclude filters, people may already selectively pick up certain component classes but not others, and we'd arguably mess that up if the plain presence of
@Enableannotations all of a sudden indicates a component nature for such scanning purposes.