web 中加载配置文件
1.web.xml中配置
<!-- 加载配置文件 --> <listener> <description>ServletContextListener</description> <listener-class>com.lanhetech.application.ContextInitListener</listener-class> </listener> |
2.加载
import java.io.FileInputStream;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Properties;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.apache.log4j.Logger;// 加载配置文件到application中// test -- okpublic class ContextInitListener implements ServletContextListener { private static Logger logger = Logger.getLogger(ContextInitListener.class); // web项目通常来说,一般来说相对路径是在/WEB-INF目录下 private static final String BUSINESS_LOGIC_CONFIG_FILE_NAME = "business_logic.properties"; // 业务逻辑的配置文件名 @Override public void contextDestroyed(ServletContextEvent arg0) { logger.info("自定义加载配置文件监听器销毁!"); } @Override public void contextInitialized(ServletContextEvent contextEvent) { logger.info("自定义加载配置文件监听器初始化!"); // 1.装载配置文件 Map<String, String> configsMap = this.loadBusinessLogicConfig(contextEvent); // 2.将配置加载到内存中Application this.loadConfigToApplication(contextEvent, configsMap); logger.info("自定义加载配置文件监听器初始化!完成!"); } // 装载配置文件 private Map<String, String> loadBusinessLogicConfig(ServletContextEvent contextEvent) { logger.info("开始装载配置文件!"); // 加载配置文件 Properties props = new Properties(); try { String web_inf = contextEvent.getServletContext().getRealPath("/WEB-INF"); // 防止转义 String loadPath = web_inf + "//" + BUSINESS_LOGIC_CONFIG_FILE_NAME; logger.info("加载配置文件路径:" + loadPath); props.load(new FileInputStream(loadPath)); } catch (Exception ex) { logger.error("加载配置文件失败!!!!"); return null; } // 解析配置文件 Map<String, String> map = new HashMap<String, String>((Map) props); logger.info("开始装载配置文件!完成!"); return map; } // 将配置加载到内存中Application private void loadConfigToApplication(ServletContextEvent contextEvent, Map<String, String> configsMap) { logger.info("开始写入配置到内存中!"); if (configsMap == null) { logger.error("配置参数为null,写入失败!!!"); return; } // 将配置文件里的值装入context属性,这样可以在JSP,SERVLET里调用 ServletContext context = contextEvent.getServletContext(); for (Entry<String, String> entry : configsMap.entrySet()) { logger.info("写入的key:" + entry.getKey() + " value:" + entry.getValue()); context.setAttribute(entry.getKey(), entry.getValue()); } logger.info("开始写入配置到内存中!完成!"); }} |
3.Spring中使用:
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = webApplicationContext.getServletContext(); logger.info("info获取请求:用户登录方法执行!"+ servletContext.getAttribute("sm2_sign_ip"));web 中加载配置文件的更多相关文章
- Spring中加载配置文件的方式
原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...
- Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .
我们往常进行文件的加载的时候 用到的都是 FileInputStream进行 文件的加载比如下面一个例子 : InputStream in=FileInputStream("1.prope ...
- spring controller中@Value取不到applicationContext.xml中加载配置文件的问题
原因还未查证: http://sunjun041640.blog.163.com/blog/static/256268322014127113844746/ 在使用spring mvc时,实际上是两个 ...
- .Net Core Web应用加载读取Json配置文件
⒈添加Json配置文件并将“复制到输出目录”属性设置为“始终复制” { "Logging": { "LogLevel": { "Default&quo ...
- 想要配置文件生效 需要通过添加到web.xml加载到内存中
想要配置文件生效 需要通过添加到web.xml加载到内存中
- spring的配置文件在web.xml中加载的方式
web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且 ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- unreal3对象属性自动从配置文件中加载的机制
unrealscript中有两个与属性自动配置相关的关键字: config/globalconfig 当把它们应用于属性时,对象在创建后,该属性的初始值会被自动设置为相对应ini文件中的值. 举例来说 ...
- springboot属性类自动加载配置文件中的值
springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...
随机推荐
- CSS中background-position使用技巧
一.background-position:left top; 背景图片的左上角和容器(container)的左上角对齐,超出的部分隐藏.等同于 background-position:0,0;也等同 ...
- hadoop之shuffle详解
Shuffle描述着数据从map task输出到reduce task输入的这段过程. 如map 端的细节图,Shuffle在reduce端的过程也能用图上标明的三点来概括.当前reduce copy ...
- 【Foreign】划分序列 [线段树][DP]
划分序列 Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 9 4 ...
- bzoj4886 [Lydsy2017年5月月赛]叠塔游戏
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4886 [题解] 跟bzoj4883:http://www.cnblogs.com/galax ...
- codechef T6 Pishty and tree dfs序+线段树
PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...
- 【洛谷 P1631】 序列合并 (堆)
题目链接 直接暴力搞是\(n\)方的复杂度.\(n^2\)个数选\(n\)个最小的,容易想到堆. 我们堆里记录两个信息:到\(A\)数组哪个位置了,到\(B\)数组哪个位置了, 我直接把这两个信息存在 ...
- [bzoj3994][SDOI2015]约数个数和-数论
Brief Description 计算\(\sum_{i\leqslant n}\sum_{j\leqslant m}\sigma_0(ij)\). Algorithm Design 首先证明一个结 ...
- echarts图表自适应浏览器窗口的大小
echarts问题描述 当浏览器窗口发生变化时,echarts图表未能自适应浏览器的变化,产生如下的效果 解决方案 在$(function(){}中添加 window.onresize = funct ...
- 转:Android 调试桥(adb)是多种用途的工具
转自:http://my.oschina.net/xuwa/blog/1574 Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你管理设备或模拟器 的状态. 可以通过下列几种方法加入 ...
- linux内核分析笔记----中断和中断处理程序【转】
转自:http://www.cnblogs.com/hanyan225/archive/2011/07/17/2108609.html 中断还是中断,我讲了很多次的中断了,今天还是要讲中断,为啥呢?因 ...