首页 > 编程知识 正文

java中的property文件,javaproperties文件

时间:2023-05-03 15:37:26 阅读:173928 作者:1582

方式1

1、如下制作PropertyUtil,能够读取哪个位置的properties文件

公共类属性实用工具{

publicstaticpropertiesgetconfig {

属性属性属性=空;

try{

props=new Properties (;

inputstream in=property util.class.getresourceasstream (path );

bufferedreaderbf=newbufferedreader (newinputstreamreader (in );

props.load(BF;

in.close (;

}catch(exceptionex ) {

ex.printStackTrace (;

}

return props;

}

}

2、需要的地方用常数注入。 例如,如下所述。

公共类配置{

publicstaticpropertiesprop=property util.getconfig ('/epay.properties );

私有字符串密钥;

公共字符串获取

returnprop.getproperty('key );

}

}

方式2

@Component

@ property source (' class path : epay.properties ) ) ) ) )。

公共类配置{

@value((${appid} ) ) ) ) )。

私有字符串应用程序;

@value((${key} ) ) ) )。

私有字符串密钥;

}

方式3

1、写PropertyUtil如下。

publicfinalclasspropertiesutilextendspropertyplaceholderconfigurer {

privatestaticmapctxpropertiesmap;

@Override

protectedvoidloadproperties (properties props ) throws IOException {

super.loadproperties(props );

ctxPropertiesMap=new HashMap (;

对象密钥: props.keyset () ) ) )。

String keyStr=key.toString (;

string value=props.getproperty (keystr;

cxpropertiesmap.put(keystr,value;

}

}

公共字符串键(string key ) {

try {

returnctxpropertiesmap.get(key;

} catch (缺少资源交换) {

返回空值;

}

}

}

2、可以放置在spring-context.xml中,在list中放置多个

classpath:epay.properties

比较总结三种方法。

关于编码量和结合的程度,方式2是最佳的

方法3相对灵活,如果在多个类中使用一个配置文件,则不需要为每个类注入一次,但必须由xml组成

-比较方式1和3,只是读取properties的方式不同,灵活性相同

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。