spring mvc DispatcherServlet详解之拾忆工具类utils
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse 常见问题及解决
- 下一篇: easy ui example