Thread.currentThread().getContextClassLoader()与Test.class.getClassLoader()区别

Thread.currentThread().getContextClassLoader()与Test.class.getClassLoader()区别忘记以前有没有问过这个问题,总之我现在有看到几个地方有这个:Thread.currentThread().getContextClassLoader()我总是想不出在什么情况下会用这种方式获得一个ClassLoader,因为好像默认情况下,它返回的是和加载应用的ClassLoader是同一个,比如说在一个类Test中写ClassLoader cl = Thread.curren

大家好,又见面了,我是你们的朋友全栈君。


忘记以前有没有问过这个问题,总之我现在有看到几个地方有这个:

Thread.currentThread().getContextClassLoader()

我总是想不出在什么情况下会用这种方式获得一个ClassLoader,因为好像默认情况下,它返回的是和加载应用的ClassLoader是同一个,比如说在一个类Test中写

ClassLoader cl = Thread.currentThread().getContextClassLoader();

为何不直接用Test.class.getClassLoader()

先记录下:有时间整理下。

//  Class.forName(“”).getResourceAsStream(name)


获得当前上下文的类加载器是啥意思?有啥好处?
 
 
 
Java code
?
1
2
3
4
5
6
7
8
9
10
11
public 
class 
Test {
 
    
public 
static 
void 
main(String[] args) {
         
        
// 此时三个ClassLoader是同一个对象
        
System.out.println(Thread.currentThread().getContextClassLoader()); 
// 当前线程的类加载器
        
System.out.println(Test.
class
.getClassLoader()); 
// 当前类的类加载器
        
System.out.println(ClassLoader.getSystemClassLoader()); 
// 系统初始的类加载器
         
    
}
}

如果楼主了解过openfire应该对ClassLoader有比较深的理解。
打个简单的比方,你一个WEB程序,发布到Tomcat里面运行。
首先是执行Tomcat org.apache.catalina.startup.Bootstrap类,这时候的类加载器是ClassLoader.getSystemClassLoader()。
而我们后面的WEB程序,里面的jar、resources都是由Tomcat内部来加载的,所以你在代码中动态加载jar、资源文件的时候,首先应该是使用Thread.currentThread().getContextClassLoader()。如果你使用Test.class.getClassLoader(),可能会导致和当前线程所运行的类加载器不一致(因为Java天生的多线程)。
Test.class.getClassLoader()一般用在getResource,因为你想要获取某个资源文件的时候,这个资源文件的位置是相对固定的。

java的类加载机制(jvm规范)是委托模型,简单的说,如果一个类加载器想要加载一个类,首先它会委托给它的parent去加载,如果它的所有parent都没有成功的加载那么它才会自己亲自来,有点儿像儿子使唤老子的感觉。。jvm也拼爹啊,,,,,
在jvm中默认有三类loaer,bootstrap,ext,app,其中boot最大是爷爷,app最小是孙子,ext中间是爹。
它们有权限访问的classpath也不一样,boot是jdk或jre下面的lib目录,ext是jdk或jre的ext目录,而app是由用户指定的路径,比如用-cp参数指定的目录或jar。他们没有权力访问其他人的classpath,这样问题就来鸟,,,,可能有人会问狗司令大人闲得蛋疼啊,搞这么复杂,据说是为了安全考虑,避免用户的恶心意代码侵蚀jvm,,问题就是当bootstrap或ext想要加载用户指定classpath中的类就会失败,因为这俩货没有权限访问团app路径中的类的,,所以就搞了这么一个不伦不类的contextloader。。。。

资料:

ContextLoaderListener作用详解


(1) org.springframework.web.context.ContextLoaderListener 这个类被定义为监听器,并读取在参数contextConfigLocation中定义的xml 文件,如果不设置contextConfigLocation的初始参数则默认会读取    WEB-INF路径下的 application.xml文件,如果需要自定义了另外的xml 则可以在contextConfigLocation下定义,ContextLoaderListener会读取这些XML文件并产生 WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要可以得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring 容器管理的bean。
    (2)解析ContextLoadListener的源代码

// 实现了servlet的ServletContextListener接口
public class ContextLoaderListener

     implements ServletContextListener
{

private ContextLoader contextLoader;

// 这个是最重要的,利用contextLoader 来完成所有的工作
public void contextInitialized(ServletContextEvent event)
{

    this.contextLoader = createContextLoader();
    this.contextLoader.initWebApplicationContext(event.getServletContext());
}

protected ContextLoader createContextLoader()
{

    return new ContextLoader();
}

public ContextLoader getContextLoader()
{

    return this.contextLoader;
}

public void contextDestroyed(ServletContextEvent event)
{

    if (this.contextLoader != null)
      this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}

下面看一下ContextLoader .initWebApplicationContext方法

public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
    throws IllegalStateException, BeansException
{

    if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {

      throw new IllegalStateException(“Cannot initialize context because there is already a root application context present – check whether you have multiple ContextLoader* definitions in your web.xml!”);
    }

    long startTime = System.currentTimeMillis();
    if (this.logger.isInfoEnabled())
      this.logger.info(“Root WebApplicationContext: initialization started”);

    servletContext.log(“Loading Spring root WebApplicationContext”);
    try
    {

      ApplicationContext parent = loadParentContext(servletContext);
     
      // 这里得到WebApplicationContext对象下面具体看一下这个方法是如何实现的
      this.context = createWebApplicationContext(servletContext, parent);
     
    // 将这个对象设置到 servletContext的属性里
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

      if (this.logger.isInfoEnabled()) {

          this.logger.info(“Using context class [” + this.context.getClass().getName() + “] for root WebApplicationContext”);
      }

      if (this.logger.isDebugEnabled()) {

        this.logger.debug(“Published root WebApplicationContext [” + this.context + “] as ServletContext attribute with name [” + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + “]”);
      }

      if (this.logger.isInfoEnabled()) {

        long elapsedTime = System.currentTimeMillis() – startTime;
        this.logger.info(“Root WebApplicationContext: initialization completed in ” + elapsedTime + ” ms”);
      }

      return this.context;
    }

protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent)
    throws BeansException
{

    // 返回在web.xml 中参数contextClass自定义类对应的对象这个类,这个类实现了XmlWebApplicationContext
    // XmlWebApplicationContext继承了AbstractRefreshableWebApplicationContext类中定义的方法protected String[] getConfigLocations() ,这个方法默认可以加载contextConfigLocation中定义的xml 文件,如果你重写了这个方法还可以在任意地方加载你想要的xml文件。

    Class contextClass = determineContextClass(servletContext);
    if (!(ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass))) {

      throw new ApplicationContextException(“Custom context class [” + contextClass.getName() + “] is not of type   ConfigurableWebApplicationContext”);
    }
   
  
// 得到 WebApplicationContext
    ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);

    wac.setParent(parent);
    wac.setServletContext(servletContext);
    String configLocation = servletContext.getInitParameter(“contextConfigLocation”);
    if (configLocation != null) {

      wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation, “,; \t\n”));
    }

    wac.refresh();
    return wac;
}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134558.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 开启Redis

    开启Redis

    2021年6月13日
    83
  • 如何配置pytorch_pytorch如何下载

    如何配置pytorch_pytorch如何下载1.好像不支持python3.8。直接从setting里面安装时不行的,按其它教程(https://blog.csdn.net/lyz21/article/details/104295042)从官网https://pytorch.org/get-started/locally/,拷贝链接用pip下载,一直报找不到版本。后来发现,python3.8的原因,改成python3.7可以了,但会一直连接超时。2.发现要下载的其实是这两个文件:点开下面的两个链接,用下载软件下载了,我下到了e盘,直接pip

    2022年10月31日
    0
  • 一个反射型XSS例子的解析

    一个反射型XSS例子的解析我们在访问一个网页的时候,在URL后面加上参数,服务器根据请求的参数值构造不同的HTML返回。如http://localhost:8080/prjWebSec/xss/reflectedXSS.jsp?param=value…上例中的value可能出现在返回的HTML(可能是JS,HTML某元素的内容或者属性)中,如果将value改成可以在浏览器中被解释执行的东西,就形成了反射型X

    2022年5月5日
    90
  • 常数变易法的原理解释_微分方程常数变易法

    常数变易法的原理解释_微分方程常数变易法高数看到常数变易法,不懂为什么要把C变成u,看了篇文章,感觉懂了转载自http://www.cnblogs.com/lookof/archive/2009/01/06/1370065.html注:本方法是对崔士襄教授写的《“常数变易法”来历的探讨》论文的解释。思路并非本人原创。特此注明。背景详见本人前一篇博文。      我们来看下面的式子:

    2022年10月24日
    0
  • 调用ShellExecute所须要头文件

    调用ShellExecute所须要头文件

    2021年12月7日
    49

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号