码迷,mamicode.com
首页 > 编程语言 > 详细

springboot实现自定义mvc组件

时间:2020-11-08 16:59:28      阅读:18      评论:0      收藏:0      [点我收藏+]

标签:ext   info   oca   public   err   frame   org   功能   解析   

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配置类
 */
@Configuration
public 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;
        }
    }
}

springboot实现自定义mvc组件

标签:ext   info   oca   public   err   frame   org   功能   解析   

原文地址:https://www.cnblogs.com/Y-wee/p/13928382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!