springfox 源码分析(五) web配置类Plugin插件的使用

时间:2019-5-23 14:46:50

地点:家中

前言

我们在上一篇文章,通过@EnableSwagger2注解,知道springfox使用开启Plugin注解的配置,注入了很多Plugin的配置类,结合我们第二篇针对Spring Plugin的使用文章介绍,该篇主要探索springfox中的各种不同Plugin的具体作用,以及声明了那些方法

插件

SpringfoxWebMvcConfiguration配置中,主要涉及了以下Plugin

  • DocumentationPlugin
  • ApiListingBuilderPlugin
  • OperationBuilderPlugin
  • ParameterBuilderPlugin
  • ExpandedParameterBuilderPlugin
  • OperationModelsProviderPlugin
  • DefaultsProviderPlugin
  • PathDecorator
  • ApiListingScannerPlugin

代码结构一览:

plugin中声明的接口,都是为处理contexts上下文中的属性

DocumentationPlugin

先来看DocumentationPlugin的源码

public interface DocumentationPlugin extends Plugin<DocumentationType> {
  /**
   * @return indicator to determine if the plugin is enabled
   */
  boolean isEnabled();

  DocumentationType getDocumentationType();

  /**
   * Creates a documentation context based on a given DocumentationContextBuilder
   *
   * @param builder - @see springfox.documentation.spi.service.contexts.DocumentationContextBuilder
   * @return context to use for building the documentation
   */
  DocumentationContext configure(DocumentationContextBuilder builder);

  /**
   * Gets the group name for the plugin. This is expected to be unique for each instance of the plugin
   * @return group the plugin belongs to
   */
  String getGroupName();
}

此Plugin使用的分隔符类是DocumentationType,文档类型,在Springfox中声明了三个版本的文档类型,主要是:

  • SWAGGER_2:swagger的2.0版本
  • SWAGGER_12:Swagger的1.2版本
  • SPRING_WEB:springfox项目是由原spring-mvc-swagger项目演变而来,所以这是最早的一个版本

DocumentationPlugin定义了三个方法:

  • 是否启用
  • 获取文档类型
  • 通过文档上下文Builder构建文档上下文对象
  • 获取分组名称

那么,他的实现类是谁,我们通过IDEA的编辑器功能能轻松定位到,是Docket类

来看类图:

由于Docket对象是最终实现类,而我们开发者一般在使用时,都是通过创建Docket的实体对象来注入到Spring的容器中

所以,我们创建几个Docket对象的Bean实例,那么通过PluginRetry<DocumentationPlugin,DocumentationType>的getPlugins()方法,最终就会获取到外部注入的Docket对象实例,然后再程序中就可以使用了

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值