这里介绍两种在代码中获取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. 二、Jmeter录制脚本过程及Could not create script recorder报错、您的连接不是私密连接报错

    两个报错:Could not create script recorder报错和您的连接不是私密连接报错 1.录制过程 * 打开jmeter * 点击Templated,选择Recoding模版 * ...

  2. Servlet和JSP比较

    1. 两者哟许多相似之处,都可以生成动态网页 2. JSP的优点是擅长于网页制作,生成动态页面,比较直观. JSP的缺点是不容易跟踪与拍错 3. Servlet是纯Java语言,擅长流程处理和业务逻辑 ...

  3. kvm 基本运维命令

    Kvm基本命令 一.查询命令 1.列出所有的虚拟机 virsh list –all 2.显示虚拟机信息 virsh dominfo kvm-1 3.显示虚拟机内存和cpu的使用情况 yum insta ...

  4. linux 查看文件夹大小 du -h --max-depth=1 ./

    du:查询文件或文件夹的磁盘使用空间 如果当前目录下文件和文件夹很多,使用不带参数du的命令,可以循环列出所有文件和文件夹所使用的空间.这对查看究竟是那个地方过大是不利的,所以得指定深入目录的层数,参 ...

  5. android 布局入门

    一.LinearLayout RelativeLayout 这俩的区别详见这里 http://www.cnblogs.com/duanweishi/p/4244233.html 二.android:l ...

  6. 【JSP中引入文件】JSP中获取根路径+引用js文件

    在jsp界面中经常需要引入js.css等文件,通常都需要先获取项目根路径,然后再引入文件. 例如: 项目路径如下,需要在index.jsp中引入FusionCharts相关的js.css等: inde ...

  7. springMVC学习(12)-使用拦截器

    一.拦截器配置和测试: 1)定义两个拦截器,(要实现HandlerInterceptor接口) HandlerInterceptor1: package com.cy.interceptor; imp ...

  8. springMVC学习(8)-数据回显

    什么是数据回显: 提交后,如果出现错误(或者别的情况),将刚才提交的数据回显到刚才的提交页面. pojo数据回显方法: 一.springmvc默认对pojo数据进行回显. 比如现在的jsp页面提示出现 ...

  9. 术语-服务:BaaS

    ylbtech-术语-服务:BaaS BaaS(后端即服务:Backend as a Service)公司为移动应用开发者提供整合云后端的边界服务. 1.返回顶部 1. 中文名:后端即服务 外文名:B ...

  10. [转]预编译 ASP.NET 网站

    转自:如何:预编译 ASP.NET 网站 Visual Studio 2005   预编译 ASP.NET 网站可缩短用户的初始响应时间,因为页在第一次被请求时无需编译.这对于经常更新的大型网站尤其有 ...