java properties 的使用(重要的信息存放在里面)

来源:互联网 发布:关口知宏父母 编辑:程序博客网 时间:2024/04/29 08:42
properties 的使用(重要的信息存放在里面)

1.工具类 : 读取配置文件的工具类
  

 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;

 /**
  * 配置文件加载类
  * @author 
  */
 public class PropertiesHandler extends PropertyPlaceholderConfigurer {

  private static Map<String, Object> ctxPropertiesMap;
  /**
   * 读取llpay.properties
   */
  @Override
  protected void processProperties(
    ConfigurableListableBeanFactory beanFactoryToProcess,
    Properties props) throws BeansException {
   super.processProperties(beanFactoryToProcess, props);
   ctxPropertiesMap = new HashMap<String, Object>();
   for (Object key : props.keySet()) {
    String keyStr = key.toString();
    String value = props.getProperty(keyStr);
    ctxPropertiesMap.put(keyStr, value);
   }
  }
  
  public static Object getConfigValue(String name) {
   return ctxPropertiesMap.get(name);
  }
 }

 
2.写配置文件
 
 message.properties
 内容(注意不要有空格)
  key1:value1
  key1:value1
  

3.在spring中去配置

<bean id="configSources" class="com.fs.core.fund.controller.PropertiesHandler">
 <property name="ignoreResourceNotFound" value="true" />
 <property name="locations">
  <list>
   <value>classpath:META-INF/你的项目名称/config/llpay.properties</value>
   <value>classpath:META-INF/你的项目名称/config/message.properties</value>
   
  </list>
 </property>
</bean>

4.调用配置文件
 String value1 = PropertiesHandler.getConfigValue("key1").toString();
 
 
0 0
原创粉丝点击