EnableWebMvc annotation meaning

前端 未结 4 1073
南笙
南笙 2020-12-02 08:34

I read javadoc about @EnableWebMvc.

But I don\'t understand what this annotation mean?

Can you expalin it clearly?

相关标签:
4条回答
  • 2020-12-02 08:43

    When we want to build a Spring Web MVC project we need to add necessary import from WebMvcConfigurationSupport.For that reason, we should use @EnableWebMvc in java based configuration. Only one @Configuration class may have @EnableWebMvc.

    0 讨论(0)
  • 2020-12-02 08:44

    Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport

    0 讨论(0)
  • 2020-12-02 08:51

    Welcome to the world of Spring. There is something you need to understand before you know what the annotation @EnableWebMVC means.

    Spring traditionally supports two types of configurations:

    • XML based configuration
    • Annotation based configuration

    These annotations are essentially implemented as a part of MVC Java Config Design.

    Consider a simple class:

    @EnableWebMvc
    @Configuration
    public class WebConfig {
    }
    

    There are no base classes. No spring beans in sight.. Hmmm..

    Lets go a little further:

    • What does this actually provide.. ?

    Well, to bore you a little bit more ,it provides a lot a things like:

    1. @MVC request processing
    2. Global JSR-303 validator

    and a few more.

    Ahahah... But your application works with it right. So, where's the magic.. ?

    @EnableWebMVC <---- What's behind this..?

    This is behind it:

    @Retention(RetentionPolicy.RUNTIME)
    @Import(DelegatingWebMvcConfiguration.class)
    @Target(ElementType.TYPE)
    public @interface EnableWebMvc {
    }
    

    See, now you would think that how pointless using @EnableWebMVC. Would you rather:

    • Extend WebMvcConfigurationSupport
    • Override @Bean and other available methods

    You can read up on:

    • Java Reflections:Annotations

    Hope it helps. :)

    0 讨论(0)
  • 2020-12-02 08:54

    When you're using Java code (as opposed to XML) to configure your Spring application, @EnableWebMvc is used to enable Spring MVC. If you're not already familiar with Spring's support for Java configuration, this is a good place to start.

    @EnableWebMvc is equivalent to <mvc:annotation-driven /> in XML. It enables support for @Controller-annotated classes that use @RequestMapping to map incoming requests to a certain method. You can read detailed information about what it configures by default and how to customise the configuration in the reference documentation.

    0 讨论(0)
提交回复
热议问题