读书人

spring读取属性资料

发布时间: 2012-09-03 09:48:39 作者: rapoo

spring读取属性文件

bean.xml:
<bean id="propertyConfigurer" class="BundlePropertiesLoader">
<property name="bundlePropertyFileNames">
<list>
<value>test.properties</value>
</list>
</property>
</bean>

/**
* spring读取属性文件
*/
public class BundlePropertiesLoader extends PropertyPlaceholderConfigurer
{
??? public void setBundlePropertyFileNames( String[] fileNames )
??? {
??????? InputStream in = null;
??????? Properties pts = null;

??????? try
??????? {
??????????? // 读取spring文件中配置的属性文件名称
??????????? for ( String fileName : fileNames )
??????????? {
??????????????? in = BundlePropertiesLoader.class.getClassLoader()
??????????????????????? .getResourceAsStream( fileName );
??????????????? pts = new Properties();

??????????????? // 加载属性文件
??????????????? pts.load( in );
????????????? super.setProperties( pts );
??????????? }
??????? } catch ( Exception e )
??????? {??????
??????? }

????????finally

?????? {

????????? if (null != in)

???????? {

?????????????? in.close();

???????? }

?????? }
??? }

?

读书人网 >软件架构设计

热点推荐