zoukankan      html  css  js  c++  java
  • Java中通过System.setProperty()设置系统属性。可以通过System.getProperty()获取

    /*
     * 设置指定键对值的系统属性
     * setProperty (String prop, String value);
     * 
     * 参数:
     * prop - 系统属性的名称。
     * value - 系统属性的值。  
     * 
     * 返回:
     * 系统属性以前的值,如果没有以前的值,则返回 null。
     * 
     * 抛出:  
     * SecurityException - 如果安全管理器存在并且其 checkPermission 方法不允许设置指定属性。
     * NullPointerException - 如果 key 或 value 为 null。
     * IllegalArgumentException - 如果 key 为空。
     * 注:这里的system,系统指的是 JRE (runtime)system,不是指 OS。
     * 
    */
    
    //实例
    System.setProperty("Property1", "abc");
    System.setProperty("Property2","def");
    
    //这样就把第一个参数设置成为系统的全局变量!可以在项目的任何一个地方 通过System.getProperty("变量");来获得,
    
    //System.setProperty 相当于一个静态变量  ,存在内存里面!
    public class SystemTest {
        
        static {
            setValue();
        }
    
        public static void setValue() {
            System.setProperty("name", "张三");
            System.setProperty("age", "28");
        }
        
        public static void main(String[] args) {
            System.out.println(System.getProperty("name"));
            System.out.println(System.getProperty("age"));
        }
    }

     输出:

    下面是在Tomcat源码中Bootstrap的代码块

     static {
            // Will always be non-null  将始终为非空
            String userDir = System.getProperty("user.dir");
            System.out.println("userDir 当前系统的用户目录====================>>>>>>>  " + userDir);
    
            // Home first 获取已经存在系统中的地址信息 catalina.home
            String home = System.getProperty(Globals.CATALINA_HOME_PROP);
    
            System.out.println("home first ======启动输出为null=======>>>>>>>>>  " + home);
            File homeFile = null;
    
            if (home != null) {
                File f = new File(home);
                try {
                    homeFile = f.getCanonicalFile();
                } catch (IOException ioe) {
                    homeFile = f.getAbsoluteFile();
                }
            }
    
            if (homeFile == null) {
                // First fall-back. See if current directory is a bin directory
                // in a normal Tomcat install
                File bootstrapJar = new File(userDir, "bootstrap.jar");
    
                if (bootstrapJar.exists()) {
                    File f = new File(userDir, "..");
                    try {
                        homeFile = f.getCanonicalFile();
                    } catch (IOException ioe) {
                        homeFile = f.getAbsoluteFile();
                    }
                }
            }
            if (homeFile == null) {
                // Second fall-back. Use current directory
                File f = new File(userDir);
                try {
                    homeFile = f.getCanonicalFile();
                } catch (IOException ioe) {
                    homeFile = f.getAbsoluteFile();
                }
            }
    
            catalinaHomeFile = homeFile;
            // 设置catalina.home键值中的系统属性,这里设置的是Tomcat在系统中的地址信息
            System.setProperty(
                    Globals.CATALINA_HOME_PROP, catalinaHomeFile.getPath());
    
            // Then base
            String base = System.getProperty(Globals.CATALINA_BASE_PROP);
            if (base == null) {
                catalinaBaseFile = catalinaHomeFile;
            } else {
                File baseFile = new File(base);
                try {
                    baseFile = baseFile.getCanonicalFile();
                } catch (IOException ioe) {
                    baseFile = baseFile.getAbsoluteFile();
                }
                catalinaBaseFile = baseFile;
            }
            System.setProperty(
                    Globals.CATALINA_BASE_PROP, catalinaBaseFile.getPath());
        }
  • 相关阅读:
    shell进行mysql统计
    java I/O总结
    Hbase源码分析:Hbase UI中Requests Per Second的具体含义
    ASP.NET Session State Overview
    What is an ISAPI Extension?
    innerxml and outerxml
    postman
    FileZilla文件下载的目录
    how to use webpart container in kentico
    Consider using EXISTS instead of IN
  • 原文地址:https://www.cnblogs.com/zhaixingzhu/p/12569111.html
Copyright © 2011-2022 走看看