专业的编程技术博客社区

网站首页 > 博客文章 正文

DispatcherServlet初始化过程-源码分析

baijin 2024-08-20 10:14:11 博客文章 5 ℃ 0 评论

DispatcherServlet的类继承图:

DispatcherServlet是一个Servlet,那么它就遵循Servlet的生命周期。如上图所示,DispatcherServlet还实现了Spring IOC的Aware接口,了解Aware接口的人都知道,Spring在创建对象的时候,会自动注入Aware接口方法里的对象。比如上图,会自动给DispatcherServlet注入Environment和ApplicationContext对象,如果你这么认为,那就大错特错了,只能说明你Spring学的不错。DispatcherServlet对象由Web容器(Tomcat)来管理,并不由Spring IOC管理,因此,根本就不可能自动注入Environment和ApplicationContext对象。这里的ApplicationContextAware和EnvironmentAware实际是作为普通接口使用,需要手动编程调用接口方法。

简化后的DispatcherServlet类继承图:

在了解DispatcherServlet的init()初始化方法之前,先了解它的static静态代码块。

private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties";

static {

// Load default strategy implementations from properties file.

// This is currently strictly internal and not meant to be customized

// by application developers.

try {

ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);

defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);

}

catch (IOException ex) {

throw new IllegalStateException("Could not load '" + DEFAULT_STRATEGIES_PATH + "': " + ex.getMessage());

}

}

静态代码块会读取 DispatcherServlet.properties 配置文件,该配置文件配置了默认的Spring MVC需要使用的一系列组件,当没有配置<mvc:annotation-driven />标签时,这些默认配置才会生效。

一般情况下,我们在Spring MVC 项目中,会配置<mvc:annotation-driven />标签。

DispatcherServlet.properties文件内容:

# Default implementation classes for DispatcherServlet's strategy interfaces.

# Used as fallback when no matching beans are found in the DispatcherServlet context.

# Not meant to be customized by application developers.

org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver

org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\

org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver,\

org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver

org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager

再次强调,SpringMVC通常不会使用这些默认的甚至过时的配置,添加<mvc:annotation-driven />标签,该标签会为我们注册当前最优秀的MVC组件。

如果配置了<mvc:annotation-driven />标签,则使用<mvc:annotation-driven />标签所绑定的SpringMVC基础组件。如果没有配置<mvc:annotation-driven />标签,则使用DispatcherServlet.properties配置文件内默认的SpringMVC基础组件 。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表