日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

spring mvc DispatcherServlet详解之拾忆工具类utils

發布時間:2025/4/5 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring mvc DispatcherServlet详解之拾忆工具类utils 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。