/**
* Created by ywq on 2016/6/30.
*/
@Named
public class PropertyConfig {
private static AbstractBeanFactory beanFactory = null;
private static final Map<String,String> cache = new oncurrentHashMap<>(); @Inject
public PropertyConfig(AbstractBeanFactory beanFactory) {
this.beanFactory = beanFactory;
} /**
* 根据key获取配置文件的Value
* @param key * @return
*/
public static String getProperty(String key) {
String propValue = "";
if(cache.containsKey(key)){
propValue = cache.get(key);
} else {
try {
propValue = beanFactory.resolveEmbeddedValue("${" + key.trim() + "}");
cache.put(key,propValue);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
}
return propValue;
}
}
Spring xml的配置
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:props/${property-path}.properties</value>
<value>classpath:important.properties</value>
</list>
</property>
</bean>
 
在项目中使用
String maxTimeInSecondsProp = PropertyConfig.getProperty("maxTimeInSeconds");
 

通过spring工厂读取property配置文件的更多相关文章

  1. spring+jidi读取property的配置文件

    在Spring项目中,你可能需要从properties文件中读入配置注入到bean中,例如数据库连接信息,memcached server的地址端口信息等,这些配置信息最好独立于jar包或者war包, ...

  2. (C)spring boot读取自定义配置文件时乱码解决办法

    这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子.接下来当然是读取简单的对象属性了. 于是按照网上各位大神教的,简单写了个对象book,如上一篇(B),其他配置不需要 ...

  3. Spring如何读取xml配置文件的

    我们通过一个小案例来看xml解析过程. 1. 导包 <dependencies> <!-- xml解析工具 --> <dependency> <groupId ...

  4. Java读取property配置文件

    读取配置文件已经成了Java程序员工作的一项必备技能. 配置文件的优点: 可维护性好 怎么个可维护性好呢? 它会让程序中变化的地方很灵活的配置,不需要修改代码. Java程序部署到服务器上去之后就变成 ...

  5. spring boot读取自定义配置文件时乱码解决办法

    @PropertySource(value = "classpath:book.yml", ignoreResourceNotFound = true,encoding = &qu ...

  6. spring读取prperties配置文件(2)

    接上篇,spring读取prperties配置文件(1),这一篇主要讲述spring如何用annotation的方式去读取自定义的配置文件. 这里我先定义好属性文件"user.propert ...

  7. spring读取prperties配置文件(1)

    博客地址http://www.cnblogs.com/shizhongtao/p/3438431.html 属性文件命名是*.properties,在java中,用类java.util.Propert ...

  8. Spring Boot的properties配置文件读取

    我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了.具体启动过程以前的博客写 ...

  9. spring boot 在不同环境下读取不同配置文件的一种方式

    在工程中,通常有根据不同的环境读取不同配置文件的需求,对于spring boot 来说,默认读取的是application.yml 或者 application.properties.为了区分不同的环 ...

随机推荐

  1. Android基本控件属性设置

    EditText: EditText去边框:<EditText  android:background="@null"  /> EditText的hint的字体大小的设 ...

  2. ajax get和post请求 后端接收并返回数据

    get请求$(function(){ //alert("23"); var x = "#page"; var y = "${ctx!}/static/ ...

  3. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

  4. Leetcode: Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  5. C# 类的介绍,参数传递,各种符号说法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 关于gem包下载网站的说明

  7. Maven2的配置文件settings.xml(转)

    当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使用settings.xml中的settings元素来确定这些配置.这包含了本地仓库位置 ...

  8. jQuery Ajax 简单的实现跨域请求

    html 代码清单: <script type="text/javascript" src="http://www.youxiaju.com/js/jquery-1 ...

  9. JSP-02- 使用JSP实现输出

    二. 使用JSP实现输出 JSP的页面构成: 静态内容.指令.表达式.Scriptlet.声明.动作.注释 Jsp脚本: 表达式.Scriptlet.声明 表达式: <%=  内容  %> ...

  10. 基于时间点恢复数据库stopat

    create database newtestdb use newtestdbgo drop table t1go create table t1 (id int not null identity( ...