Spring获取properties文件中的属性

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的方式,部署之后能够获取对应的属性值。

 

 

https://blog.csdn.net/wlfighter/article/details/52563605

 

转载于:https://www.cnblogs.com/0xcafedaddy/p/8999135.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值