博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊springboot2的MappingsEndpoint
阅读量:6625 次
发布时间:2019-06-25

本文共 9382 字,大约阅读时间需要 31 分钟。

本文主要研究下springboot2的MappingsEndpoint

MappingsEndpointAutoConfiguration

spring-boot-actuator-autoconfigure-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/autoconfigure/web/mappings/MappingsEndpointAutoConfiguration.java

@ManagementContextConfigurationpublic class MappingsEndpointAutoConfiguration {	@Bean	@ConditionalOnEnabledEndpoint	public MappingsEndpoint mappingsEndpoint(ApplicationContext applicationContext,			ObjectProvider
> descriptionProviders) { return new MappingsEndpoint( descriptionProviders.getIfAvailable(Collections::emptyList), applicationContext); } @Configuration @ConditionalOnWebApplication(type = Type.SERVLET) static class ServletWebConfiguration { @Bean ServletsMappingDescriptionProvider servletMappingDescriptionProvider() { return new ServletsMappingDescriptionProvider(); } @Bean FiltersMappingDescriptionProvider filterMappingDescriptionProvider() { return new FiltersMappingDescriptionProvider(); } @Configuration @ConditionalOnClass(DispatcherServlet.class) @ConditionalOnBean(DispatcherServlet.class) static class SpringMvcConfiguration { @Bean DispatcherServletsMappingDescriptionProvider dispatcherServletMappingDescriptionProvider() { return new DispatcherServletsMappingDescriptionProvider(); } } } @Configuration @ConditionalOnWebApplication(type = Type.REACTIVE) @ConditionalOnClass(DispatcherHandler.class) @ConditionalOnBean(DispatcherHandler.class) static class ReactiveWebConfiguration { @Bean public DispatcherHandlersMappingDescriptionProvider dispatcherHandlerMappingDescriptionProvider( ApplicationContext applicationContext) { return new DispatcherHandlersMappingDescriptionProvider(); } }}复制代码

这里使用descriptionProviders创建mappingsEndpoint,然后根据是servlet或reactive的方式来创建不同的MappingDescriptionProvider,servlet创建的是ServletsMappingDescriptionProvider,FiltersMappingDescriptionProvider,DispatcherServletsMappingDescriptionProvider,reactive创建的是DispatcherHandlersMappingDescriptionProvider

MappingsEndpoint

spring-boot-actuator-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/web/mappings/MappingsEndpoint.java

@Endpoint(id = "mappings")public class MappingsEndpoint {	private final Collection
descriptionProviders; private final ApplicationContext context; public MappingsEndpoint(Collection
descriptionProviders, ApplicationContext context) { this.descriptionProviders = descriptionProviders; this.context = context; } @ReadOperation public ApplicationMappings mappings() { ApplicationContext target = this.context; Map
contextMappings = new HashMap<>(); while (target != null) { contextMappings.put(target.getId(), mappingsForContext(target)); target = target.getParent(); } return new ApplicationMappings(contextMappings); } private ContextMappings mappingsForContext(ApplicationContext applicationContext) { Map
mappings = new HashMap<>(); this.descriptionProviders .forEach((provider) -> mappings.put(provider.getMappingName(), provider.describeMappings(applicationContext))); return new ContextMappings(mappings, applicationContext.getParent() == null ? null : applicationContext.getId()); } //......}复制代码

这里根据descriptionProviders创建ContextMappings

servlet方式

ServletsMappingDescriptionProvider

spring-boot-actuator-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/web/mappings/servlet/ServletsMappingDescriptionProvider.java

public class ServletsMappingDescriptionProvider implements MappingDescriptionProvider {	@Override	public List
describeMappings( ApplicationContext context) { if (!(context instanceof WebApplicationContext)) { return Collections.emptyList(); } return ((WebApplicationContext) context).getServletContext() .getServletRegistrations().values().stream() .map(ServletRegistrationMappingDescription::new) .collect(Collectors.toList()); } @Override public String getMappingName() { return "servlets"; }}复制代码

这里从servletContext中获取ServletRegistrations,再获取ServletRegistrationMappingDescription

FiltersMappingDescriptionProvider

spring-boot-actuator-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/web/mappings/servlet/FiltersMappingDescriptionProvider.java

public class FiltersMappingDescriptionProvider implements MappingDescriptionProvider {	@Override	public List
describeMappings( ApplicationContext context) { if (!(context instanceof WebApplicationContext)) { return Collections.emptyList(); } return ((WebApplicationContext) context).getServletContext() .getFilterRegistrations().values().stream() .map(FilterRegistrationMappingDescription::new) .collect(Collectors.toList()); } @Override public String getMappingName() { return "servletFilters"; }}复制代码

这里从servletContext中获取FilterRegistrations,再获取FilterRegistrationMappingDescription

DispatcherServletsMappingDescriptionProvider

spring-boot-actuator-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/web/mappings/servlet/DispatcherServletsMappingDescriptionProvider.java

public class DispatcherServletsMappingDescriptionProvider		implements MappingDescriptionProvider {	private static final List
> descriptionProviders; static { List
> providers = new ArrayList<>(); providers.add(new RequestMappingInfoHandlerMappingDescriptionProvider()); providers.add(new UrlHandlerMappingDescriptionProvider()); if (ClassUtils.isPresent( "org.springframework.data.rest.webmvc.support.DelegatingHandlerMapping", null)) { providers.add(new DelegatingHandlerMappingDescriptionProvider( new ArrayList<>(providers))); } descriptionProviders = Collections.unmodifiableList(providers); } @Override public String getMappingName() { return "dispatcherServlets"; } @Override public Map
> describeMappings( ApplicationContext context) { if (context instanceof WebApplicationContext) { return describeMappings((WebApplicationContext) context); } return Collections.emptyMap(); } private Map
> describeMappings( WebApplicationContext context) { Map
> mappings = new HashMap<>(); context.getBeansOfType(DispatcherServlet.class) .forEach((name, dispatcherServlet) -> mappings.put(name, describeMappings(new DispatcherServletHandlerMappings(name, dispatcherServlet, context)))); return mappings; } private List
describeMappings( DispatcherServletHandlerMappings mappings) { return mappings.getHandlerMappings().stream().flatMap(this::describe) .collect(Collectors.toList()); } private
Stream
describe( T handlerMapping) { return describe(handlerMapping, descriptionProviders).stream(); } @SuppressWarnings("unchecked") private static
List
describe( T handlerMapping, List
> descriptionProviders) { for (HandlerMappingDescriptionProvider
descriptionProvider : descriptionProviders) { if (descriptionProvider.getMappingClass().isInstance(handlerMapping)) { return ((HandlerMappingDescriptionProvider
) descriptionProvider) .describe(handlerMapping); } } return Collections.emptyList(); } //......}复制代码

这里主要从DelegatingHandlerMappingDescriptionProvider、RequestMappingInfoHandlerMappingDescriptionProvider、UrlHandlerMappingDescriptionProvider获取DispatcherServletMappingDescription

reactive方式

DispatcherHandlersMappingDescriptionProvider

spring-boot-actuator-2.0.1.RELEASE-sources.jar!/org/springframework/boot/actuate/web/mappings/reactive/DispatcherHandlersMappingDescriptionProvider.java

public class DispatcherHandlersMappingDescriptionProvider		implements MappingDescriptionProvider {	private static final List
> descriptionProviders = Arrays .asList(new RequestMappingInfoHandlerMappingDescriptionProvider(), new UrlHandlerMappingDescriptionProvider(), new RouterFunctionMappingDescriptionProvider()); @Override public String getMappingName() { return "dispatcherHandlers"; } @Override public Map
> describeMappings( ApplicationContext context) { Map
> mappings = new HashMap<>(); context.getBeansOfType(DispatcherHandler.class).forEach( (name, handler) -> mappings.put(name, describeMappings(handler))); return mappings; } private List
describeMappings( DispatcherHandler dispatcherHandler) { return dispatcherHandler.getHandlerMappings().stream().flatMap(this::describe) .collect(Collectors.toList()); } @SuppressWarnings("unchecked") private
Stream
describe( T handlerMapping) { for (HandlerMappingDescriptionProvider
descriptionProvider : descriptionProviders) { if (descriptionProvider.getMappingClass().isInstance(handlerMapping)) { return ((HandlerMappingDescriptionProvider
) descriptionProvider) .describe(handlerMapping).stream(); } } return Stream.empty(); } //......}复制代码

与servlet获取DispatcherServletsMappingDescriptionProvider不同,reactive获取的是DispatcherHandlersMappingDescriptionProvider 主要从RequestMappingInfoHandlerMappingDescriptionProvider、RouterFunctionMappingDescriptionProvider、UrlHandlerMappingDescriptionProvider这三个来获取HandlerMappingDescriptionProvider

小结

springboot2的MappingsEndpoint通过provider根据servlet及reactive分别进行各自的mappings获取,servlet主要分为ServletsMappingDescriptionProvider、FiltersMappingDescriptionProvider、DispatcherServletsMappingDescriptionProvider,而reactive主要分为DispatcherHandlersMappingDescriptionProvider

doc

转载地址:http://qvjpo.baihongyu.com/

你可能感兴趣的文章
醒醒吧少年,只用Cucumber不能帮助你BDD
查看>>
一名女程序员对iOS的想法
查看>>
Cloud Native未来值得关注的方向:Service Mesh简介
查看>>
西班牙现新型电费退款网络诈骗 侨胞需谨防上当
查看>>
JVM新生代和老年代配置原则
查看>>
太合音乐发布“少年红星音乐计划” 力促00后创作浪潮
查看>>
ICO后STO也被明令禁止,区块链并不是法外之地
查看>>
浅尝辄止,React是如何工作的
查看>>
6000多套iOS源码大分享
查看>>
一个完整Java Web项目背后的密码
查看>>
前端笔试题面试题记录(上)| 掘金技术征文
查看>>
收发数据的原理(上)
查看>>
AccessibilityService 从入门到出轨
查看>>
七层网络协议-tcp/ip协议
查看>>
React 学习资源
查看>>
Jenkins插件开发入门指南
查看>>
Hacking with Unicode
查看>>
试用React语法的多端框架Taro问题汇总
查看>>
The impacts of using index as key in React
查看>>
深度分析Facebook ADS广告投放平台(1):平台介绍
查看>>