zoukankan      html  css  js  c++  java
  • class ResultServletContextListener implements ServletContextListener

    package com.management.spring;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.TypeReference;
    
    
    public class ResultServletContextListener implements ServletContextListener  {
        
        private TypeReference<HashMap<String,String>> mapReference = new TypeReference<HashMap<String,String>>(){};
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
        }
    
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            System.out.println("=========================ResultMap开始加载...");
            ServletContext servletContext = servletContextEvent.getServletContext();
    //        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            
            try {
                InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/result.json");
                BufferedReader bis = new BufferedReader(new InputStreamReader(is));
                String line;
                StringBuffer json = new StringBuffer();
                while ((line = bis.readLine())!=null) {
                    json.append(line);
                }
                is.close();
                bis.close();
                HashMap<String, String> map = JSON.parseObject(json.toString(), mapReference);
                System.out.println(map);
                System.out.println("=========================ResultMap加载完毕...");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    package com.management.spring;
    
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.TypeReference;
    
    
    public class ResultServletContextListener implements ServletContextListener  {
        
        private TypeReference<HashMap<String,String>> mapReference = new TypeReference<HashMap<String,String>>(){};
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
        }
    
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            System.out.println("=========================ResultMap开始加载...");
            ServletContext servletContext = servletContextEvent.getServletContext();
    //        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            
            try {
                InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/result.json");
                BufferedInputStream bis = new BufferedInputStream(is);
                byte[] arr = new byte[1024];
                int len;
                StringBuffer json = new StringBuffer();
                while (-1 != (len = bis.read(arr, 0, 1024))) {
                    String str = new String(arr, 0, len);
                    json.append(str);
                }
                is.close();
                bis.close();
                HashMap<String, String> map = JSON.parseObject(json.toString(), mapReference);
                System.out.println(map);
                System.out.println("=========================ResultMap加载完毕...");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Unity下Reflection相关测试记录
    【疯狂造轮子-iOS】JSON转Model系列之一
    【原】FMDB源码阅读(三)
    【原】FMDB源码阅读(二)
    【原】FMDB源码阅读(一)
    (没时间维护,已下架)博客园第三方客户端-i博客园正式发布App Store
    【原】AFNetworking源码阅读(六)
    【原】AFNetworking源码阅读(五)
    【原】AFNetworking源码阅读(四)
    【原】AFNetworking源码阅读(三)
  • 原文地址:https://www.cnblogs.com/exmyth/p/5345323.html
Copyright © 2011-2022 走看看