通过spring工厂读取property配置文件

/**
 * 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");
 
时间: 2024-08-02 23:03:10

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

读取property配置文件

[支持main方法测试] ResourceBundle resourceBundle = ResourceBundle.getBundle("service");        String url = resourceBundle.getString("serviceid");        System.out.println(url); service.properties [配置文件,放在src根目录下] serviceid=1 [不支持中文] 读取prop

Spring整合mybatis框架-Spring框架读取数据库配置文件

通过前面的例子我们能够知道Spring框架能够链接数据库,,但是通常情况下,我们的Spring核心配置文件中配置的内容会比较多,如果我么后期数据库链接有改动的话,去这么大的一个配置文件中修改数据库的链接是不怎么友好的,所以建议将数据库的配置单独分离出来,最后我们只通过Spring框架去读取数据库的配置文件,这样做是比较好的,实际项目也是这样做的. datasource.properties数据库配置文件 applicationContext.xml核心配置文件,读取properties文件 编写

如题,properties配置文件在项目中是经常用到的,那么读取properties配置文件的方法有哪些呢?

方法一:可以通过java.util.Properties类的load()方法 1 InputStreamin=lnewBufferedInputStream(newFileInputStream(name)); 2 Propertiesp=newProperties(); 3 p.load(in); 方法二:利用spring来读取properties配置文件org.springframework.beans.factory.support.PropertiesBeanDefinitionRead

Spring利用propertyConfigurer类 读取.property数据库配置文件

1.Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中 一些动态设定的值(value),在XML中替换为占位该键($key$)的值, .properties文件可以根据客户需求,自定义一些相关的参数,这样的设计可提供程序的灵活性. 2.在Spring中,使用PropertyPlaceholderConfigurer可以在XM

Spring Boot的properties配置文件读取

我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了.具体启动过程以前的博客写过Spring Boot启动过程(一),这次入口在SpringApplication类中: private ConfigurableEnvironment prepareEnvironment( SpringApplicationRunListeners listeners, Application

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

在工程中,通常有根据不同的环境读取不同配置文件的需求,对于spring boot 来说,默认读取的是application.yml 或者 application.properties.为了区分不同的环境,也提供了profile的机制.例如,当有一个开发环境的配置文件为application-dev.yml的时候,通过设置 spring.profiles.actives = dev, 程序启动的时候,会优先取 application-dev.yml中的值,然后再去取 application.yml

Spring工厂模式

普通工厂模式和抽象共产模式的优缺点:(1)普通工厂模式能够在单一产品维度上进行扩展,但是增加一个产品就要增加一个相应的工厂类,这样就会造成工厂的泛滥:(2)抽象工厂模式:很好的实现了一批产品的更新,但是,另一方面在单一产品维度上不好扩展. 而Spring的工厂模式原理是将Bean的信息放在配置文件当中.我们要模拟的话,可以用Java当中提供的一个特殊的类java.util.Properties,从而将类名信息在bean.properties文件中进行配置.将配置信息读取出来后,再利用反射技术进行

Spring使用外部的配置文件

在使用Spring做web项目的时候,通常会使用到数据库的连接信息 jdbcUrl driverClass username password 那么应该如何使用这些属性呢? 如在Spring中使用数据库连接池(数据源) 你可能会这样写 <!-- 配置数据库连接池 ComboPooledDataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource&qu

【XML配置文件读取】使用jdom读取XML配置文件信息

在项目中我们经常需要将配置信息写在配置文件中,而XML配置文件是常用的格式. 下面将介绍如何通过jdom来读取xml配置文件信息. 配置文件信息 <?xml version="1.0" encoding="UTF-8"?> <config> <base-config> <stringValue>Hello world</stringValue> <integerValue>8</integ