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(); } } }