Java EnableWebMvc注解含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19291329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:44:28  来源:igfitidea点击:

EnableWebMvc annotation meaning

javaspringspring-mvcannotations

提问by

I read javadoc about @EnableWebMvc.

我阅读了关于@EnableWebMvc.

But I don't understand what this annotation mean?

但我不明白这个注释是什么意思?

Can you expalin it clearly?

你能解释清楚吗?

回答by Andy Wilkinson

When you're using Java code (as opposed to XML) to configure your Spring application, @EnableWebMvcis 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.

当您使用 Java 代码(而不是 XML)来配置您的 Spring 应用程序时,@EnableWebMvc用于启用 Spring MVC。如果您还不熟悉 Spring 对 Java 配置的支持,这是一个很好的起点

@EnableWebMvcis equivalent to <mvc:annotation-driven />in XML. It enables support for @Controller-annotated classes that use @RequestMappingto 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.

@EnableWebMvc相当于<mvc:annotation-driven />在 XML 中。它支持用于将传入请求映射到特定方法的带@Controller注释的类@RequestMapping。您可以在参考文档中阅读有关它默认配置的内容以及如何自定义配置的详细信息。

回答by user2339071

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

欢迎来到春天的世界。在了解注释的@EnableWebMVC含义之前,您需要了解一些内容。

Spring traditionally supports two types of configurations:

Spring 传统上支持两种类型的配置:

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

这些注释本质上是作为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
  1. @MVC 请求处理
  2. 全局 JSR-303 验证器

and a few more.

还有一些。

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

啊哈...但您的应用程序可以正常工作。那么,魔法在哪里..?

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

@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:

看,现在您会认为使用@EnableWebMVC. 你会宁愿:

You can read up on:

您可以继续阅读:

Hope it helps. :)

希望能帮助到你。:)

回答by Mahibub Nadaf

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

将此注释添加到 @Configuration 类会从 WebMvcConfigurationSupport 导入 Spring MVC 配置

回答by Lokman Hossain

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

当我们想要构建一个 Spring Web MVC 项目时,我们需要添加必要的 import from WebMvcConfigurationSupport。因此,我们应该@EnableWebMvc在基于 java 的配置中使用。只有一个@Configuration班级可能有@EnableWebMvc