Springboot自定義mvc組件如何實現(xiàn)
更新時間:2020年11月18日 09:53:28 作者:Y_wee
這篇文章主要介紹了Springboot自定義mvc組件如何實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
如果你想實現(xiàn)一些定制化功能,只需要寫這個組件,然后將它交給springboot管理,springboot會給我們自動裝配
以下是spring官方文檔解釋
由官方文檔可知,想要自定義組件,需要實現(xiàn)以下步驟
- 寫一個配置類,加上@Configuration注解
- 實現(xiàn)WebMvcConfigurer接口
- 不添加@EnableWebMvc注解
示例:自定義視圖解析器
package com.yl.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.View; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.Locale; /** * mvc配置類 */ @Configuration public class MyMvcConfig implements WebMvcConfigurer { /** * 將自定義視圖解析器配置成bean存入spring */ @Bean public ViewResolver myViewResovler(){ return new MyViewResolver(); } /** * 自定義視圖解析器,實現(xiàn)視圖解析器接口 */ public static class MyViewResolver implements ViewResolver{ @Override public View resolveViewName(String viewName, Locale locale) throws Exception { return null; } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot啟動嵌入式Tomcat的實現(xiàn)步驟
本文主要介紹了淺談SpringBoot如何啟動嵌入式Tomcat,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08redis中存儲list<map>,list<entity>的處理
本文主要介紹了redis中存儲list<map>,list<entity>的處理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-06-06