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 -- ok
public 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 中加载配置文件的更多相关文章

  1. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  2. Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .

    我们往常进行文件的加载的时候 用到的都是  FileInputStream进行 文件的加载比如下面一个例子 : InputStream in=FileInputStream("1.prope ...

  3. spring controller中@Value取不到applicationContext.xml中加载配置文件的问题

    原因还未查证: http://sunjun041640.blog.163.com/blog/static/256268322014127113844746/ 在使用spring mvc时,实际上是两个 ...

  4. .Net Core Web应用加载读取Json配置文件

    ⒈添加Json配置文件并将“复制到输出目录”属性设置为“始终复制” { "Logging": { "LogLevel": { "Default&quo ...

  5. 想要配置文件生效 需要通过添加到web.xml加载到内存中

    想要配置文件生效 需要通过添加到web.xml加载到内存中

  6. spring的配置文件在web.xml中加载的方式

    web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且 ...

  7. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  8. unreal3对象属性自动从配置文件中加载的机制

    unrealscript中有两个与属性自动配置相关的关键字: config/globalconfig 当把它们应用于属性时,对象在创建后,该属性的初始值会被自动设置为相对应ini文件中的值. 举例来说 ...

  9. springboot属性类自动加载配置文件中的值

    springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...

随机推荐

  1. BZOJ 4823: [Cqoi2017]老C的方块

    分析: 我觉得我的网络流白学了...QAQ... 其实数据范围本是无法用网络流跑过去的,然而出题者想让他跑过去,也就跑过去了... 看到题目其实感觉很麻烦,不知道从哪里入手,那么仔细观察所给出的有用信 ...

  2. 【20160811】noip模拟-未完

    T1 T2 T3 小奇回地球 [问题描述] 简单来说,它要从标号为1的星球到标号为n的星球,某一些星球之间有航线.由于超时空隧道的存在,从一个星球到另一个星球时间可能会倒流,而且,从星球a到b耗费的时 ...

  3. python收集

    http://my.oschina.net/mutour/blog/?disp=2&catalog=0&sort=time&p=4 Python 不使用win32api打开任务 ...

  4. POJ2680(动态规划,大数)

    Computer Transformation Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4548   Accepted ...

  5. POJ1236 (强连通分量缩点求入度为0和出度为0的分量个数)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13804   Accepted: 55 ...

  6. Linux执行shell脚本方式及区别&命令后台运行

    Linux执行shell脚本方式及区别&命令后台运行 http://blog.csdn.net/heqiyu34/article/details/19089951/

  7. Multi-Paxos协议日志同步应用

    使用Multi-Paxos协议的日志同步与恢复 基于Basic-Paxos协议的日志同步方案, 所有成员的身份都是平等的, 任何成员都可以提出日志持久化的提案, 并且尝试在成员组中进行持久化. 而在实 ...

  8. python访问网站

    #!/usr/bin/env python # encoding: utf-8 from functools import wraps import requests from lxml import ...

  9. 【bzoj2796】 [Poi2012]Fibonacci Representation

    给出一个数字,用FIB数列各项加加减减来得到. 问最少要多少个(可以重复使用) 大概试了一下,fibonacci数列的增长是很快的,大概到了90+项就超过了题目范围…… 所以每次找一个最近的fibon ...

  10. RBTree 红黑树

    红黑树 一.红黑树概述 红黑树不仅是一个二叉搜索树,并且满足以下规则: 1>每个节点不是红的就是黑的, 2>根结点为黑色, 3>如果节点为红色,其子节点必须为黑色, 4>任一节 ...