开发者社区> 问答> 正文

Java中直接使用System.getProperty和通过安全控制器获取属性值有什么区别吗?

//下面的代码来自hashmap,事实上直接使用getProperty也不会受到权限限制 static { String altThreshold = java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction( "jdk.map.althashing.threshold"));

int threshold;
try {
    threshold = (null != altThreshold)
            ? Integer.parseInt(altThreshold)
            : ALTERNATIVE_HASHING_THRESHOLD_DEFAULT;

    // disable alternative hashing if -1
    if (threshold == -1) {
        threshold = Integer.MAX_VALUE;
    }

    if (threshold < 0) {
        throw new IllegalArgumentException("value must be positive integer.");
    }
} catch(IllegalArgumentException failed) {
    throw new Error("Illegal value for 'jdk.map.althashing.threshold'", failed);
}

ALTERNATIVE_HASHING_THRESHOLD = threshold;

}

展开
收起
问问小秘 2020-01-09 16:39:47 1207 0
1 条回答
写回答
取消 提交回答
  • 做的动作是一样的。GetPropertyAction 内部就是 调用了 System.getProperty。通过Access控制器来调用,顾名思义就是加了一层校验调用代码是否有权限访问对应属性。

    PS: 知道个意思就好了,不要太纠结,把时间放到更意义的学习上。现在这种写法很少见了。 public class GetPropertyAction implements PrivilegedAction { private String theProp; private String defaultVal;

    public GetPropertyAction(String var1) { this.theProp = var1; }

    public GetPropertyAction(String var1, String var2) { this.theProp = var1; this.defaultVal = var2; }

    public String run() { String var1 = System.getProperty(this.theProp); return var1 == null?this.defaultVal:var1; }

    2020-01-09 18:23:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载