• 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();
            }
        }
    }
  • 相关阅读:
    Java注解详解
    浅析@Deprecated
    BeanUtils使用概要
    使用内省方式操作JavaBean
    怎样通过ajax提交数据
    Java 反射学习笔记
    jackson 学习笔记
    Dom4j 学习笔记
    Tensorflow打造聊天机器人
    Hive官方文档
  • 原文地址:https://www.cnblogs.com/exmyth/p/5345323.html
Copyright © 2020-2023  润新知