DispatcherServlet的静态初始化

    /*** Name of the class path resource (relative to the DispatcherServlet class)* that defines DispatcherServlet's default strategy names.*/private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties";private static final Properties defaultStrategies;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 'DispatcherServlet.properties': " + ex.getMessage());}}

配置文件加载

    /*** Load properties from the given resource (in ISO-8859-1 encoding).* @param resource the resource to load from* @return the populated Properties instance* @throws IOException if loading failed* @see #fillProperties(java.util.Properties, Resource)*/public static Properties loadProperties(Resource resource) throws IOException {Properties props = new Properties();fillProperties(props, resource);return props;}

属性填充:

    /*** Fill the given properties from the given resource (in ISO-8859-1 encoding).* @param props the Properties instance to fill* @param resource the resource to load from* @throws IOException if loading failed*/public static void fillProperties(Properties props, Resource resource) throws IOException {InputStream is = resource.getInputStream();try {String filename = resource.getFilename();if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {props.loadFromXML(is);}else {props.load(is);}}finally {is.close();}}

我们从这个可以得到什么呢?

我们可以直接拿过来作为读取属性文件的工具类。

DispatcherServlet的策略初始化

初始化策略代码:

    /*** Initialize the strategy objects that this servlet uses.* <p>May be overridden in subclasses in order to initialize further strategy objects.*/protected void initStrategies(ApplicationContext context) {initMultipartResolver(context);initLocaleResolver(context);initThemeResolver(context);initHandlerMappings(context);initHandlerAdapters(context);initHandlerExceptionResolvers(context);initRequestToViewNameTranslator(context);initViewResolvers(context);initFlashMapManager(context);}

我们再看一个这个方法结构图:

从上面的图中我们可以看出它们都共同使用一个方法:

/*** Create a List of default strategy objects for the given strategy interface.* <p>The default implementation uses the "DispatcherServlet.properties" file (in the same* package as the DispatcherServlet class) to determine the class names. It instantiates* the strategy objects through the context's BeanFactory.* @param context the current WebApplicationContext* @param strategyInterface the strategy interface* @return the List of corresponding strategy objects*/@SuppressWarnings("unchecked")protected <T> List<T> getDefaultStrategies(ApplicationContext context, Class<T> strategyInterface) {String key = strategyInterface.getName();String value = defaultStrategies.getProperty(key);if (value != null) {String[] classNames = StringUtils.commaDelimitedListToStringArray(value);List<T> strategies = new ArrayList<T>(classNames.length);for (String className : classNames) {try {Class<?> clazz = ClassUtils.forName(className, DispatcherServlet.class.getClassLoader());Object strategy = createDefaultStrategy(context, clazz);strategies.add((T) strategy);}catch (ClassNotFoundException ex) {throw new BeanInitializationException("Could not find DispatcherServlet's default strategy class [" + className +"] for interface [" + key + "]", ex);}catch (LinkageError err) {throw new BeanInitializationException("Error loading DispatcherServlet's default strategy class [" + className +"] for interface [" + key + "]: problem with class file or dependent class", err);}}return strategies;}else {return new LinkedList<T>();}}

StringUtils 和ClassUtils 可以一个丰富的代码库哦。

更进一步 我们可以从spring的源码库查找所有的*utils.java类,发现一个巨大的代码库,从中学习和重新利用这类工具类库,我们可以完善自己的代码库,加快开发效率。

转载于:https://www.cnblogs.com/davidwang456/p/4125305.html

spring mvc DispatcherServlet详解之拾忆工具类utils相关推荐

  1. spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程

    整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:reques ...

  2. spring mvc DispatcherServlet详解之前传---FrameworkServlet

    做项目时碰到Controller不能使用aop进行拦截,从网上搜索得知:使用spring mvc 启动了两个context:applicationContext 和WebapplicationCont ...

  3. spring mvc DispatcherServlet详解之二---request通过Controller获取ModelAndView过程

    整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet通过request获取控制器Controller的过程,现在来讲解DispatcherServletDisp ...

  4. spring mvc DispatcherServlet详解之一--request通过HandlerMaping获取控制器Controller过程

    整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的第一步:获取控制器. HandlerMapping HandlerMappi ...

  5. spring mvc DispatcherServlet详解之四---视图渲染过程

    整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染.视图渲染的过程是在获取到ModelAndView后的过程 ...

  6. spring mvc DispatcherServlet详解之interceptor和filter的区别

    首先我们看一下spring mvc Interceptor的功能及实现: http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy ...

  7. spring mvc DispatcherServlet详解之一---处理请求深入解析

    要深入理解spring mvc的工作流程,就需要先了解spring mvc的架构: 从上图可以看到 前端控制器DispatcherServlet在其中起着主导作用,理解了DispatcherServl ...

  8. spring mvc DispatcherServlet详解之前传---前端控制器架构

    前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端.前端控制器既可以使用Filter实现 ...

  9. spring mvc DispatcherServlet详解前传---HttpServletBean类

    从上章里我们已经看到: DispatcherServlet extends FrameworkServlet FrameworkServlet extends HttpServletBean impl ...

最新文章

  1. cocos2d Labels and Fonts 标签和字体(附:关于Hiero的二三事)
  2. iOS:切换视图的第三种方式:UITabBarController标签栏控制器
  3. MyBatis 缓存详解-第三方缓存做二级缓存
  4. NFS方式挂载rootfs的设置方法
  5. disable path length limit_通过Antsword看绕过disable_functions
  6. 服务器低功耗cpu性能,节能省电家庭共享 7款低功耗处理器推荐
  7. Redis集群部署(半自动)
  8. 使用cronolog-1.6.2按日期截取Tomcat日志
  9. cpu利用率低linux,linux计算,cpu 利用率超低,如何处理?
  10. matlab如何设全局变量,请问MATLAB中如何修改全局变量
  11. fastcgi php 进程用户 lighttpd,说说lighttpd的fastcgi
  12. url data 模式(url scheme data)
  13. 怎样检测和应对数据集的Outliers
  14. 利用autojs制作抢购支付宝消费劵的手机脚本
  15. Excel导入导出百万级数据
  16. 【python】N的多次方
  17. DNS劫持原理、dns劫持有什么办法解决、DNS劫持原理与操作
  18. 2022.11.20 学习周报
  19. 2D和3D版本的重力游戏
  20. 001_C#我的第一个串口上位机软件

热门文章

  1. dicom多帧转换_Python解析多帧dicom数据详解
  2. axure中的拐弯箭头_Axure教程:实现菜单下拉效果
  3. 伺服步进电机选型软件_关于伺服步进电机的28个问题
  4. android webview重绘,Android-怎么判断android中WebView滑动到了低端
  5. matlab地图掩膜,要素轮廓线掩膜
  6. mysql的存储过程与事务_mysql的存储过程与事务入门
  7. vscode 运行html服务器运行_如何在vscode中调用浏览器运行html?
  8. 8.0ble设备 android_【胖猴小玩闹】智能门锁与BLE设备安全Part 4:一次BLE智能手环的小玩闹...
  9. 编写精美的聊天界面(左边显示接收消息,右边显示发送消息)(项目已上传GitHub)
  10. Python基础——Anaconda的安装使用