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

地点:单位、家中

@EnableSwagger2

有了二三章的理解,此时我们再来看EnableSwagger2注解的内容

@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)

@Target(value = { java.lang.annotation.ElementType.TYPE })

@Documented

@Import({Swagger2DocumentationConfiguration.class})

public @interface EnableSwagger2 {

}

Swagger2DocumentationConfiguration

该注解没啥好说的,最终是导入Swagger2DocumentationConfiguration的配置类

@Configuration

@Import({ SpringfoxWebMvcConfiguration.class, SwaggerCommonConfiguration.class })

@ComponentScan(basePackages = {

"springfox.documentation.swagger2.mappers"

})

@ConditionalOnWebApplication

public class Swagger2DocumentationConfiguration {

此处的@ComponentScan注解,扫描了springfox.documentation.swagger2.mappers包路径

Mappers

该包路径下包含了众多运用MapStruct组件自动生成的Mapper实体类转换关系,通过扫描注解,自动注入到Spring的容器中

关于MapStruct组件的使用,可参考:springfox 源码分析(二) 初探mapstruct

主要包括如下:

LicenseMapper

ModelMapper

ParameterMapper

SecurityMapper

SerivceModelToSwagger2Mapper

VendorExtensionsMapper

每个Mapper接口都有一个实现类MapperImpl,实现类通过@Component注解注入到Spring的容器中

最重要的是SerivceModelToSwagger2Mapper这个Mapper

该类的作用会聚合使用Model、Parameter、License等Mapper,将springfox中的对象转化为Swagger标准的对象,包括Swagger

@Generated(

value = "org.mapstruct.ap.MappingProcessor",

date = "2018-06-23T17:02:57-0500",

comments = "version: 1.2.0.Final, compiler: javac, environment: Java 1.8.0_151 (Oracle Corporation)"

)

@Component

public class ServiceModelToSwagger2MapperImpl extends ServiceModelToSwagger2Mapper {

@Autowired

private ModelMapper modelMapper;

@Autowired

private ParameterMapper parameterMapper;

@Autowired

private SecurityMapper securityMapper;

@Autowired

private LicenseMapper licenseMapper;

@Autowired

private VendorExtensionsMapper vendorExtensionsMapper;

@Override

public Swagger mapDocumentation(Documentation from) {

if ( from == null ) {

return null;

}

Swagger swagger = new Swagger();

swagger.setVendorExtensions( vendorExtensionsMapper.mapExtensions( from.getVendorExtensions() ) );

swagger.setSchemes( mapSchemes( from.getSchemes() ) );

swagger.setPaths( mapApiListings( from.getApiListings() ) );

swagger.setHost( from.getHost() );

swagger.setDefinitions( modelMapper.modelsFromApiListings( from.getApiListings() ) );

swagger.setSecurityDefinitions( securityMapper.toSecuritySchemeDefinitions( from.getResourceListing() ) );

ApiInfo info = fromResourceListingInfo( from );

if ( info != null ) {

swagger.setInfo( mapApiInfo( info ) );

}

swagger.setBasePath( from.getBasePath() );

swagger.setTags( tagSetToTagList( from.getTags() ) );

Listlist2 = from.getConsumes();

if ( list2 != null ) {

swagger.setConsumes( new ArrayList( list2 ) );

}

else {

swagger.setConsumes( null );

}

Listlist3 = from.getProduces();

if ( list3 != null ) {

swagger.setProduces( new ArrayList( list3 ) );

}

else {

swagger.setProduces( null );

}

return swagger;

}

//more...

}

各个Mapper组件的映射关系如下:

Mapper

目标类

LicenseMapper

io.swagger.models.License

通过ApiInfo的属性Lincese构建目标类实体对象

ModelMapper

io.swagger.models.Model

将springfox.documentation.schema.Model转化成目标类

ParameterMapper

io.swagger.models.parameters.Parameter

将springfox.documentation.service.Parameter转化成目标类

SecurityMapper

io.swagger.models.auth.SecuritySchemeDefinition

ServiceModelToSwagger2Mapper

io.swagger.models.Swagger

输出Swagger完整对象

SpringfoxWebMvcConfiguration

在Swagger2DocumentationConfiguration源码中,我们看到该Configuration类还引入了SpringfoxWebMvcConfiguration,该类是注入Spring Rest接口相关的配置核心类

先来看源码:

@Configuration

@Import({ ModelsConfiguration.class })

@ComponentScan(basePackages = {

"springfox.documentation.spring.web.scanners",

"springfox.documentation.spring.web.readers.operation",

"springfox.documentation.spring.web.readers.parameter",

"springfox.documentation.spring.web.plugins",

"springfox.documentation.spring.web.paths"

})

@EnablePluginRegistries({ DocumentationPlugin.class,

ApiListingBuilderPlugin.class,

OperationBuilderPlugin.class,

ParameterBuilderPlugin.class,

ExpandedParameterBuilderPlugin.class,

ResourceGroupingStrategy.class,

OperationModelsProviderPlugin.class,

DefaultsProviderPlugin.class,

PathDecorator.class,

ApiListingScannerPlugin.class

})

public class SpringfoxWebMvcConfiguration {

@Bean

public Defaults defaults() {

return new Defaults();

}

@Bean

public DocumentationCache resourceGroupCache() {

return new DocumentationCache();

}

@Bean

public static ObjectMapperConfigurer objectMapperConfigurer() {

return new ObjectMapperConfigurer();

}

@Bean

public JsonSerializer jsonSerializer(ListmoduleRegistrars) {

return new JsonSerializer(moduleRegistrars);

}

@Bean

public DescriptionResolver descriptionResolver(Environment environment) {

return new DescriptionResolver(environment);

}

@Bean

public HandlerMethodResolver methodResolver(TypeResolver resolver) {

return new HandlerMethodResolver(resolver);

}

}

从源码中我们可以看到:

使用import导入ModelConfiguration配置类,该类

使用@ComponentScan注解扫描配置的package包路径,完成Spring的Bean实例注入

使用@EnablePluginRegistries插件机制来完成插件的动态实例Bean注入到Spring容器中,关于Spring Plugin的使用,不明白的可以参考下上一篇文章对Spring Plugin的说明

注入相关Bean的实例对象

ModelsConfiguration

从webmvc配置类导入的Models配置类,我们来看该类的源码

@Configuration

@ComponentScan(basePackages = {

"springfox.documentation.schema"

})

@EnablePluginRegistries({

ModelBuilderPlugin.class,

ModelPropertyBuilderPlugin.class,

TypeNameProviderPlugin.class,

SyntheticModelProviderPlugin.class

})

public class ModelsConfiguration {

@Bean

public TypeResolver typeResolver() {

return new TypeResolver();

}

}

该类的配置和SpringfoxWebMvcConfiguration配置类相似,作用都是扫描包路径,启用PluginRetry进行Spring的实体Bean动态注入

SwaggerCommonConfiguration

Swagger2DocumenationConfiguration导入的第二个配置类SwaggerCommonConfiguration

来看代码:

SwaggerCommonConfiguration.java

@Configuration

@ComponentScan(basePackages = {

"springfox.documentation.swagger.schema",

"springfox.documentation.swagger.readers",

"springfox.documentation.swagger.web"

})

public class SwaggerCommonConfiguration {

}

作用和以上类似

总结

通过@EnableSwagger2注解,我们看到了三个4个Configuration配置类的导入

主要作用:

实体Bean的注入

Plugin插件的动态Bean注入

扫描springfox配置的各种package路径

看到这里相信我们还是一头雾水,我们并没有发现springfox何时初始化接口类的.

接下来,我们会针对上面Configuration涉及到的Plugin和@CompnentScan扫描package路径进行一一探索.

springfox源码_springfox 源码分析(四) 配置类初始化相关推荐

  1. springfox源码_springfox 源码分析(七) 文档初始化

    时间:2019-5-23 20:12:04 地点:家中 通过前面几篇文章对springfox的介绍,以及我们的学习准备工作,这篇我们将正式来探索springfox是如何初始化的 我们在学算法的时候,其 ...

  2. 集合框架源码分析四(Collections类详细分析)

    我认为Collections类主要是完成了两个主要功能  1.提供了若干简单而又有用的算法,比如排序,二分查找,求最大最小值等等.  2.提供对集合进行包装的静态方法.比如把指定的集合包装成线程安全的 ...

  3. spring源码分析06-spring配置类解析

    什么是spring配置类? 类上有注解:@Configuration .@Component.@ComponentScan.@Import.@ImportResource 或者类中的任意方法有@Bea ...

  4. Tomcat8源码分析系列-启动分析(四) webapp

    前言 上一篇文章中我们分析了 Service.Engine.Host.Pipeline.Valve 组件的启动逻辑,在 HostConfig 中会实例化 StandardContext,并启动 Con ...

  5. 【源码篇】聊聊源码mybatis(更新分析)

    文章目录 1.举个case-向数据库插入单个实体对象 2.核心类和核心方法 2.1.Part1​[解析]:zap:解析Mapper接口.@Insert注解和其他入参. 2.1.1.MapperProx ...

  6. 手机自动化测试:Appium源码分析之跟踪代码分析四 1

    手机自动化测试:Appium源码分析之跟踪代码分析四 控制器模块 // Appium webserver controller methods // https://github.com/hugs/a ...

  7. ABP源码分析四十七:ABP中的异常处理

    ABP源码分析四十七:ABP中的异常处理 参考文章: (1)ABP源码分析四十七:ABP中的异常处理 (2)https://www.cnblogs.com/1zhk/p/5538983.html (3 ...

  8. 【投屏】Scrcpy源码分析四(最终章 - Server篇)

    Scrcpy源码分析系列 [投屏]Scrcpy源码分析一(编译篇) [投屏]Scrcpy源码分析二(Client篇-连接阶段) [投屏]Scrcpy源码分析三(Client篇-投屏阶段) [投屏]Sc ...

  9. gSOAP 源码分析(四)

    gSOAP 源码分析(四) 2012-6-2 邵盛松 前言 本文主要说明gSOAP中对Client的认证分析 gSOAP中包含了HTTP基本认证,NTLM认证等,还可以自定义SOAP Heard实现认 ...

最新文章

  1. 如何使用markdown编辑器?官方文档在此
  2. inotifywait监听php,利用inotifywait监控主机文件和目录
  3. 2018-2019-2 20175224 实验五《网络编程与安全》实验报告
  4. shell初学之PHP
  5. Nginx系列(6):Web服务器分析(理论)
  6. 闲谈输入法、MinGW、日文字体
  7. canfd收不到数据_CAN FD网络的通信距离问题分析
  8. 【f1c200s/f1c100s】不带中断引脚采用扫描的方式实现通用gpio-keys
  9. 微信公众号消息模板——Java
  10. c++_2: 类的定义
  11. 网站被劫持了怎么办?
  12. matlab 7y30,屏幕及音响表现出众_华硕 灵焕3(M3 7Y30/8GB/256GB)_笔记本评测-中关村在线...
  13. Win10 使用黑屏重置键 解决 黑屏问题
  14. 如何扩展Linux系统分区大小
  15. PAT 乙级 1040 有几个PAT (25分)
  16. sourcetree 中文版
  17. 计算云服务——弹性伸缩服务
  18. 8086CPU相关汇编语言的简单概述
  19. Python画图常用代码总结,这20个画图代码现拿现用
  20. 交换机端口镜像及其工作原理

热门文章

  1. 27 CPUs Benchmarked With AOM AV1, Intel SVT VP9/AV1/HEVC Video Encoders
  2. (vue)switch循环遍历
  3. 【计算机图形学】图形显示设备
  4. 高等学校计算机专业教材精选计算机基础,大学计算机基础教程/高等学校计算机基础教育教材精选简介,目录书摘...
  5. 新媒体短视频运营要素分析
  6. 关于AI如何实现短视频制作的方案仅供参考
  7. Dell戴尔笔记本电脑灵越Inspiron 5590原装出厂系统恢复原厂OEM系统1903
  8. 百度富文本编辑器UEditor 图片宽度100%自适应,手机端
  9. 搭建邮件服务器 dns,搭建DNS服务器+邮件服务器
  10. java BigDecimal加法/减法/乘法/除法 保留两位小数