Spring在代码中获取properties文件属性
这里介绍两种在代码中获取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文件属性的更多相关文章
- Spring在代码中获取bean的几种方式
方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...
- Spring在代码中获取bean的几种方式(转:http://www.dexcoder.com/selfly/article/326)
方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...
- Spring在代码中获取bean的几种方式(转)
获取spring中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 ApplicationContext ac = new FileSystemXmlApplica ...
- java代码中获取进程process id(转)
另一方面,线程ID=进程ID+内部线程对象ID并不成立, 参考: blog.csdn.net/heyetina/article/details/6633901 如何在java代码中获取进 ...
- spring mvc controller中获取request head内容
spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...
- Java代码中获取Json的key值
测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym ...
- Android在代码中获取应用签名
平时都是用AS敲命令获取签名信息...还没有在代码中获取过签名~ 也算是老编程了,没做过这个稍微有点尴尬...本着有好轮子就用的原则,网上找了几篇博客,这块内容已经很完善了,我也没什么可以优化的... ...
- java如何从一段html代码中获取图片的src路径
java如何从一段html代码中获取图片的src路径 package com.cellstrain.icell.Test; import java.util.ArrayList;import java ...
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
1.在<connectionStrings> 标签里添加连接 <connectionStrings> <add name="ConnectionName&q ...
随机推荐
- Spring 相关注解
spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller. 在目前的 Sprin ...
- 洛谷4030(Codeplus11月月赛)可做题1
题目:https://www.luogu.org/problemnew/show/P4030 原来一个方阵巧妙的充要条件是该方阵的每个2*2子方阵都是巧妙的!!! 可以把每一行选的列视为一个排列,需要 ...
- mySQL 教程 第2章 安装和介绍mySQL
设置mySQL字符集 支持中文的字符集是utf8,该设置可以更改mySQL配置文件进行全局设置,也可以针对数据库设置,也可以针对表设置,也可以针对列设置.字符集更改后新插入的数据生效,对以前不生效. ...
- react-router4.0的使用
近来很忙,学了一波react,特来记一笔,分享下react-router的使用方式 第一步引入内部组件 import {Route,BrowserRouter as Router,Switch,Lin ...
- XML-RPC简单使用
RPC(Remote Procedure Call)即远程方法调用,是一种在本地的机器上调用远端机器上的一个过程(方法)的技术.这个过程也被大家称为“分布式计算”,是为了提高各个分立机器的“互操作性” ...
- mysql分区表之三:MySQL分区建索引[转]
介绍 mysql分区后每个分区成了独立的文件,虽然从逻辑上还是一张表其实已经分成了多张独立的表,从“information_schema.INNODB_SYS_TABLES”系统表可以看到每个分区都存 ...
- 开发框架-APP:Hybird App
ylbtech-开发框架-APP:Hybird App Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App良好用户交互体 ...
- 查看iPhoneCPU、内存占用
使用Xcode可以查看iPhone cpu 内存 disk 网络占用读取 Xcode-Opem Developer Tool-Instruments, 在打开的窗口里选择Activity Minito ...
- 如何缓解DDOS攻击
1.减少攻击面 (a) reduce the number of necessary Internet entry points,(b) eliminate non-critical Internet ...
- 关于javascript的cookie的封装
/******************cookie*********************/ /* cookie的组成部分: 名称:唯一值,不区分大小写,必须经过URL编码 值:必须经过URL编码 ...