Loading a configuration file in the Web

Source: Internet
Author: User

Configuration in 1.web.xml

  <!-- 加载配置文件 -->  <listener    <description>ServletContextListener</description    <listener-class>com.lanhetech.application.ContextInitListener</listener-class  </listener>

2. Loading

importjava.io.FileInputStream;importjava.util.HashMap;import java.util.Map;importjava.util.Map.Entry;importjava.util.Properties;importjavax.servlet.ServletContext;importjavax.servlet.ServletContextEvent;importjavax.servlet.ServletContextListener;importorg.apache.log4j.Logger;// 加载配置文件到application中// test -- okpublicclassContextInitListener implementsServletContextListener {    privatestaticLogger logger = Logger.getLogger(ContextInitListener.class);    // web项目通常来说,一般来说相对路径是在/WEB-INF目录下    privatestaticfinalString BUSINESS_LOGIC_CONFIG_FILE_NAME = "business_logic.properties"// 业务逻辑的配置文件名    @Override    publicvoidcontextDestroyed(ServletContextEvent arg0) {        logger.info("自定义加载配置文件监听器销毁!");    }    @Override    publicvoid 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 = newProperties();        try{            String web_inf = contextEvent.getServletContext().getRealPath("/WEB-INF"); // 防止转义            String loadPath = web_inf + "//"+ BUSINESS_LOGIC_CONFIG_FILE_NAME;            logger.info("加载配置文件路径:"+ loadPath);            props.load(newFileInputStream(loadPath));        catch(Exception ex) {            logger.error("加载配置文件失败!!!!");            returnnull;        }        // 解析配置文件        Map<String, String> map = newHashMap<String, String>((Map) props);        logger.info("开始装载配置文件!完成!");        returnmap;    }    // 将配置加载到内存中Application    privatevoidloadConfigToApplication(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("开始写入配置到内存中!完成!");    }}

Used in 3.Spring:

WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();     ServletContext servletContext = webApplicationContext.getServletContext();         logger.info( "info获取请求:用户登录方法执行!" +  servletContext.getAttribute( "sm2_sign_ip" ));

Loading a configuration file in the Web

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.