Java,spring程序中实时获取.properties属性key的value
来源:互联网 发布:网络电影播放器 编辑:程序博客网 时间:2024/06/16 11:08
方法一:
public static String getValue(String fileNamePath, String key)throws IOException { Properties props = new Properties(); InputStream in = null; try { in = new FileInputStream(fileNamePath); // 如果将in改为下面的方法,必须要将.Properties文件和此class类文件放在同一个包中 //in = propertiesTools.class.getResourceAsStream(fileNamePath); props.load(in); String value = props.getProperty(key); // 有乱码时要进行重新编码 // new String(props.getProperty("name").getBytes("ISO-8859-1"), "GBK"); return value; } catch (IOException e) { e.printStackTrace(); return null; } finally { if (null != in) in.close(); } }
方法二:
通过spring配置properties文件
<bean id="propertyConfigurer" class="com.hapishop.util.ProjectDBinfoConfigurer"> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>app.properties</value> </list> </property></bean>
自定义类PorpertiesConfigurer
import java.util.HashMap;import java.util.Map;import java.util.Properties; import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class PorpertiesConfigurer extends PropertyPlaceholderConfigurer { private static Map ctxPropertiesMap; @Override protected void processProperties( ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropertiesMap = new HashMap(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } public static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); }}
续:关于程序中加载spring,自定义类实现spring 配置文件中通配符${jdbc.name}类似的正确解析,有大神说是重载PropertyPlaceholderConfigurer中的resolvePlaceholder(String placeholder, Properties props) 方法,但是看源码API说明,改方法仅是从传入的properties中获取参数一placeholder的值,是否可行,等之后做了osgi框架中,继承到spring的时候便可知道。我目前觉得是
- processProperties(
- ConfigurableListableBeanFactory beanFactoryToProcess,
- Properties props)
方法是干这事儿的。
下面是转载另外一位博友的文章:
<span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px; background-color: rgb(255, 255, 255);">在Spring初始化时,可以使用Properties配置器把properties文件装载到Spring的上下文中。</span>
Xml代码
- ...
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation=“http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd”
- ...
- <context:property-placeholder location="classpath:dataSource.properties" />
这样在Spring的配置文件中可以用表达式来获得load进来的properties内容,例如:
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
有时候我们在程序中也需要用到这些配置,那么如何取值,显然不能使用${}方式的。
这时要决定用什么方式来获取properties了,最方便的当然是直接读取文件,此处省略。
如果程序一定要用通过Spring加载的properties,那么我们首先要得到Context了。
1、FileSystemXmlApplicationContext——从指定的目录中加载:
- ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
2、ClassPathXmlApplicationContext——从classpath路径加载:
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
3、参照http://blog.csdn.net/dqatsh/article/details/3469278, 通过web.xml来获取Context。
4、在servlet中获取。
- ServletContext servletContext = servlet.getServletContext();
- WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
然后可以通过相应的bean去访问需要的properties(spring配置文件中${}方式设置到bean里)的值,这里不记录。
用PropertyPlaceholderConfigurer在加载上下文的时候暴露properties
- <bean id="configBean"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>hello.properties</value>
- </property>
- </bean>
表明PropertyPlaceholderConfigurer是承担properties读取任务的类。
下面的类继承PropertyPlaceholderConfigurer,通过重写processProperties方法把properties暴露出去了。
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
- import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
- public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
- private static Map<String, Object> ctxPropertiesMap;
- @Override
- protected void processProperties(ConfigurableListableBeanFactory beanFactory,
- Properties props)throws BeansException {
- super.processProperties(beanFactory, props);
- //load properties to ctxPropertiesMap
- ctxPropertiesMap = new HashMap<String, Object>();
- for (Object key : props.keySet()) {
- String keyStr = key.toString();
- String value = props.getProperty(keyStr);
- ctxPropertiesMap.put(keyStr, value);
- }
- }
- //static method for accessing context properties
- public static Object getContextProperty(String name) {
- return ctxPropertiesMap.get(name);
- }
- }
这样此类即完成了PropertyPlaceholderConfigurer的任务,同时又提供了上下文properties访问的功能。
于是在Spring配置文件中把PropertyPlaceholderConfigurer改成CustomizedPropertyConfigurer
- <!-- use customized properties configurer to expose properties to program -->
- <bean id="configBean"
- class="com.payment.taobaoNavigator.util.CustomizedPropertyConfigurer">
- <property name="location" value="classpath:dataSource.properties" />
- </bean>
最后在程序中我们便可以使用CustomizedPropertyConfigurer.getContextProperty()来取得上下文中的properties的值了。
- Java,spring程序中实时获取.properties属性key的value
- @Value spring 注解 获取*.properties 的属性值
- Java中如何获取spring中配置的properties属性文件内容
- Java中如何获取spring中配置的properties属性文件内容
- Java中如何获取spring中配置的properties属性文件内容
- Java中如何获取spring中配置的properties属性文件内容
- java 获取 Map中所有的 key 和 value 值
- java 获取map中所有的key和value值
- spring mvc controller中获取属性文件(properties)的值
- spring mvc controller中获取属性文件(properties)的值
- 属性attr(name|properties|key,value|fn)
- Spring 读取properties文件key+value方式
- 根据key从Properties文件中加载指定的value
- java Properties文件key-value值的读与写
- spring @value标签从properties文件中注入属性值
- Java中获取HashMap中所有的Key的值,获取所有的value的值
- PropertyPlaceholderConfigurer获取属性文件指定key的value值
- 由Java属性文件*.properties中getParameter(String key)出现乱码引发的探索
- 学生信息管理系统总结
- 编译器错误error: expected unqualified-id before 'using'| ||=== Build finished: 1
- hdu 2222 AC自动机入门详解
- LeetCode: Copy List with Random Pointer
- hdu 5383 Yu-Gi-Oh!(最大费用费用流)
- Java,spring程序中实时获取.properties属性key的value
- HDU 2563 统计问题 (DFS + 打表)
- 结构之美——优先队列三大结构(三)——Pairing Heap
- Visual studio 2013 批量删除空行的方式
- 黑马程序员———C语言———【枚举、typedef、宏、static和extern、文件操作】
- input文本框没有onchange事件,可以通过下面两种方法实现当input内容改变时触发事件
- Codeforces Round #316 (Div. 2) C. Replacement
- 关于Java多线程小结
- 枚举 - hdu5353 Average