方法一:
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>
- ...
- 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的值了。