欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Springboot自定义mvc组件如何实现

时间:2021-04-07 10:12:31|栏目:JAVA代码|点击:

如果你想实现一些定制化功能,只需要写这个组件,然后将它交给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;
    }
  }
}

上一篇:java实现遍历Map的方法

栏    目:JAVA代码

下一篇:Java并发编程volatile关键字的作用

本文标题:Springboot自定义mvc组件如何实现

本文地址:http://www.codeinn.net/misctech/96307.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有