Annotation Type EnableWebMvc. Enables default Spring MVC configuration and registers Spring MVC infrastructure components expected by the DispatcherServlet . Use this annotation on an @ Configuration class. In turn that will import DelegatingWebMvcConfiguration , which provides default Spring MVC configuration.
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.
Interface WebMvcConfigurer. Defines callback methods to customize the Java-based configuration for Spring MVC enabled via @EnableWebMvc . @EnableWebMvc -annotated configuration classes may implement this interface to be called back and given a chance to customize the default configuration.
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.
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:
Well, to bore you a little bit more ,it provides a lot a things like:
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:
@Bean
and other available methodsYou can read up on:
Hope it helps. :)
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
.
Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With