zoukankan      html  css  js  c++  java
  • Properties类和如何操作属性

    Properties类
    继承关系
    java.lang.Object
      java.util.Dictionary<K,V>
          java.util.Hashtable<Object,Object>
              java.util.Properties
    所有已实现的接口:
    Serializable, Cloneable, Map<Object,Object>
    直接已知子类:
    Provider

    public class Propertiesextends Hashtable<Object,Object>
    Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
    一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。
    此类是线程安全的:多个线程可以共享单个 Properties 对象而无需进行外部同步

    System.getenv() 方法是获取指定的环境变量的值。
    System.getenv(String str) 接收参数为任意字符串,当存在指定环境变量时即返回环境变量的值,否则返回null。
    System.getProperty() 是获取系统的相关属性,包括文件编码、操作系统名称、区域、用户名等,此属性一般由jvm自动获取,不能设置。
    System.getProperty(String str) 接收参数为任意字符串,当存在指定属性时即返回属性的值,否则返回null。
    public class Environment extends PropertiesLoaderSupport implements FactoryBean<Properties>{
       public Properties getObject() throws Exception {}
    }
       ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(getContextFilename(args));
            ctx.registerShutdownHook();
            //使用自定义工厂方法,返回的是一个对象。前面的博客有详细说明
            Properties prtis =  (Properties) ctx.getBean("environmentFactoryBean");
            System.out.println(prtis.getProperty("jdbcUrl"));
            Enumeration en = prtis.propertyNames();
          /*  while(en.hasMoreElements()){
                String key = (String) en.nextElement();
                String value = prtis.getProperty(key);
                System.out.println(key + " = " + value);
            }*/
            Map env = System.getenv();
            System.out.println(env.toString());
    java.util  接口 Enumeration<E>
    所有已知子接口:
    NamingEnumeration<T>
    所有已知实现类:
    StringTokenizer
    public interface Enumeration<E>
    实现 Enumeration 接口的对象,它生成一系列元素,一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素。
    例如,要输出 Vector<E> v 的所有元素,可使用以下方法:
       for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
           System.out.println(e.nextElement());
    PropertiesLoaderSupport类有一个重要的protected void loadProperties(Properties props)方法,
    查看它的注释,可以知道该方法的作用是将PropertyPlaceholderConfigurer 中locations属性所定义的属性文件的内容读取到props入参对象中。

  • 相关阅读:
    字符串转XML保存后保证XML的格式
    SmartFoxServer学习(3)--第一个Extension
    SmartFoxServer学习(2)--Extension调试
    SmartFoxServer学习(1)--初步了解
    jmeter 开发自己的java请求 二次开发
    hive 面试题
    美丽说-爬虫记录
    python中re.sub的使用 --解读一段代码
    python 抓取美丽说店铺的宝贝图片及详细信息的实现(爬虫)
    hadoop编码问题,mapreduce中Tex与string的转化 乱码问题
  • 原文地址:https://www.cnblogs.com/mutong1228/p/9062187.html
Copyright © 2011-2022 走看看