Spring获取properties文件中的属性
来源:互联网 发布:淘宝云客服报名入口 编辑:程序博客网 时间:2024/05/28 05:14
1.前言
本文主要是对这两篇blog的整理,感谢作者的分享
Spring使用程序方式读取properties文件
Spring通过@Value注解注入属性的几种方式
2.配置文件
application.properties
socket.time.out=1000
3.使用spring代码直接载入配置文件,获取属性信息
代码如下:
Resource resource = new ClassPathResource("/application.properties");Properties props = PropertiesLoaderUtils.loadProperties(resource);
4.使用@Value注解获取属性
4.1 使用PropertyPlaceholderConfigurer
spring配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application.properties" /></bean>
代码
@Value("${socket.time.out}")int socketTimeout;
4.2 使用PropertiesFactoryBean
Spring配置
<bean id="application" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:application.properties" /></bean>
代码
@Value("#{$application['socket.time.out']}")int socketTimeOut;
4.3 备注:
如果将代码部署到resin容器,使用4.1的方法,在程序启动时,总是报无法将”${socket.time.out}”转换成整数的错误。这说明程序并没有找到对应的配置属性。但是在进行单元测试的使用使用ApplicationContext时,则能够找到对应的属性。这可能是在容器里面使用的是WebApplicationContext的问题吧。目前还没有找到确切的原因,现在这里mark一下。
使用4.2的方式,部署之后能够获取对应的属性值。
0 0
- Spring获取properties文件中的属性
- 获取属性文件properties
- spring的属性加载器,加载properties文件中的属性
- Spring 依赖注入:获取properties文件中属性值
- spring mvc controller中获取属性文件(properties)的值
- spring mvc controller中获取属性文件(properties)的值
- web工程下,获取属性文件properties中的值
- SpringMVC 使用 @Value获取properties文件中的属性值
- 使用spring的PropertyPlaceholderConfigurer加密properties文件中的属性
- spring的xml配置文件读取properties文件中的属性
- spring 读取properties属性文件
- spring配置文件获取其他properties文件中的值及使用
- spring基于注解的方式获取.properties文件中的数据
- ApplicationContextAware获取Spring上下文,通过基类获取properties中的配置属性
- 获取.properties 配置中的属性名
- 使用Properties从文件获取属性
- spring中属性文件properties的读取
- Spring通过.properties文件引入属性配置
- 数据结构实验之链表三:链表的逆置
- 4.Android模拟器上,滑动出现C问题解决
- axis webservice tomcat 服务发布总结之相关流程和问题汇总
- 【Leetcode】22.Generate Parentheses【DFS】
- $.fn.extend(); 和 $.extend();
- Spring获取properties文件中的属性
- 在Drupal中使用Restful之一
- 4-1 复数类的运算符重载
- HTTP方法--Put 还是 Patch?
- poj 1562 Oil Deposits
- iOS动画详解
- 二叉树中两个节点的最近公共节点
- U盘启动安装Cent0S 6.4安装中问题:安装程序试图挂载映像#1,但在硬盘上无法找到该映像...
- Linux服务器网络故障诊断