Java,spring程序中实时获取.properties属性key的value
2015-08-14 16:05
155 查看
方法一:
方法二:
续:关于程序中加载spring,自定义类实现spring 配置文件中通配符${jdbc.name}类似的正确解析,有大神说是重载PropertyPlaceholderConfigurer中的resolvePlaceholder(String placeholder, Properties props) 方法,但是看源码API说明,改方法仅是从传入的properties中获取参数一placeholder的值,是否可行,等之后做了osgi框架中,继承到spring的时候便可知道。我目前觉得是
processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props)
方法是干这事儿的。
下面是转载另外一位博友的文章:
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内容,例如:
Xml代码
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
有时候我们在程序中也需要用到这些配置,那么如何取值,显然不能使用${}方式的。
这时要决定用什么方式来获取properties了,最方便的当然是直接读取文件,此处省略。
如果程序一定要用通过Spring加载的properties,那么我们首先要得到Context了。
1、FileSystemXmlApplicationContext——从指定的目录中加载:
Java代码
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
2、ClassPathXmlApplicationContext——从classpath路径加载:
Java代码
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
3、参照/article/8529578.html, 通过web.xml来获取Context。
4、在servlet中获取。
Java代码
ServletContext servletContext = servlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
然后可以通过相应的bean去访问需要的properties(spring配置文件中${}方式设置到bean里)的值,这里不记录。
用PropertyPlaceholderConfigurer在加载上下文的时候暴露properties
Java代码
<bean id="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>hello.properties</value>
</property>
</bean>
表明PropertyPlaceholderConfigurer是承担properties读取任务的类。
下面的类继承PropertyPlaceholderConfigurer,通过重写processProperties方法把properties暴露出去了。
Java代码
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
Xml代码
<!-- 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的值了。
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内容,例如:
Xml代码
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
有时候我们在程序中也需要用到这些配置,那么如何取值,显然不能使用${}方式的。
这时要决定用什么方式来获取properties了,最方便的当然是直接读取文件,此处省略。
如果程序一定要用通过Spring加载的properties,那么我们首先要得到Context了。
1、FileSystemXmlApplicationContext——从指定的目录中加载:
Java代码
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
2、ClassPathXmlApplicationContext——从classpath路径加载:
Java代码
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
3、参照/article/8529578.html, 通过web.xml来获取Context。
4、在servlet中获取。
Java代码
ServletContext servletContext = servlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
然后可以通过相应的bean去访问需要的properties(spring配置文件中${}方式设置到bean里)的值,这里不记录。
用PropertyPlaceholderConfigurer在加载上下文的时候暴露properties
Java代码
<bean id="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>hello.properties</value>
</property>
</bean>
表明PropertyPlaceholderConfigurer是承担properties读取任务的类。
下面的类继承PropertyPlaceholderConfigurer,通过重写processProperties方法把properties暴露出去了。
Java代码
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
Xml代码
<!-- 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关键字this、super使用总结
- java设计模式之二 —— 装饰者模式整理
- Java关键字this、super使用总结
- eclipse 报错Version 1.6.0_45 of the JVM is not suitable for this product. Version:1.7 or greater is required
- java基础之修饰符如private,默认,protected,public
- java利用commons-email发送邮件并进行封装
- Spring注解@Component、@Repository、@Service、@Controller区别
- JAVA生成图片验证码
- 通过eclipse查看、阅读hadoop2.4源码
- java简述HASH
- Java关键字final、static使用总结
- Java关键字final、static使用总结
- 关于Java多线程小结
- Java ++i与i++
- java设计模式之一 —— 工厂模式整理
- spring_jar包的详解
- Java连接MySQL中文乱码处理
- Java集合类操作优化经验总结
- java的overload与override
- java的overload与override