配置文件工具类

参考:http://www.cnblogs.com/hafiz/p/5876243.html

有两种方式:

首先

看配置:

config.properties和config1.properties

util代码:

package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

public class ConfigLoadUtil {
	    private Logger logger = LoggerFactory.getLogger(ConfigLoadUtil.class);
	    private static final Properties properties = new Properties();
	    private String fileName;

	    private ConfigLoadUtil(String fileName) {
	        this.fileName = fileName;
	        load();
	    }
	    /**
	     * 加载配置文件
	     */
	    private void load() {
	        if (null != properties) {
	            InputStream in = null;
	            try {
	                in = new ClassPathResource(fileName).getInputStream();
	                if (null == in) {
	                    /* 从文件路径获取配置文件 */
	                    logger.debug("classpath not found filename!!");
	                    in = new FileInputStream(fileName);
	                    properties.load(in);
	                } else {
	                    properties.load(in);
	                }
	                logger.info("load config file success!");
	            } catch (FileNotFoundException e) {
	                logger.error("file not found!", e);
	            } catch (IOException e) {
	                logger.error("load config file error!", e);
	            } finally {
	                if (null != in) {
	                    try {
	                        in.close();
	                    } catch (IOException e) {
	                        logger.error("read config file error:", e);
	                    }
	                }
	            }
	        }
	    }

	    public static String getValue(String key) {
	    	new ConfigLoadUtil("config1.properties");
	        String value = null;
	        if (null != properties) {
	        	value = properties.getProperty(key);
	        }
	        return value;
	    }
	    
	    public static void main(String[] args) {
	    	new ConfigLoadUtil("config1.properties");
	        System.out.println(ConfigLoadUtil.getValue("toIcrmServerPort"));
	        
	    }
	}

test:

package auth.util;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public abstract class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationContext.xml" });
		context.start();
		System.out.println(ConfigUtil.getSql("myname"));
	}

}

配置文件config:

myname=yxy


配置文件config1:

yourname=lhf

spring配置:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:config.properties</value>
			</list>
		</property>
	</bean>
	<context:property-placeholder location="classpath:config1.properties" ignore-unresolvable="true" />
	<bean id="configUtil" class="auth.util.ConfigUtil">
        <constructor-arg name="fileName" value="config1.properties" />
    </bean>

如果配置了构造函数,那么util的getValue就不要new了

第二种方法是继承类

public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
	
    private static Map<String, Object> ctxPropertiesMap;  
  
    @Override  
    protected void processProperties(ConfigurableListableBeanFactory beanFactory,Properties props)throws BeansException {  
  
        super.processProperties(beanFactory, props);  
        
        ctxPropertiesMap = new HashMap<String, Object>();  
        for (Object key : props.keySet()) {  
            String keyStr = key.toString();  
            String value = props.getProperty(keyStr);  
            ctxPropertiesMap.put(keyStr, value);  
        }  
    }  
    
    public static Object getContextProperty(String name) {  
        return ctxPropertiesMap.get(name);  
    }
    
}

调用方法:

ip=(String) CustomizedPropertyConfigurer.getContextProperty("ip");

===

原文地址:https://www.cnblogs.com/JAYIT/p/7000314.html