这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取
(一)利用spring读取properties 文件
我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:
properties 代码
1. helloBean.class=chb.demo.vo.HelloBean
2. helloBean.helloWorld=Hello!chb!
属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。
然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件
java 代码
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new propertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties"));
BeanFactory factory = (BeanFactory)reg;
HelloBean helloBean = (HelloBean)factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
(二)利用java.util.Properties读取属性文件
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:
properties 代码
1. ip=192.168.0.1
2. port=8080
则,我们可以用如下程序来获得服务器配置信息:
java 代码
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
} System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));
分享到:
相关推荐
主要介绍了Python实现读取Properties配置文件的方法,结合实例形式分析了Python读取Properties配置文件类的定义与使用相关操作技巧,需要的朋友可以参考下
java如何读取properties配置文件
spring读取properties配置文件的key值。
主要介绍了java简单读取properties配置文件的方法,涉及java针对properties配置的载入及文件属性读取相关操作技巧,需要的朋友可以参考下
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容
c#操作properties,读写配置文件,非常方便,几行代码就搞定
读取配置文件所用jar包,本人亲测,可用。 使用PropertiesConfiguration读取配置文件
NULL 博文链接:https://liuzidong.iteye.com/blog/776637
使用Java中的Properties类的方法,以及类加载器来读取配置文件中的信息。可以读取.properties文件和.cfg文件。详细代码+注释+测试例子
主要介绍了Python读取properties配置文件操作,结合实例形式分析了Python读取properties配置文件操作中增加对键值对支持与使用的相关技巧,需要的朋友可以参考下
大家都喜欢把配置文件放在src目录下,如果有10个以上的配置文件为什么不考虑在WEB-INF目录下新建一个文件夹,专门放配置文件;这样即好管理,文件安全性又高。亲问题已经解决,把源代码共享给大家,已经通过测试;...
Android读取properties配置文件的实例详解 因为一些配置信息,多处用到的。且以后可能变更的,我想写个.prorperties配置文件给管理起来。 我把配置文件放在了assets文件夹下 appConfig.properties: serverUrl=...
NULL 博文链接:https://bijian1013.iteye.com/blog/2307358
主要介绍了java读取properties配置文件的方法,涉及java操作properties配置文件的相关技巧,需要的朋友可以参考下
读取properties文件返回map
java中properties文件读取demo
主要介绍了SPRINGBOOT读取PROPERTIES配置文件数据过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下