0
点赞
收藏
分享

微信扫一扫

字符串函数(二)—— 长度受限制的字符串函数

zhaoxj0217 2023-10-02 阅读 30

一、前言

在SpringMVC框架中,DispatcherServlet负责接收所有的HTTP请求并将其分发给相应的处理器。在初始化过程中,DispatcherServlet会遍历所有的HandlerMethod,为它们创建相应的处理器适配器,以便在处理请求时能够调用合适的处理器方法。

二、代码说明

1、加载处理器映射器:在SpringMVC中,处理器映射器负责将请求映射到相应的处理器方法。DispatcherServlet在初始化过程中,会加载配置文件中定义的处理器映射器。例如,在XML配置文件中,可以使用<bean>元素来定义一个处理器映射器:

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

在上面的配置中,定义了一个BeanNameUrlHandlerMapping处理器映射器。

2、遍历HandlerMethod:加载处理器映射器后,DispatcherServlet会遍历所有的HandlerMethod,为它们创建相应的处理器适配器。HandlerMethod是一个封装了处理器方法和相关信息的对象。在SpringMVC中,可以通过实现RequestMappingHandlerMapping接口来创建自定义的处理器映射器,并通过实现HandlerMethodFactory接口来创建自定义的HandlerMethod对象。例如,以下代码演示了如何遍历所有的HandlerMethod:

@Override  
protected void initHandlerMethods() {
for (String beanName : getApplicationContext().getBeanNamesForType(Object.class)) {
if (!beanName.startsWith(SCOPE_SUFFIX)) {
Class<?> beanType = getApplicationContext().getType(beanName);
if (beanType != null && isHandler(beanType)) {
detectHandlerMethods(beanName);
}
}
}
}

private void detectHandlerMethods(final String beanName) {
Class<?> targetClass = getApplicationContext().getType(beanName);
final Class<?> userDeclaredTargetClass = (targetClass != null ? targetClass : ClassUtils.getUserDeclaredType(beanName, getApplicationContext()));
final Set<Method> methods = HandlerMethodSelector.selectMethods(userDeclaredTargetClass, new MethodFilter() {
@Override
public boolean matches(Method method) {
return getMappingForMethod(method, beanName) != null;
}
});
for (Method method : methods) {
T handlerMethod = createHandlerMethod(beanName, method);
if (handlerMethod != null) {
this.handlerMethods.put(createRequestMappingInfo(handlerMethod), handlerMethod);
}
}
}

在上面的代码中,首先通过getApplicationContext().getBeanNamesForType()方法获取所有实现了Object接口的Bean的名称。然后,对于每个Bean,通过getApplicationContext().getType()方法获取其类型,并通过isHandler()方法判断该类型是否是一个处理器类型。如果是处理器类型,则调用detectHandlerMethods()方法检测该Bean中所有的方法,找出符合要求的HandlerMethod,并将其添加到handlerMethods集合中。在detectHandlerMethods()方法中,首先通过HandlerMethodSelector.selectMethods()方法选出所有符合要求的方法,然后通过createHandlerMethod()方法创建相应的HandlerMethod对象,并通过createRequestMappingInfo()方法创建相应的RequestMappingInfo对象。最后,将RequestMappingInfo对象和HandlerMethod对象添加到handlerMethods集合中。

3、创建处理器适配器:创建好所有的HandlerMethod后,DispatcherServlet会为它们创建相应的处理器适配器。处理器适配器是一个封装了HandlerMethod和请求信息的对象,用于在处理请求时调用合适的处理器方法。在SpringMVC中,可以通过实现HandlerAdapter接口来创建自定义的处理器适配器。例如,以下代码演示了如何创建一个RequestMappingHandlerAdapter对象:

@Bean  
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
adapter.setContentNegotiationManager(contentNegotiationManager());
adapter.setMessageConverters(messageConverters());
return adapter;
}

在上面的代码中,通过创建一个RequestMappingHandlerAdapter对象,并设置其ContentNegotiationManager和MessageConverters属性,来创建一个自定义的处理器适配器。ContentNegotiationManager用于协商请求的内容类型,而MessageConverters用于将请求和响应的消息进行转换。

举报

相关推荐

0 条评论