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

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.
主站蜘蛛池模板: 正镶白旗| 新干县| 云梦县| 泰来县| 昭苏县| 福鼎市| 松桃| 邵东县| 青龙| 扎兰屯市| 普格县| 蒙城县| 工布江达县| 阜新| 柘荣县| 中江县| 邯郸县| 宝清县| 临猗县| 沐川县| 织金县| 佛学| 清苑县| 驻马店市| 抚宁县| 新绛县| 塔河县| 宣城市| 沧州市| 新宾| 武定县| 米泉市| 阳山县| 开封市| 咸宁市| 南汇区| 昌江| 通海县| 宜都市| 酒泉市| 南充市|