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());
        }
  • 相关阅读:
    C# 实现将 PDF 转文本的功能
    推荐大家使用的CSS书写规范、顺序
    Android 应用开发者必看的 9 个 Tips
    网站统计中的数据收集原理及实现
    JSON劫持漏洞攻防原理及演练
    巧妙使用 CSS3 的褪色和动画效果制作消息提醒框
    20条常见的编码陷阱
    11 个最佳 jQuery 滚动条插件
    JavaScript客户端MVC 框架综述
    20个初学者实用的CSS技巧
  • 原文地址:https://www.cnblogs.com/zhaixingzhu/p/12569111.html
Copyright © 2011-2022 走看看