Java Bean 获取properties文件的读取
实际的开发过程中,将一些配置属性从java代码中提取到properties文件中是个很好的选择,降低了代码的耦合度。下面介绍两种通过spring读取properties文件的方法,以ip地址配置为例。ip.properties文件:
host=127.0.01
port=80801、 使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 类解析,在applicationContenxt.xml添加配置:<value>classpath*:/ip.properties</value>这样可以在其他bean定义中使用:<property name="host" value="${host}" />另外一种通过bean的@value注解实现:1: import org.springframework.beans.factory.annotation.Value;2: import org.springframework.stereotype.Component;3:4: @Component5: public class Sample{6:7: @Value("${host}")8: private String host;9: @Value("${port}")10: private String port;11: }
然后注入Sample 对象即可:@Autowired
private Sample sample;
2、使用org.springframework.core.io.support.PropertiesLoaderUtils 类来加载properties文件
1: import org.springframework.core.io.ClassPathResource;2: import org.springframework.core.io.Resource;3: import org.springframework.core.io.support.PropertiesLoaderUtils;4:5: Resource resource = new ClassPathResource("/ip.properties");6: Properties props = PropertiesLoaderUtils.loadProperties(resource);7: Set<Object> keys = props.keySet();8: for(Object key : keys){9: System.out.println(key+" : "+props.get(key));10: }
两种方法输出结果:
port : 8080
host : 127.0.01
Java Bean 获取properties文件的读取的更多相关文章
- 关于properties文件的读取(Java/spring/springmvc/springboot)
一.Java读取properties文件 1.基于ClassLoder读取配置文件 注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便. Properties prope ...
- 【Properties】获取Properties文件
获取Properties文件 package com.chinamobile.epic.tako.v2.query.commons; import org.springframework.core.i ...
- java加载properties文件的六中基本方式实现
java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...
- Java实现获取属性文件的参数值
Java实现获取属性文件的参数值 1,属性文件内容(analysis.properties),路径必须在:src根目录下: #client data path analysis.client.data ...
- java加载properties文件的六种方法总结
java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...
- 获取properties文件的内容
获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...
- Java代码操作properties文件(读取,新增/修改,删除)
项目中需要用到操作properties文件中的数据,记录一下 package com.bonc.savepic.save; import java.io.FileNotFoundException; ...
- java工具类获取properties文件的配置
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- JAVA中自定义properties文件介绍
Gradle中的使用 1. 使用gradle.properties buid.gradle 和 gradle.properties可以项目使用,在同一个项目中,build.gradle可以直接获取其同 ...
随机推荐
- Eclipse反编译工具Jad及插件
Eclipse反编译工具Jad及插件下载路径 http://download.csdn.net/detail/lijun7788/9689312 http://files.cnblogs.com/fi ...
- 【FJOI2014】【偏导+数学】病毒防护带
转载:http://trinklee.blog.163.com/blog/static/23815806020150155296528/ 问题描述: 众所周知,在国王胖哥的带领下,K国国泰民安,空前繁 ...
- 对象序列化XML
/// <summary>/// 对象序列化XML/// </summary>/// <param name="type">类型</par ...
- IS about 64bit system
This function supports the 64-bit parts of the registry by using the REGDB_OPTION_WOW64_64KEY option ...
- 搬瓦工vps搭建vpn
一.下载centos6一键安装包 wget --no-check-certificate https://raw.githubusercontent.com/teddysun/across/maste ...
- 异步请求HTTP
代码: @interface HttpProcessor : NSObject <NSURLConnectionDataDelegate> { NSMutableData *buffer; ...
- Hibernate中的一对多关系详解(2)
一对多的关系:例如,部门对员工,一个部门可以有多个员工 多对一的关系:例如,员工对部门,多个员工属于一个部门,并且每个员工只能属于一个部门 那么一对多.多对一在数据库中的是怎样表示的呢?好多话都不说了 ...
- SAP Crystal Dashboard Design 2011 win7 x64 安装
suggest: unZip the setup package to C:\dashboard\ (make sure that the path cannot contain non-unico ...
- Java serialVersionUID
1.为什么要使用serialVersionUID (1)对于实现了Serializable接口的类,可以将其序列化输出至磁盘文件中,同时会将其serialVersionUID输出到文件中. (2)然后 ...
- h.264 scanning process for transform coefficients
宏块在经过变换.量化后,得到大小为4x4或者8x8的矩阵,矩阵中的数据被称为transform coefficient levels.这些level在后面会被用于熵编码,因此我们需要把矩阵按照一定顺序 ...