96

I read javadoc about @EnableWebMvc.

But I don't understand what this annotation mean?

Can you expalin it clearly?

4 Answers4

111

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.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 1
    What is mvc:annotation-driven ? – Koray Tugay Jul 18 '15 at 11:18
  • 1
    "It enables support for @Controller-annotated classes", is not correct. From Spring docs, `To enable auto-detection of such @Controller beans, you can add component scanning` – Rich Jun 28 '18 at 19:53
  • 1
    It is correct. Without `@EnableWebMvc`, a `@Controller` bean is just a bean, not a controller. That applies whether it’s found via component scanning or registered via a `@Bean` method. – Andy Wilkinson Jun 28 '18 at 20:45
  • 1
    Is this still relevant? We are using mcv approach with spring boot and @ controller , @ RequestMapping etc. Have never seen @ EnableWebMvc. – user3192295 Jan 30 '21 at 05:20
  • 2
    @user3192295 If you're using Spring Boot, it will auto-configure the equivalent of `@EnableWebMvc` for you. Unless you want to switch off all of Boot's opinions about how Spring MVC should be configured, you should not use `@EnableWebMvc` in your application. – Andy Wilkinson Jan 30 '21 at 12:28
  • A good explain here https://stackoverflow.com/questions/70358391/what-is-the-difference-between-using-enablewebmvc-webmvcconfigurer-and-webmvc – Abe Feb 02 '23 at 23:10
58

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:

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:

You can read up on:

Hope it helps. :)

user2339071
  • 4,254
  • 1
  • 27
  • 46
3

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.

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
0

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