spring 读取properties属性文件

本文介绍如何使用Spring框架及Java标准库读取配置文件。通过实例演示了利用Spring的PropertiesBeanDefinitionReader读取bean配置,并展示了如何使用java.util.Properties获取服务器IP和端口信息。

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

这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取
(一)利用spring读取properties 文件
我们还利用上面的HelloBean.java文件,构造如下b<wbr>eanConfig.properties文件:</wbr>
helloBean.class=chb.demo.vo.He<wbr>lloBean<br> helloBean.helloWorld=Hello!chb<wbr>!</wbr></wbr>
属性文件中的"helloBean"名称即是Bean的别名设定<wbr>,.class用于指定类<a class="link-s-e-o" title="来源" href="http://travel.mipang.com/3196/" target="_blank">来源</a>。</wbr>
然后利用org.springframework.beans.<wbr>factory.support.PropertiesBean<wbr>DefinitionReader来读取属性文件 <div style="background:rgb(230,230,230); padding:4px 5.4pt; border:0.5pt solid windowtext; width:95%; word-break:break-all"> <div><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt=""><span style="color:rgb(0,0,0)"> <wbr>BeanDefinitionRegistry<wbr> reg<wbr></wbr></wbr></wbr></span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">new</span><span style="color:rgb(0,0,0)"> DefaultListableBeanFacto<wbr>ry();<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt=""><wbr>PropertiesBeanDefinition<wbr>Reader<wbr> reader<wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">new</span><span style="color:rgb(0,0,0)"> PropertiesBeanDefinition<wbr>Reader(reg);<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt=""><wbr>reader.loadBeanDefinitio<wbr>ns(</wbr></wbr></wbr></wbr></span><span style="color:rgb(0,0,255)">new</span><span style="color:rgb(0,0,0)"> ClassPathResource(</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">beanConfig.properties</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">));<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt=""><wbr>BeanFactory<wbr> factory<wbr></wbr></wbr></wbr></wbr></span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(0,0,0)"> (BeanFactory)reg;<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt=""><wbr>HelloBean<wbr> helloBean<wbr></wbr></wbr></wbr></wbr></span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(0,0,0)"> (HelloBean)factory.getBe<wbr>an(</wbr></span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">helloBean</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">);<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt=""><wbr>System.out.println(hello<wbr>Bean.getHelloWorld());</wbr></wbr></wbr></span></wbr></div> </div> </wbr></wbr>
(二)利用java.util.Properties读取属性文<wbr>件</wbr>
比如,我们构造一个ipConfig.properties来保存服<wbr>务器ip地址和端口,如:</wbr>
ip=192.168.0.1
port=8080
则,我们可以用如下程序来获得服务器配置信息:
<wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt=""><span style="color:rgb(0,0,0)"> <wbr>InputStream<wbr> inputStream<wbr></wbr></wbr></wbr></span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">this</span><span style="color:rgb(0,0,0)">.getClass().getClassLoader().g<wbr>etResourceAsStream(</wbr></span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">ipConfig.properties</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">);<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt=""><wbr>Properties<wbr> p<wbr></wbr></wbr></wbr></wbr></span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">new</span><span style="color:rgb(0,0,0)"> Properties();<br><wbr><img id="_146_174_Open_Image" name="_146_174_Open_Image" align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" width="11" height="16" alt=""><wbr><img id="_146_174_Closed_Image" name="_146_174_Closed_Image" align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" width="11" height="16" alt="" style=""><wbr></wbr></wbr></wbr></span><span style="color:rgb(0,0,255)">try</span><span style="color:rgb(0,0,0)"> </span><span style="border:1px solid rgb(128,128,128); background-color:rgb(255,255,255)">...</span><span><span style="color:rgb(0,0,0)">{<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" width="11" height="16" alt=""><wbr><wbr>p.load(inputStream);<br><wbr><img id="_199_228_Open_Image" name="_199_228_Open_Image" align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" width="11" height="16" alt=""><wbr><img id="_199_228_Closed_Image" name="_199_228_Closed_Image" align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" width="11" height="16" alt="" style=""><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></span></span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">catch</span><span style="color:rgb(0,0,0)"> (IOException<wbr> e1)<wbr></wbr></wbr></span><span style="border:1px solid rgb(128,128,128); background-color:rgb(255,255,255)">...</span><span><span style="color:rgb(0,0,0)">{<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" width="11" height="16" alt=""><wbr><wbr>e1.printStackTrace();<br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" width="11" height="16" alt=""><wbr>}</wbr></wbr></wbr></wbr></wbr></span></span><span style="color:rgb(0,0,0)"><br><wbr><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" width="11" height="16" alt="">System.out.println(</wbr></span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">ip:</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">+</span><span style="color:rgb(0,0,0)">p.getProperty(</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">ip</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">)</span><span style="color:rgb(0,0,0)">+</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">,port:</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">+</span><span style="color:rgb(0,0,0)">p.getProperty(</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">port</span><span style="color:rgb(0,0,0)">"</span><span style="color:rgb(0,0,0)">));</span></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值