工程启动加载.properties/.xml配置文件

工程目录:

demo.properties

today=2017-06-10

PropertiesMap.java

package com.dzpykj.common.utils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 将用户授权demo.properties文件读取到集合Map中
 */
@Component
public class PropertiesMap {

    private static final Logger logger = LoggerFactory.getLogger(PropertiesMap.class);
    public Map<String,String> getMap(){
        Properties props = new Properties();
        Map<String,String> map = new HashMap<String,String>();
        try {
            final String propertiesName = "demo";//必须与.properties文件路径一致
            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertiesName+".properties");
            props.load(in);

            Iterator<String> it=props.stringPropertyNames().iterator();
            while(it.hasNext()){
                String key=it.next();
                map.put(key, props.getProperty(key));
                //System.out.println(key+":"+props.getProperty(key));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return map;
    }
}
时间: 2024-08-30 00:43:12

工程启动加载.properties/.xml配置文件的相关文章

maven工程无法加载其他子模块配置文件

07:29:40,900 INFO ContextLoader:307 - Root WebApplicationContext: initialization started07:29:41,050 INFO XmlWebApplicationContext:578 - Refreshing Root WebApplicationContext: startup date [Wed Jul 04 07:29:41 CST 2018]; root of context hierarchy07:2

Tomcat(一):Tomcat启动时加载web.xml

server.xml配置文件样例: <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional inf

tomcat web.xml启动加载类

tomcat web.xml启动加载类 2010-03-30 22:31suitieming | 分类:JAVA相关 | 浏览3870次 我写了一个程序配在tomcat上 想启动tomcat的时候自动运行这个类 怎么配置XML文件类名Test 分享到: 2010-03-31 01:57提问者采纳 答题闯关,过关即送礼!快来参加~ 把你需要启动时自动运行的类做成一个Servlet,然后在web.xml做如下配置:  <servlet>   <servlet-name>StartupL

加载Properties配置文件

/** * 加载Properties配置文件 * * @author ZhangHaiNing * @param file 要读取的文件 * @return */ public static Properties getProp(String file){ Properties prop=new Properties(); try { String url = TelChargeDwr.class.getResource("").getPath().replaceAll("%

xml文件 加载properties文件的两种方法与注意事项

1.遇到的问题: 配置redisSpringContext.xml 时,遇到 properties加载失败,提示BeanDefinitionStoreException  和   java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx'  , 找了很久都找不到原因,文件路径和键名都没写错,加载语法也没有写错...这就很难受了. 直到检查web.xml文件,,我猜测,是不是因为 springcontext-*.x

Spring加载properties文件的两种方式

在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源代码,这样更加方便.在Spring中也可以这么做,而且Spring有两种加载properties文件的方式:基于xml方式和基于注解方式.下面分别讨论下这两种方式. 1. 通过xml方式加载properties文件 我们以Spring实例化dataSource为例,我们一般会在beans

WEB系统启动时加载Log4j的配置文件

如何在系统启动的时候加载log4j的配置文件呢? 1.自定义监听类并且继承“ServletContextListener”接口: 1 package cn.ibeans.common; 2 3 import java.io.File; 4 import java.util.List; 5 6 import javax.servlet.ServletContextEvent; 7 import javax.servlet.ServletContextListener; 8 9 import org

Spring学习(一)tomcat加载web.xml、以及项目集成Spring支持

tomcat容器加载web.xml 一. 1 .启动一个 WEB 项目的时候, WEB 容器会去读取它的配置文件 web.xml ,读取 <listener> 和 <context-param> 两个结点. 2 .紧急着,容创建一个 ServletContext ( servlet 上下文),这个 web 项目的所有部分都将共享这个上下文. 3 .容器将 <context-param> 转换为键值对,并交给 servletContext . 4 .容器创建 <li

spring 如何动态加载properties文件的内容

1. 在xml中配置你的properties路径: <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <!-- 指定资源文件基名称 jdbc为文件名,不包含扩展名 --&g