配置文件工具类

博客介绍了Java中使用配置文件的两种方式。一是查看config.properties和config1.properties等配置文件,以及util代码等,若配置了构造函数,util的getValue无需new;二是通过继承类的方法,还给出了调用方法,并提供了参考链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值