相关文章推荐
彷徨的斑马  ·  Platform.OnRequestPerm ...·  7 月前    · 
无邪的单杠  ·  DC-6 ...·  9 月前    · 
逼格高的小笼包  ·  jquery ...·  10 月前    · 
讲道义的鸡蛋  ·  System.QueryException: ...·  1 年前    · 
打篮球的荒野  ·  inputstream转base64-掘金·  1 年前    · 

Thread线程API中, getContextClassLoader ()方法的描述为:

* Returns the context ClassLoader for this Thread. The context
* ClassLoader is provided by the creator of the thread for use
* by code running in this thread when loading classes and resources.
* If not {@linkplain #setContextClassLoader set}, the default is the
* ClassLoader context of the parent Thread. The context ClassLoader of the
* primordial thread is typically set to the class loader used to load the
* application.
* <p>If a security manager is present, and the invoker's class loader is not
* {@code null} and is not the same as or an ancestor of the context class
* loader, then this method invokes the security manager's {@link
* SecurityManager#checkPermission(java.security.Permission) checkPermission}
* method with a {@link RuntimePermission RuntimePermission}{@code
* ("getClassLoader")} permission to verify that retrieval of the context
* class loader is permitted.

翻译成中文大概意思为:

返回该线程的ClassLoader上下文。线程创建者提供ClassLoader上下文,以便运行在该线程的代码在加载类和资源时使用。如果没有,则默认返回父线程的ClassLoader上下文。原始线程的上下文 ClassLoader 通常设定为用于加载应用程序的类加载器。

首先,如果有安全管理器,并且调用者的类加载器不是 null,也不同于其上下文类加载器正在被请求的线程上下文类加载器的祖先,则通过 RuntimePermission("getClassLoader") 权限调用该安全管理器的 checkPermission 方法,查看是否可以获取上下文 ClassLoader。

下面的例子说明子线程的ClassLoader与父线程的一致:

public class Test {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getId()+"-outer:"+Thread.currentThread().getContextClassLoader());
        new Thread(){
            public void run(){
                System.out.println(Thread.currentThread().getId()+"-inner:"+Thread.currentThread().getContextClassLoader());
        }.start();
        new Thread(){
            public void run(){
                System.out.println(Thread.currentThread().getId()+"-inner:"+Thread.currentThread().getContextClassLoader());
        }.start();
1-outer:sun.misc.Launcher$AppClassLoader@18b4aac2
12-inner:sun.misc.Launcher$AppClassLoader@18b4aac2
13-inner:sun.misc.Launcher$AppClassLoader@18b4aac2

以上结果表示返回的ClassLoader是一样的。

在阅读org.apache.commons.beanutils.BeanUtils#copyProperties源码时,发现里面用到了类org.apache.commons.beanutils.ContextClassLoaderLocal,该类中的map让ClassLoader作为key,通过以上的描述,在一个JVM程序中,得到的ClassLoader是一个,所以可以作为一个全局的key存在。

Thread线程API中,getContextClassLoader()方法的描述为:* Returns the context ClassLoader for this Thread. The context* ClassLoader is provided by the creator of the thread for use* by code running in this t... //此处可以使用hbase的配置文件,也可以通过代码来实例化hbase连接 * Configuration HBASE_CONFIG = new Configuration(); * HBASE_CONFIG.set("hbase.zookeeper.quorum", "10.229.171.45"); * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); * HBASE_CONFIG.set("hbase.zookeeper.quorum","10.233.92.85,10.233.92.86,10.233.92.88"); * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); * HBASE_CONFIG.set("zookeeper.znode.parent", "/hbase-unsecure"); conf = HBaseConfiguration.create(HBASE_CONFIG); conf = HBaseConfiguration.create(); if("kerberos".equals(conf.get("hbase.security.authentication"))){ try { //设置hadoop.security.authentication为kerberos conf.set("hadoop.security.authentication", "kerberos"); //获取kerberos配置文件路径(krb为kerberos配置文件) String krbStr=Thread.currentThread().getContextClassLoader().getResource("krb").getFile(); //获取用户票据hezhong路径(hezhong为给合众分配的用户配置文件) String keyStr=Thread.currentThread().getContextClassLoader().getResource("jdzy").getFile(); //初始化配置文件 System.setProperty("java.security.krb5.conf", krbStr); //使用用户hezhong登录 UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab("jdzy/f04345e6-70c1-448a-9bbb-4ac6b4c0109b@POLICE.COM", keyStr); } catch (IOException e) { e.printStackTrace(); tablePool = new HTablePool(conf, poolsize); logger.debug("create hbase connection success"); // System.out.println("create hbase connection success"); 今天在学习尚硅谷小项目的时候看到老师写的代码中用到了 JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); 这样一段代码,目的是为了加载jdbc.properties这个配置文件,但是我发现我自己使用的时候是空指针异常。非常莫名其妙。 于是乎,在网上找了一堆资料,发现很多人都在文章中推荐使用 Thread.currentThread().getContextClassLoader().
今天碰到一个问题,我做了一个动态编译的程序,但在javaSE环境直接运行(即javaApplication状态)可以运行,但在tomcat服务器上通过请求来处理就不行,提示找不到我动态生成的类:关键语句如下 (报错时我写的是:return ClassLoader.getSystemClassLoader()) 最后我发现写成如图这样,调用Thread.currentThread().getContextClassLoader().loadClass(name);,就不会报错了。 查询资料后解释如下:
⼤数据⾯试题 Big Data ⾯试题总结 JAVA相关 1-1)List 与set 的区别? ⽼掉⽛的问题了,还在这⾥⽼⽣常谈:List特点:元素有放⼊顺序,元素可重复 ,Set特点:元素⽆放⼊顺序,元素不可重复。 1-2)数据库的三⼤范式? 原⼦性、⼀致性、唯⼀性 1-3)java 的io类的图解 1-4)对象与引⽤对象的区别 对象就是好没有初始化的对象,引⽤对象即使对这个对象进⾏了初始化,这个初始化可以使⾃⼰的直接new的也可以是直接其他的赋值的, 那么背new或者背其他赋值的我们叫做是引⽤对象,最⼤的区别于 1-5)谈谈你对反射机制的理解及其⽤途? 反射有三种获取的⽅式,分别是:forName / getClass / 直接使⽤class⽅式 使⽤反射可以获取类的实例 1-6)列出⾄少五种设计模式 设计⽅式有⼯⼚法,懒加载,观察者模式,静态⼯⼚,迭代器模式,外观模式、、、、 1-7)RPC 原理? Rpc分为同步调⽤和⼀部调⽤,异步与同步的区别在于是否等待服务器端的返回值。Rpc的组件 有RpcServer,RpcClick,RpcProxy,RpcConnection,RpcChannel,RpcProtocol,RpcInvoker等组件, 1-8)ArrayList、Vector、LinkedList 的区别及其优缺点?HashMap、HashTable 的区别及优缺点? ArrayList 和 Vector 是采⽤数组⽅式存储数据的,是根据索引来访问元素的,都可以 根据需要⾃动扩展内部数据长度,以便增加和插⼊元素,都允许直接序号索引元素,但 是插⼊数据要涉及到数组元素移动等内存操作,所以索引数据快插⼊数据慢,他们最⼤ 的区别就是 synchronized 同步的使⽤。 LinkedList 使⽤双向链表实现存储,按序号索引数据需要进⾏向前或向后遍历,但 是插⼊数据时只需要记录本项的前后项即可,所以插⼊数度较快! 如果只是查找特定位置的元素或只在集合的末端增加、移除元素,那么使⽤ Vector 或 ArrayList 都可以。如果是对其它指定位置的插⼊、删除操作,最好选择 LinkedList HashMap、HashTable 的区别及其优缺点: HashTable 中的⽅法是同步的 HashMap 的⽅法在缺省情况下是⾮同步的 因此在多线程环境下需要做额外的同步机制。 HashTable 不允许有 null 值 key 和 value 都不允许,⽽ HashMap 允许有 null 值 key和 value 都允许 因此 HashMap 使 ⽤ containKey()来判断是否存在某个键。 HashTable 使⽤ Enumeration ,⽽ HashMap 使⽤ iterator。 Hashtable 是 Dictionary 的⼦类,HashMap 是 Map 接⼝的⼀个实现类。 1-9)使⽤ StringBuffer ⽽不是 String 当需要对字符串进⾏操作时,使⽤ StringBuffer ⽽不是 String,String 是 read-only 的,如果对它进⾏修改,会产⽣临时对象, ⽽ StringBuffer 是可修改的,不会产⽣临时对象。 1-10)集合的扩充 ArrayList list = new ArrayList(90000); list扩充多少次?? public ArrayList() { this(10); } 默认的扩充是10由此计算 1-11)java的拆包与封包的问题 System.out.println("5" + 2); 52 1-12)Java中Class.forName和ClassLoader.loadClass的区别 Class.forName("xx.xx")等同于Class.forName("xx.xx",true,CALLClass.class.getClassLoader()),第⼆个参数(bool)表⽰装载类的时候是否 初始化该类,即调⽤类的静态块的语句及初始化静态成员变量。 ClassLoader loader = Thread.currentThread.getContextClassLoader(); //也可以⽤(ClassLoader.getSystemClassLoader()) Class cls = loader.loadClass("xx.xx"); //这句话没有执⾏初始化 forName可以控制是否初始化类,⽽loadClass加载时是没有初始化的。 1-13)hashMap与hashTable的区别 HashMap Hashtable ⽗类 AbstractMap Dictiionary 是否同步 否 是 k,v可否nu
Java路径中的空格问题   1、 URLTest.class.getResource(/).getPath();   URLTest.class.getResource(/).getFile();   URLTest.class.getClassLoader().getResource().getPath();   Thread.currentThread().getContextClassLoader().getResource().getPath();等多种相似方式获得的路径,不能被FileReader()和FileWriter()直接应用,原因是URL对空格,特
0.问题描述   通过下面的代码获得配置文件的路径,并传给FileReader,发现找不到系统路径 String path=Thread.currentThread().getContextClassLoader().getResource("jdbc.properties"); try( FileReader fileReader = new FileReader(path)) { //.......省略  通过打印path,发现 /D:/I