实际的开发过程中,将一些配置属性从java代码中提取到properties文件中是个很好的选择,降低了代码的耦合度。下面介绍两种通过spring读取properties文件的方法,以ip地址配置为例。ip.properties文件:

host=127.0.01
port=8080
 1、 使用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:  @Component
   5:  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文件的读取的更多相关文章

  1. 关于properties文件的读取(Java/spring/springmvc/springboot)

    一.Java读取properties文件 1.基于ClassLoder读取配置文件 注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便. Properties prope ...

  2. 【Properties】获取Properties文件

    获取Properties文件 package com.chinamobile.epic.tako.v2.query.commons; import org.springframework.core.i ...

  3. java加载properties文件的六中基本方式实现

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...

  4. Java实现获取属性文件的参数值

    Java实现获取属性文件的参数值 1,属性文件内容(analysis.properties),路径必须在:src根目录下: #client data path analysis.client.data ...

  5. java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...

  6. 获取properties文件的内容

    获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...

  7. Java代码操作properties文件(读取,新增/修改,删除)

    项目中需要用到操作properties文件中的数据,记录一下 package com.bonc.savepic.save; import java.io.FileNotFoundException; ...

  8. java工具类获取properties文件的配置

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  9. JAVA中自定义properties文件介绍

    Gradle中的使用 1. 使用gradle.properties buid.gradle 和 gradle.properties可以项目使用,在同一个项目中,build.gradle可以直接获取其同 ...

随机推荐

  1. SGU 246. Black & White(数论)

    题意: 有2*n-1个黑色和白色的珠子组成的环形项链,求至少需要多少颗黑色珠子才能使任意排列的项链中都存在两个黑珠间有n个珠子. (2*n-1<=2^31-1); Solution: 先分析n= ...

  2. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  3. Object layout in C++ and access control

    The variables are guaranteed to be laid out contiguously, as in C. However, the access blocks may no ...

  4. LVS 介绍以及配置应用

    1.负载均衡集群介绍 1.1.什么是负载均衡集群 负载均衡集群提供了一种廉价.有效.透明的方法,来扩展网络设备和服务器的负载.带宽.增加吞吐量.加强网络数据的处理能力.提高网络的灵活性和可用性 搭建负 ...

  5. getting “fatal: not a git repository: '.'” when using post-update hook to execute 'git pull' on another repo

    Here is the script that ultimately worked. I think the bit I was originally missing that prevented i ...

  6. 安装sql server 2008,提示要删除SQL Server 2005 Express 工具 怎么解决?

    x86 修改注册表:HKLM\Software\Microsoft\Microsoft SQL Server\90\Tools\ShellSEM,把 ShellSEM重命名即可. x64       ...

  7. demo_04绘制三角形

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. jQuery获取Select选择的Text和 Value

    jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...});   //为Se ...

  9. 隐藏input边框(ie6、ie7)

    去掉元素的边框,我们一贯使用border:none; 但在ie6.ie7下的input[type=text]元素,并没有去掉. 两种解决方案: 1. border:none; 并设置背景backgro ...

  10. pip assert_source_matches_version(self)版本验证报错Source in %s has version %s, which satisfies requirement %s的解决方式

    在win8.1下为了安装flask模块,开始安装pip,结果发生了上篇博客里面的错误ntpath join(path, *paths) 发生UnicodeDecodeError.解决之后继续发现版本验 ...