Springboot自定義mvc組件如何實現
如果你想實現一些定制化功能,只需要寫這個組件,然后將它交給springboot管理,springboot會給我們自動裝配
以下是spring官方文檔解釋
由官方文檔可知,想要自定義組件,需要實現以下步驟
寫一個配置類,加上@Configuration注解 實現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配置類 */@Configurationpublic class MyMvcConfig implements WebMvcConfigurer { /** * 將自定義視圖解析器配置成bean存入spring */ @Bean public ViewResolver myViewResovler(){ return new MyViewResolver(); } /** * 自定義視圖解析器,實現視圖解析器接口 */ public static class MyViewResolver implements ViewResolver{ @Override public View resolveViewName(String viewName, Locale locale) throws Exception { return null; } }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. vue實現web在線聊天功能2. JavaEE SpringMyBatis是什么? 它和Hibernate的區別及如何配置MyBatis3. JavaScript實現頁面動態驗證碼的實現示例4. Springboot 全局日期格式化處理的實現5. Java使用Tesseract-Ocr識別數字6. 完美解決vue 中多個echarts圖表自適應的問題7. Python使用urlretrieve實現直接遠程下載圖片的示例代碼8. SpringBoot+TestNG單元測試的實現9. 在Chrome DevTools中調試JavaScript的實現10. 解決Android Studio 格式化 Format代碼快捷鍵問題
