您的位置:首页 > 编程语言 > Java开发

Spring MVC读取properties文件时遇到的问题

2017-09-01 11:25 190 查看
java读取properties配置文件方式有很多种,如果需要了解,自行google

1,spring项目读取properties文件,需要在配置文件中加

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/db.properties</value>
<value>classpath:config/url.properties</value>
<value>classpath:config/parameter.properties</value>
</list>
</property>

</bean>

类似这种的配置,but

Controller和service的代码里,竟然需要两份配置,详情看这里:
http://www.cnblogs.com/shoren/p/spring.html
问题当时是,我在service中已经读取过配置文件信息了,但是在Controller中怎么就获取不到,最后需要配置两份上面的东西

感觉这个问题最终到了,spring配置文件的的问题了,这些配置文件的位置到底对项目有什么影响,以后再查,先记录下。

2、properties读取中文乱码

地址:http://www.cnblogs.com/kevin-yuan/archive/2012/08/25/2655851.html

总结起来就是,properties有自己的默认编码,读出来的时候不一致就乱了。

(当时自己的代码例子:String
cityList
= new
String(ele_contract_city_allow.getBytes("ISO8859-1"),
"UTF-8");

ele_contract_city_allow是在properties中读出的属性)

3、顺带一个别的常识

判断String为空效率较高的方法

if( str==null || str.length <= 0 ){}

不要用   "".equals(str) ,效率不高
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: