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

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.
主站蜘蛛池模板: 瓦房店市| 凉城县| 西安市| 剑河县| 淮阳县| 桑日县| 龙胜| 昌黎县| 双牌县| 贵溪市| 依兰县| 上蔡县| 彭山县| 林周县| 连州市| 东乌| 新余市| 保定市| 肃南| 定西市| 南投市| 定安县| 莱州市| 怀仁县| 宣汉县| 马尔康县| 垦利县| 宜阳县| 丽水市| 侯马市| 肥西县| 报价| 师宗县| 宜宾市| 扬州市| 农安县| 沙湾县| 仙居县| 泸定县| 海兴县| 宜黄县|