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

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

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

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

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

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

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

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

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

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

测试结果:

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

重写PropertyPlaceholderConfigurer:

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

  1. public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {
  2.  
  3. private static Map<String, String> propertyMap;
  4.  
  5. @Override
  6. protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
  7. throws BeansException {
  8. super.processProperties(beanFactoryToProcess, props);
  9. propertyMap = new HashMap<String, String>();
  10. for (Object key : props.keySet()) {
  11. String keyStr = key.toString();
  12. String value = props.getProperty(keyStr);
  13. propertyMap.put(keyStr, value);
  14. }
  15. }
  16.  
  17. // static method for accessing context properties
  18. public static String getProperty(String name) {
  19. return propertyMap.get(name);
  20. }
  21. }

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

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

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

  1.   public static final String solrURL = PropertyPlaceholder.getProperty("solrURL");
  2.  
  3. @RequestMapping(value = "/test", method = RequestMethod.GET)
  4. @ResponseBody
  5. public Result queryTest() {
  6. System.out.println(solrURL);
  7. }

测试结果:

参考:

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. 【转】Ubuntu添加PATH环境变量

    原文网址:http://www.cnblogs.com/pang123hui/archive/2011/05/28/2309889.html 添加分两种: 一.临时性添加 ~$ echo $PATH  ...

  2. gitlab-ce-omnibus社区版的备份、还原及升级

    gitlab-ce-omnibus社区版的备份和还原,可以使用gitlab自带工具,gitlab-rake来完成,详见下面例子 将旧gitlab服务器备份,并还原至新gitlab服务器 ,这两台git ...

  3. PHP 图片打水印

    <?php /*----------------------------------------------------------------------------------- *函数名称 ...

  4. Qt中路径问题小结

    转载:奋斗Andy 在做Qt项目的时候,我们难免遇到到文件路径问题. 如QFile file("text.txt")加载不成功.QPixmap("../text.png& ...

  5. Qt5布局管理(一)——QSplitter分割窗口类

    转载:LeeHDsniper 概述 本文首先通过三个实例分别介绍Qt5的分割窗口QSplitter类.停靠窗口QDockWidget类.堆栈窗体QStackedWidget类,然后介绍布局管理器的使用 ...

  6. wkhtmltopdf+itext实现html生成pdf文件的打印下载(适用于linux及windows)

    目中遇到个根据html转Java的功能,在java中我们itext可以快速的实现pdf打印下载的功能,在itext中我们一般有以下三中方式实现 配置pdf模板,通过Adobe Acrobat 来设置域 ...

  7. 关于Linux DNS部分处理

    如果不能ping 通外网地址进行解析的话进行如下处理

  8. Python 实现双向链表(图解)

    原文:https://blog.csdn.net/qq490691606/article/details/49948263 git 路径 https://github.com/wangpanjun/d ...

  9. 什么是最小可行性数据产品(MVP)?如何用它做机器学习?

  10. MySQL数据库Innodb储存引擎----储存页的结构

    上一篇博客回顾: 1:数据库拥有众多的储存引擎,现在主要使用的是Inoodb,这个储存引擎有Compact,Redundant,Dynamic,Compressed四种行格式 2:Compact行格式 ...