Skip to content

@Enable* should be supported without @Configuration [SPR-10660] #15288

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

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

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions