properties配置文件的读取
来源:互联网 发布:js中遍历数组对象 编辑:程序博客网 时间:2024/06/11 21:08
现在的项目为了更易维护,一般都是使用配置文件。这样做更灵活,可以很方便应对不同的环境。properties文件大家也见得多了,下面介绍一下properties的读取问题
一、普通类加载:
public FileSkServer() { Properties pro = new Properties(); pro.load(FileSkServer.class.getResourceAsStream("configure.properties"));//在跟类同一个目录里面的文件 String port = pro.getProperty("fileServer.port");//配置文件里的字段}使用pro.getProperty("fileServer.port");就可以获取fileServer.port的值了
二、spring MVC Controller加载方式:
1、首先在applicationContext.xml配置
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations" > <list> <value>classpath:config/configure.properties</value> </list> </property> </bean><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties" /> </bean>configure.properties是你的文件名,config是配置文件所在的目录,
结构
然后在Controller里中使用注解获得配置项内容:
@Value("#{configProperties['server.file.path']}")private String filePath;public void setFilePath(String filePath) {this.filePath = filePath;}
这样就可以直接获取配置项server.file.path了
------------------------------------20160510添加------------------------------------
三、通过ResourceBundle来加载
示例代码:
ResourceBundle bundle=ResourceBundle.getBundle("config");String driver=bundle.getString("driver");
创建一个默认的ResourceBundle对象
ResourceBundle会查找WEB-INF\classes\下的config.properties的文件
它跟普通java类的命名规则完全一样:
- 区分大小写
- 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样
- 资源文件必须位于指定包的路径之下(位于所指定的classpath中)
假如你是在非Web项目中使用,则一定要写资源文件的路径,也就是包路径必须存在。
如果是Web项目,不写包路径可以,此时将资源文件放在WEB-INF\classes\目录下就可以。
然后可以直接获取值,这里也是区分大小写的,大家注意
0 0
- 配置文件properties的读取
- properties配置文件的读取
- 读取配置文件properties的问题
- xml、properties配置文件的读取
- 读取配置文件properties的方法
- 读取配置文件properties的方法
- 读取Java的配置文件Properties
- Java配置文件properties的读取
- 配置文件中如何读取properties的配置文件
- 通过Spring读取properties配置文件的信息 Spring 读取properties
- 读取Properties的配置文件的信息
- 运用JAVA读取properties配置文件的研究
- 读取***.properties配置文件里数据的方法
- 配置文件读取的一种方法config.properties
- 读取properties配置文件的工具类
- 读取properties.xml配置文件的类
- properties配置文件的读取和使用
- 读取properties配置文件的方法汇总
- ubuntu安装和查看已安装
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
- Android Http请求方法汇总
- 交互设计起步指南总结
- makefile.win
- properties配置文件的读取
- Win7 64位下Visual Studio 2010连接Oracle数据库错误ORA-06413
- SSH框架系列:Spring配置多个数据源
- 用命令行删除Windows服务
- Linux中使用U盘
- SlidingMenu深入剖析
- C6000优化经验
- cocos2d-x 24种特效
- ubuntu nginx php 配置相关链接