- 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.
推薦閱讀
- 物聯網與北斗應用
- 黑客攻防實戰技術完全手冊:掃描、嗅探、入侵與防御
- Mastering Node.js(Second Edition)
- Go Web Scraping Quick Start Guide
- 信息通信網絡建設安全管理概要2
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 網絡環境中基于用戶視角的信息質量評價研究
- Working with Legacy Systems
- 5G+區塊鏈
- 人人都該都懂的互聯網思維
- 5G時代的大數據技術架構和關鍵技術詳解
- 基于IPv6的家居物聯網開發與應用技術
- Guide to NoSQL with Azure Cosmos DB
- Professional Scala
- 新媒體交互藝術