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
EnableWebMvc annotation meaning
提问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, @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.
当您使用 Java 代码(而不是 XML)来配置您的 Spring 应用程序时,@EnableWebMvc
用于启用 Spring MVC。如果您还不熟悉 Spring 对 Java 配置的支持,这是一个很好的起点。
@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.
@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 @EnableWebMVC
means.
欢迎来到春天的世界。在了解注释的@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:
好吧,让你更无聊一点,它提供了很多东西,比如:
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
. 你会宁愿:
- Extend WebMvcConfigurationSupport
- Override
@Bean
and other available methods
- 扩展WebMvcConfigurationSupport
- 覆盖
@Bean
和其他可用方法
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 @EnableWebMvc
in java based configuration. Only one @Configuration
class may have @EnableWebMvc
.
当我们想要构建一个 Spring Web MVC 项目时,我们需要添加必要的 import from WebMvcConfigurationSupport
。因此,我们应该@EnableWebMvc
在基于 java 的配置中使用。只有一个@Configuration
班级可能有@EnableWebMvc
。