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可以直接获取其同 ...
随机推荐
- LINUX 压缩目录成一个压缩文件
#!/bin/bash file =$(date +%y%m%d%H%M)logfile=/home/目录名/backup/file.log echo "------"$(date ...
- iOS: AFNetworking手动配置(iOS7.1, AF2.2.4)
一.下载AFNetworking. 二.将AFNetworking-master下的AFNetworking目录拖入到项目中 三.为项目添加Linking to a Library or Framew ...
- C语言学习笔记(一):数组传递时退化为指针
这几天闲来无事,写了一个数组元素排序函数如下: #include <stdio.h> #include <stdlib.h> void ArraySort(int array[ ...
- Junit4拓展工具JCategory与Testng的Group功能比较
前言 笔者前段时间分享过一遍文章,关于如何通过引入新注解来扩展Junit4,以解决Process上的问题: JUnit扩展:引入新注解Annotation 最近在跟外面的同事聊的时候,得知Testng ...
- LightOj_1284 Lights inside 3D Grid
题目链接 题意: 给一个X * Y * Z 的立方体, 每个单位立方体内都有一盏灯, 初始状态是灭的, 你每次操作如下: 1)选择一个点(x1, y1, z1) 再选择一个点(x2, y2, ...
- WPF Canvas小例子
源码下载:DraggingElementsInCanvas_demo.rar
- eclipse的使用、优化配置
一.简介 eclipse 可谓是Java开发界的神器,基本占据了大部分的Java开发市场,而且其官方还对其他语言提供支持,如C++,Ruby,JavaScript等等.为 什么使用它?我想离不开下面的 ...
- spoj gss2 : Can you answer these queries II 离线&&线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
- Python on Android
Python on Android Posted on April 29, 2015 by Alexander Taylor There are an increasing number of r ...
- 小测几种python web server的性能
http://blog.csdn.net/raptor/article/details/8038476 因为换了nginx就不再使用mod_wsgi来跑web.py应用了,现在用的是gevent-ws ...