这里介绍两种在代码中获取properties文件属性的方法。

使用@Value注解获取properties文件属性:

1.因为在下面要用到Spring的<util />配置,所以,首先要在applicationContext.xml中引入其对应的命名空间:

xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util
            http://www.springframework.org/schema/util/spring-util-3.0.xsd"

2.创建properties文件并增加内容:

#搜索服务地址
solrURL=http://localhost:8080/solr 

3.在applicationContext.xml中加入以下的配置:

<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 注解默认扫描的包路径 -->
<context:component-scan base-package="com.wdcloud.solr" /> <!-- 载入配置文件 -->
<util:properties id="constants" location="classpath:config/statics.properties"/>

4.使用@Value注解,在java类中获取properties文件中的值(这里constants对应上面的id):

  @Value("#{constants.solrURL}")
public String testUrl; @RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public Result queryTest() {
System.out.println("testUrl:" + testUrl);
}

测试结果:

使用@Value获取属性值的方法有一个问题,我每用一次配置文件中的值,就要声明一个局部变量,即不能使用static和final来修饰变量。而第二种方法可以解决这个问题。

重写PropertyPlaceholderConfigurer:

1.通常我们使用spring的PropertyPlaceholderConfigurer类来读取配置信息,这里我们需要重写它:

public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {

    private static Map<String, String> propertyMap;

    @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
propertyMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertyMap.put(keyStr, value);
}
} // static method for accessing context properties
public static String getProperty(String name) {
return propertyMap.get(name);
}
}

2.在applicationContext.xml中加入以下的配置:

  <!-- 加载properties文件配置信息 -->
<bean scope="singleton" id="propertyConfigurer"
class="com.wdcloud.solr.util.PropertyPlaceholder">
<property name="locations">
<list>
<value>classpath*:config/statics.properties</value>
</list>
</property>
</bean>

3.使用PropertyPlaceholder.getProperty方法获取属性值:

  public static final String solrURL = PropertyPlaceholder.getProperty("solrURL");

    @RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public Result queryTest() {
System.out.println(solrURL);
}

测试结果:

参考:

http://1358440610-qq-com.iteye.com/blog/2090955

http://www.cnblogs.com/Gyoung/p/5507063.html

Spring在代码中获取properties文件属性的更多相关文章

  1. Spring在代码中获取bean的几种方式

    方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...

  2. Spring在代码中获取bean的几种方式(转:http://www.dexcoder.com/selfly/article/326)

    方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...

  3. Spring在代码中获取bean的几种方式(转)

    获取spring中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 ApplicationContext ac = new FileSystemXmlApplica ...

  4. java代码中获取进程process id(转)

    另一方面,线程ID=进程ID+内部线程对象ID并不成立,    参考: blog.csdn.net/heyetina/article/details/6633901     如何在java代码中获取进 ...

  5. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  6. Java代码中获取Json的key值

    测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym ...

  7. Android在代码中获取应用签名

    平时都是用AS敲命令获取签名信息...还没有在代码中获取过签名~ 也算是老编程了,没做过这个稍微有点尴尬...本着有好轮子就用的原则,网上找了几篇博客,这块内容已经很完善了,我也没什么可以优化的... ...

  8. java如何从一段html代码中获取图片的src路径

    java如何从一段html代码中获取图片的src路径 package com.cellstrain.icell.Test; import java.util.ArrayList;import java ...

  9. 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串

      1.在<connectionStrings> 标签里添加连接 <connectionStrings> <add name="ConnectionName&q ...

随机推荐

  1. virtualbox安装增强功能并设置共享文件夹

    virtualbox安装增强功能并设置共享文件夹 我们在安装之前,必须得先安装好它所需要的依赖包,不然安装过程必定会出现错误! 一.安装依赖包 #yum install kernel-headers# ...

  2. Oracle根据表的大小排序SQL语句

    --按照数据行数排序select table_name,blocks,num_rows from dba_tables where owner not like '%SYS%' and table_n ...

  3. hdu 1506 Largest Rectangle in a Histogram——笛卡尔树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 关于笛卡尔树的构建:https://www.cnblogs.com/reverymoon/p/952 ...

  4. JZ2440 裸机驱动 第13章 LCD控制器(2)

    13.2 TFT LCD显示实例 13.2.1 程序设计     本实例的目的是从串口输出一个菜单,从中选择各种方法进行测试,比如画线. 画圆.显示单色.使用调色板等. 13.2.2代码详解     ...

  5. Python 中的sort()排序

    v = [1, 3, 5, 2, 4, 6] v.sort() print(v) # [1, 2, 3, 4, 5, 6] v2 = [(1, 2), (2, 2), (2, 3), (3, 1)] ...

  6. Python中的偏函数

    偏函数是从Python2.5引入的一个概念,通过functools模块被用户调用. 偏函数是将所要承载的函数作为partial()函数的第一个参数,原函数的各个参数依次作为partial()函数后续的 ...

  7. python中将HTTP头部中的GMT时间转换成datetime时间格式

    原文: https://blog.csdn.net/zoulonglong/article/details/80585716 需求背景:目前在做接口的自动化测试平台,由于接口用例执行后返回的结果中的时 ...

  8. SpringBoot学习记(一)第一个SpringBoot Web服务

    工具IDEA 一.构建项目 1.选择Spring Initializr 2.填写项目信息 3.勾选webService 4.勾选Thymeleaf 5.项目建立完成,启动类自动生成 二.写个Contr ...

  9. 测试运行kafka的时候缺少包的错误

    把kafka安装好了,在开启Kafka producer生产者,消费者的时候报这个错误 解决方法: 下载slf4j-1.7.6.ziphttp://www.slf4j.org/dist/slf4j-1 ...

  10. 理解prototype

    从别人的博客里面盗了2张图,这2张图将对象/实例/prototype/__proto__/constructor之间的关系描绘的很清楚. 1.prototype 为 function的属性,实例化的对 ...