Skip to content

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

Closed

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

Activity

  1. spring-projects-issues commented on Jan 18, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Rob Winch commented

    Juergen Hoeller Any chance we could look into this for the next release?

  2. spring-projects-issues commented on Jan 20, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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?

  3. spring-projects-issues commented on Jan 20, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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!

  4. spring-projects-issues commented on Jul 7, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Rob Winch commented

    Juergen Hoeller I just noticed that this was not actually resolved completely. The enableWebMvcNoConfigurationClasspathScanning still fails.

  5. spring-projects-issues commented on Jul 14, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I'm afraid this works as designed: Classpath scanning requires a match for an include filter, by default one of our @Component derived stereotypes. Since @EnableWebMvc on 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.

  6. spring-projects-issues commented on Jul 14, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Rob Winch commented

    Juergen Hoeller Thanks for the response. Does it make sense to explicitly add @Component to @Enable* annotations?

  7. spring-projects-issues commented on Jul 14, 2017

    @spring-projects-issues
    CollaboratorAuthor

    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 @Enable annotations all of a sudden indicates a component nature for such scanning purposes.

  8. added
    status: declined
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: declinedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions