官术网_书友最值得收藏!

  • Spring 5.0 Projects
  • Nilang Patel
  • 203字
  • 2021-07-02 12:34:58

Enabling Web MVC using @EnableWebMvc

To be able to make use of the Spring MVC features, we need to have one class that has been annotated with @Configuration, to be annotated with @EnableWebMvc. The @EnableWebMvc annotation, imports the Spring MVC configuration from the WebMvcConfigurationSupport class present in the Spring MVC framework. If we need to override any of the default imported configuration, we would have to implement the WebMvcConfigurer interface present in the Spring MVC framework and override the required methods.

We will create an AppConfiguration class with the following definition:

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.nilangpatel.worldgdp")
public class AppConfiguration implements WebMvcConfigurer{

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
}

In the previous configuration, a few important things to note are as follows:

  • @EnableWebMvc: This imports the Spring MVC related configuration.
  • @ComponentScan: This is used for declaring the packages that have to be scanned for Spring components (which can be @Configuration, @Service, @Controller, @Component, and so on). If no package is defined, then it scans starting from the package where the class is defined.
  • WebMvcConfigurer: We are going to implement this interface to override some of the default Spring MVC configuration seen in the previous code.
主站蜘蛛池模板: 汤阴县| 汾西县| 贵港市| 德阳市| 保康县| 抚远县| 阳泉市| 遵义市| 平定县| 浦城县| 额敏县| 松原市| 仪陇县| 衡水市| 苍山县| 洛川县| 青岛市| 年辖:市辖区| 栖霞市| 荆门市| 尉氏县| 曲靖市| 金塔县| 宁河县| 沁水县| 泰宁县| 福建省| 太仓市| 昌邑市| 迁安市| 昌吉市| 临泽县| 伊金霍洛旗| 象州县| 普兰店市| 博野县| 改则县| 汉川市| 广平县| 华亭县| 临沭县|