Springboot自定義mvc組件如何實(shí)現(xiàn)
如果你想實(shí)現(xiàn)一些定制化功能,只需要寫這個(gè)組件,然后將它交給springboot管理,springboot會(huì)給我們自動(dòng)裝配
以下是spring官方文檔解釋
由官方文檔可知,想要自定義組件,需要實(shí)現(xiàn)以下步驟
寫一個(gè)配置類,加上@Configuration注解 實(shí)現(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配置類 */@Configurationpublic class MyMvcConfig implements WebMvcConfigurer { /** * 將自定義視圖解析器配置成bean存入spring */ @Bean public ViewResolver myViewResovler(){ return new MyViewResolver(); } /** * 自定義視圖解析器,實(shí)現(xiàn)視圖解析器接口 */ public static class MyViewResolver implements ViewResolver{ @Override public View resolveViewName(String viewName, Locale locale) throws Exception { return null; } }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JavaScript Reduce使用詳解2. 詳解JAVA 強(qiáng)引用3. 深入了解JAVA 軟引用4. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法5. SpringBoot集成Redisson實(shí)現(xiàn)延遲隊(duì)列的場(chǎng)景分析6. Python TestSuite生成測(cè)試報(bào)告過(guò)程解析7. android實(shí)現(xiàn)滾動(dòng)文本效果8. 使用Python3 poplib模塊刪除服務(wù)器多天前的郵件實(shí)現(xiàn)代碼9. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題10. java中的按位與(&)用法說(shuō)明
