Advanced Java Config

@EnableWebMvc导入DelegatingWebMvcConfiguration,该配置:

@EnableWebMvc imports DelegatingWebMvcConfiguration, which:

  • Provides default Spring configuration for Spring MVC applications

  • Detects and delegates to WebMvcConfigurer implementations to customize that configuration.

对于高级模式,你可以删除@EnableWebMvc并直接从DelegatingWebMvcConfiguration扩展,而不是实现WebMvcConfigurer,如下例所示:

For advanced mode, you can remove @EnableWebMvc and extend directly from DelegatingWebMvcConfiguration instead of implementing WebMvcConfigurer, as the following example shows:

  • Java

  • Kotlin

@Configuration
public class WebConfiguration extends DelegatingWebMvcConfiguration {

	// ...
}
@Configuration
class WebConfiguration : DelegatingWebMvcConfiguration() {

	// ...
}

你可以在WebConfig中保留现有方法,但现在你还可以覆盖基类的bean声明,你仍可以在类路径上拥有任意数量的其他WebMvcConfigurer实现。

You can keep existing methods in WebConfig, but you can now also override bean declarations from the base class, and you can still have any number of other WebMvcConfigurer implementations on the classpath.