您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

spring如何读取properties文件?(附代码)

2025/9/29 2:00:29发布21次查看
本篇文章给大家带来的内容是关于spring如何读取properties文件?(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
问题:
需要通过properties读取页面的所需楼盘的名称.为了以后便于修改.
解决:
可以通过spring的 propertiesfactorybean 读取properties属性,就不需要自己通过jdk的properties类编写程序读取信息.
<!-- 第二种方式是使用注解的方式注入,主要用在java代码中使用注解注入properties文件中相应的value值 --> <bean id="prop" class="org.springframework.beans.factory.config.propertiesfactorybean"> <property name="locations"><!-- 这里是propertiesfactorybean类,它也有个locations属性,也是接收一个数组,跟上面一样 --> <array> <value>classpath:recommondhouse.properties</value> </array> </property> <!-- 设置编码格式 --> <property name="fileencoding" value="utf-8"></property> </bean>
注意: 需要设置fileencoding,否则会出现乱码情况,在eclipse中也需要设置properties编码情况,否则页面会显示一堆字符和字母,无法显示汉字,eclipse中设置如下:
如图,修改3编码为utf-8,点击update即可.
随后通过@value注解通过get,set方法注入数据.
package com.fyinqing.util;import java.util.arraylist;import java.util.list;import org.springframework.beans.factory.annotation.value;import org.springframework.stereotype.component;@component("test")public class propertiesutil { @value("#{prop.name1}") private string name1; @value("#{prop.name2}") private string name2; @value("#{prop.name3}") private string name3; @value("#{prop.name4}") private string name4; public string getname2() { return name2; } public void setname2(string name2) { this.name2 = name2; } public string getname3() { return name3; } public void setname3(string name3) { this.name3 = name3; } public string getname4() { return name4; } public void setname4(string name4) { this.name4 = name4; } public string getname1() { return name1; } public void setname1(string name1) { this.name1 = name1; } public list<string> getnamelist(){ list<string> list = new arraylist<string>(); list.add(name1); list.add(name2); list.add(name3); list.add(name4); return list; }}
测试如下:(只写了关键代码)
@autowired propertiesutil proputil;@test public void test4() { system.out.println(proputil.getnamelist()); }
以上就是spring如何读取properties文件?(附代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product