欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

tomcat 7 中的類加載器學習

系統 1921 0

tomcat 7自帶很多junit測試用例,可以幫助我們窺探源碼的秘密。以下使用來測試類加載器的一個測試用例。類加載器也是對象,他們用來將類從類從。class文件加載到虛擬機,這些已經講了很多,深入jvm中說的很詳細,什么雙親委派模型,在書中還以tomcat為例講解。

    

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.loader;

    

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

    

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

    


import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;

    

public class TestWebappClassLoader extends TomcatBaseTest {

    

@Test
public void testGetURLs() throws Exception {

    

File f = new File("test/webresources/war-url-connection.war");

    

String[] expected = new String[2];
String warUrl = f.toURI().toURL().toExternalForm();
expected[0] = "jar:" + warUrl + "!/WEB-INF/classes/";
expected[1] = "jar:" + warUrl + "!/WEB-INF/lib/test.jar";

    

Tomcat tomcat = getTomcatInstance();
// Must have a real docBase - just use temp
StandardContext ctx =
(StandardContext)tomcat.addContext("", f.getAbsolutePath());

    

tomcat.start();

    

//獲得當前的類加載器
ClassLoader cl = ctx.getLoader().getClassLoader();

    

Assert.assertTrue(cl instanceof URLClassLoader);
//獲得類加載器中對應的路徑

    

try (URLClassLoader ucl = (URLClassLoader) cl) {
URL[] urls = ucl.getURLs();
Assert.assertEquals(expected.length, urls.length);
String[] actual = new String[urls.length];
for (int i = 0; i < urls.length; i++) {
actual[i] = urls[i].toExternalForm();
System.out.println(actual[i]);
}
Assert.assertArrayEquals(expected, actual);
//獲得類加載器的各級父類加載器
while(cl!=null)
{
System.out.println(cl);
cl=cl.getParent();

}


}
}
@Test
public void testBug53454() throws Exception {
Tomcat tomcat = getTomcatInstance();

    

// Must have a real docBase - just use temp
StandardContext ctx = (StandardContext)
tomcat.addContext("", System.getProperty("java.io.tmpdir"));
System.out.println("臨時目錄"+System.getProperty("java.io.tmdir"));

    

// Map the test Servlet
MyServlet largeBodyServlet = new MyServlet();
Tomcat.addServlet(ctx, "MyServlet", largeBodyServlet);
ctx.addServletMapping("/", "MyServlet");

    

tomcat.start();

    

Map<String,List<String>> resHeaders= new HashMap<>();
int rc = headUrl("http://localhost:" + getPort() + "/", new ByteChunk(),
resHeaders);

    

Assert.assertEquals(HttpServletResponse.SC_OK, rc);
ClassLoader cl=ctx.getClass().getClassLoader();
while(cl!=null)
{
System.out.println(cl);
cl=cl.getParent();
}


}
private static class MyServlet extends HttpServlet {

    

private static final long serialVersionUID = 1L;

    

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {


}

}
}

    

?

      
        ?
      
    

輸出結果:

      
        臨時目錄null


      
      
        

sun.misc.Launcher$AppClassLoader@4bb8d481

sun.misc.Launcher$ExtClassLoader@538787fd


      
      
        

jar:file:
      
      /F:/javal/Tomcat-Research-trunk/test/webresources/war-url-connection.war!/WEB-INF/classes/
      
        

jar:file:
      
      /F:/javal/Tomcat-Research-trunk/test/webresources/war-url-connection.war!/WEB-INF/lib/
      
        test.jar

WebappClassLoader

  context: 

  delegate: 
      
      
        false
      
      

---------->
      
         Parent Classloader:

sun.misc.Launcher$AppClassLoader@4bb8d481



sun.misc.Launcher$AppClassLoader@4bb8d481

sun.misc.Launcher$ExtClassLoader@538787fd

一月 
      
      26, 2015 6:52:56
      
         下午 org.apache.coyote.AbstractProtocol stop

INFO: Stopping ProtocolHandler [
      
      "http-nio-127.0.0.1-auto-2-64075"
      
        ]

一月 
      
      26, 2015 6:52:56
      
         下午 org.apache.coyote.AbstractProtocol destroy

INFO: Destroying ProtocolHandler [
      
      "http-nio-127.0.0.1-auto-2-64075"]
    

?

如果使用臨時目錄和默認目錄,應用的類加載器是AppClassLoader,而其的目錄可以每個應用的類加載器為WebappClassLoader

?

tomcat 7 中的類加載器學習


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产欧美日韩在线观看 | 粉嫩粉嫩一区二区三区在线播放 | 麻豆短视频app网站 天天澡天天碰天天狠伊人五月 | 四虎影视免费观看免费观看 | 天天做天天干 | 亚州 色毛片免费观看 | 久热免费 | 免费福利视频在线观看 | 黄网站入口| 久久综合色婷婷 | 欧美精品网站 | 久久久久久99 | 在线视频h| 九九99国产精品视频 | 丁香午夜 | 91欧美激情一区二区三区成人 | 日韩大尺度电影在线观看 | 日韩大尺度电影在线观看 | 日日骚av| 一级a级国产不卡毛片 | 天天看天天爽天天摸天天添 | 天天色综合影视 | 欧美二区三区 | 久久夜色精品国产亚洲噜噜 | 日韩大片在线 | 碰碰碰精品视频在线观看 | 成人午夜电影网 | 国产精品自拍99 | 爱高潮www亚洲精品 亚洲精品免费网站 | 免费在线成人 | 色阁阁日韩欧美在线 | 2019中文字幕在线视频 | 久久久久国产一区二区三区四区 | 爱爱视频在线观看 | 欧美日韩精品久久久免费观看 | 日日干夜夜拍 | 在线精品亚洲欧美日韩国产 | 欧美成视频无需播放器 | 黄色成年在线观看 | 九九九九九九精品任你躁 | 日韩在线黄色 |