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

  • 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.
主站蜘蛛池模板: 辽阳县| 宜丰县| 山阴县| 新昌县| 宁蒗| 临城县| 南江县| 五大连池市| 翼城县| 波密县| 西丰县| 大悟县| 安塞县| 太湖县| 台北市| 绥中县| 开平市| 湘西| 烟台市| 克什克腾旗| 封开县| 晋中市| 青州市| 万宁市| 阳春市| 衡阳县| 星子县| 蕲春县| 兴义市| 登封市| 新昌县| 山东省| 通河县| 合阳县| 民乐县| 南宁市| 五家渠市| 西城区| 巴南区| 诏安县| 灵丘县|