主頁 > 後端開發 > Spring Boot 中的 Tomcat 是如何啟動的?

Spring Boot 中的 Tomcat 是如何啟動的?

2020-10-16 16:40:07 後端開發

作者:木木匠
https://my.oschina.net/luozhou/blog/3088908

我們知道 Spring Boot 給我們帶來了一個全新的開發體驗,讓我們可以直接把 Web 程式打包成 jar 包直接啟動,這得益于 Spring Boot 內置了容器,可以直接啟動,

本文將以 Tomcat 為例,來看看 Spring Boot 是如何啟動 Tomcat 的,同時也將展開學習下 Tomcat 的原始碼,了解 Tomcat 的設計,

從 Main 方法說起

用過 Spring Boot 的人都知道,首先要寫一個 main 方法來啟動:

@SpringBootApplication
public class TomcatdebugApplication {

    public static void main(String\[\] args) {
 SpringApplication.run(TomcatdebugApplication.class, args);
 }

}

我們直接點擊run方法的原始碼,跟蹤下來,發現最終的run方法是呼叫
ConfigurableApplicationContext方法,原始碼如下:

public ConfigurableApplicationContext run(String... args) {
	StopWatch stopWatch = new StopWatch();
	stopWatch.start();
	ConfigurableApplicationContext context = null;
	Collection<springbootexceptionreporter> exceptionReporters = new ArrayList&lt;&gt;();
	//設定系統屬性『java.awt.headless』,為true則啟用headless模式支持
	configureHeadlessProperty();
	//通過*SpringFactoriesLoader*檢索*META-INF/spring.factories*,
   //找到宣告的所有SpringApplicationRunListener的實作類并將其實體化,
   //之后逐個呼叫其started()方法,廣播SpringBoot要開始執行了
	SpringApplicationRunListeners listeners = getRunListeners(args);
	//發布應用開始啟動事件
	listeners.starting();
	try {
	//初始化引數
		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
		//創建并配置當前SpringBoot應用將要使用的Environment(包括配置要使用的PropertySource以及Profile),
	//并遍歷呼叫所有的SpringApplicationRunListener的environmentPrepared()方法,廣播Environment準備完畢,
		ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
		configureIgnoreBeanInfo(environment);
		//列印banner
		Banner printedBanner = printBanner(environment);
		//創建應用背景關系
		context = createApplicationContext();
		//通過*SpringFactoriesLoader*檢索*META-INF/spring.factories*,獲取并實體化例外分析器
		exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
				new Class[] { ConfigurableApplicationContext.class }, context);
		//為ApplicationContext加載environment,之后逐個執行ApplicationContextInitializer的initialize()方法來進一步封裝ApplicationContext,
	//并呼叫所有的SpringApplicationRunListener的contextPrepared()方法,【EventPublishingRunListener只提供了一個空的contextPrepared()方法】,
	//之后初始化IoC容器,并呼叫SpringApplicationRunListener的contextLoaded()方法,廣播ApplicationContext的IoC加載完成,
	//這里就包括通過**@EnableAutoConfiguration**匯入的各種自動配置類,
		prepareContext(context, environment, listeners, applicationArguments, printedBanner);
		//重繪背景關系
		refreshContext(context);
		//再一次重繪背景關系,其實是空方法,可能是為了后續擴展,
		afterRefresh(context, applicationArguments);
		stopWatch.stop();
		if (this.logStartupInfo) {
			new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
		}
		//發布應用已經啟動的事件
		listeners.started(context);
		//遍歷所有注冊的ApplicationRunner和CommandLineRunner,并執行其run()方法,
	//我們可以實作自己的ApplicationRunner或者CommandLineRunner,來對SpringBoot的啟動程序進行擴展,
		callRunners(context, applicationArguments);
	}
	catch (Throwable ex) {
		handleRunFailure(context, ex, exceptionReporters, listeners);
		throw new IllegalStateException(ex);
	}

	try {
	//應用已經啟動完成的監聽事件
		listeners.running(context);
	}
	catch (Throwable ex) {
		handleRunFailure(context, ex, exceptionReporters, null);
		throw new IllegalStateException(ex);
	}
	return context;
}

其實這個方法我們可以簡單的總結下步驟為 > 1. 配置屬性 > 2. 獲取監聽器,發布應用開始啟動事件 > 3. 初始化輸入引數 > 4. 配置環境,輸出 banner > 5. 創建背景關系 > 6. 預處理背景關系 > 7. 重繪背景關系 > 8. 再重繪背景關系 > 9. 發布應用已經啟動事件 > 10. 發布應用啟動完成事件

其實上面這段代碼,如果只要分析 Tomcat 內容的話,只需要關注兩個內容即可,背景關系是如何創建的,背景關系是如何重繪的,分別對應的方法就是createApplicationContext() 和refreshContext(context),接下來我們來看看這兩個方法做了什么,

protected ConfigurableApplicationContext createApplicationContext() {
	Class<!--?--> contextClass = this.applicationContextClass;
	if (contextClass == null) {
		try {
			switch (this.webApplicationType) {
			case SERVLET:
				contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
				break;
			case REACTIVE:
				contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
				break;
			default:
				contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
			}
		}
		catch (ClassNotFoundException ex) {
			throw new IllegalStateException(
					"Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass",
					ex);
		}
	}
	return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}

這里就是根據我們的webApplicationType來判斷創建哪種型別的 Servlet,代碼中分別對應著 Web 型別(SERVLET),回應式 Web 型別(REACTIVE),非 Web 型別(default),我們建立的是Web型別,所以肯定實體化DEFAULT_SERVLET_WEB_CONTEXT_CLASS

指定的類,也就是
AnnotationConfigServletWebServerApplicationContext類,我們來用圖來說明下這個類的關系,

通過這個類圖我們可以知道,這個類繼承的是ServletWebServerApplicationContext,這就是我們真正的主角,而這個類最終是繼承了AbstractApplicationContext,了解完創建背景關系的情況后,我們再來看看重繪背景關系,相關代碼如下:

//類:SpringApplication.java

private void refreshContext(ConfigurableApplicationContext context) {
//直接呼叫重繪方法
	refresh(context);
	if (this.registerShutdownHook) {
		try {
			context.registerShutdownHook();
		}
		catch (AccessControlException ex) {
			// Not allowed in some environments.
		}
	}
}
//類:SpringApplication.java

protected void refresh(ApplicationContext applicationContext) {
	Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
	((AbstractApplicationContext) applicationContext).refresh();
}

這里還是直接傳遞呼叫本類的refresh(context)方法,最后是強轉成父類AbstractApplicationContext呼叫其refresh()方法,該代碼如下:

// 類:AbstractApplicationContext
public void refresh() throws BeansException, IllegalStateException {
	synchronized (this.startupShutdownMonitor) {
		// Prepare this context for refreshing.
		prepareRefresh();

		// Tell the subclass to refresh the internal bean factory.
		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

		// Prepare the bean factory for use in this context.
		prepareBeanFactory(beanFactory);

		try {
			// Allows post-processing of the bean factory in context subclasses.
			postProcessBeanFactory(beanFactory);

			// Invoke factory processors registered as beans in the context.
			invokeBeanFactoryPostProcessors(beanFactory);

			// Register bean processors that intercept bean creation.
			registerBeanPostProcessors(beanFactory);

			// Initialize message source for this context.
			initMessageSource();

			// Initialize event multicaster for this context.
			initApplicationEventMulticaster();

			// Initialize other special beans in specific context subclasses.這里的意思就是呼叫各個子類的onRefresh()
			onRefresh();

			// Check for listener beans and register them.
			registerListeners();

			// Instantiate all remaining (non-lazy-init) singletons.
			finishBeanFactoryInitialization(beanFactory);

			// Last step: publish corresponding event.
			finishRefresh();
		}

		catch (BeansException ex) {
			if (logger.isWarnEnabled()) {
				logger.warn("Exception encountered during context initialization - " +
						"cancelling refresh attempt: " + ex);
			}

			// Destroy already created singletons to avoid dangling resources.
			destroyBeans();

			// Reset 'active' flag.
			cancelRefresh(ex);

			// Propagate exception to caller.
			throw ex;
		}

		finally {
			// Reset common introspection caches in Spring's core, since we
			// might not ever need metadata for singleton beans anymore...
			resetCommonCaches();
		}
	}
}

這里我們看到onRefresh()方法是呼叫其子類的實作,根據我們上文的分析,我們這里的子類是ServletWebServerApplicationContext

//類:ServletWebServerApplicationContext
protected void onRefresh() {
	super.onRefresh();
	try {
		createWebServer();
	}
	catch (Throwable ex) {
		throw new ApplicationContextException("Unable to start web server", ex);
	}
}

private void createWebServer() {
	WebServer webServer = this.webServer;
	ServletContext servletContext = getServletContext();
	if (webServer == null &amp;&amp; servletContext == null) {
		ServletWebServerFactory factory = getWebServerFactory();
		this.webServer = factory.getWebServer(getSelfInitializer());
	}
	else if (servletContext != null) {
		try {
			getSelfInitializer().onStartup(servletContext);
		}
		catch (ServletException ex) {
			throw new ApplicationContextException("Cannot initialize servlet context", ex);
		}
	}
	initPropertySources();
}

到這里,其實廬山真面目已經出來了,createWebServer()就是啟動 Web 服務,但是還沒有真正啟動 Tomcat,既然webServer是通過ServletWebServerFactory來獲取的,我們就來看看這個工廠的真面目,

走進 Tomcat 內部

根據上圖我們發現,工廠類是一個介面,各個具體服務的實作是由各個子類來實作的,所以我們就去看看TomcatServletWebServerFactory.getWebServer()的實作,

@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
	Tomcat tomcat = new Tomcat();
	File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
	tomcat.setBaseDir(baseDir.getAbsolutePath());
	Connector connector = new Connector(this.protocol);
	tomcat.getService().addConnector(connector);
	customizeConnector(connector);
	tomcat.setConnector(connector);
	tomcat.getHost().setAutoDeploy(false);
	configureEngine(tomcat.getEngine());
	for (Connector additionalConnector : this.additionalTomcatConnectors) {
		tomcat.getService().addConnector(additionalConnector);
	}
	prepareContext(tomcat.getHost(), initializers);
	return getTomcatWebServer(tomcat);
}

根據上面的代碼,我們發現其主要做了兩件事情,第一件事就是把 Connnctor (我們稱之為連接器)物件添加到 Tomcat 中,第二件事就是configureEngine,這連接器我們勉強能理解(不理解后面會述說),那這個Engine是什么呢?我們查看tomcat.getEngine()的原始碼:

public Engine getEngine() {
	Service service = getServer().findServices()[0];
	if (service.getContainer() != null) {
		return service.getContainer();
	}
	Engine engine = new StandardEngine();
	engine.setName( "Tomcat" );
	engine.setDefaultHost(hostname);
	engine.setRealm(createDefaultRealm());
	service.setContainer(engine);
	return engine;
}

根據上面的原始碼,我們發現,原來這個 Engine 是容器,我們繼續跟蹤原始碼,找到Container介面

上圖中,我們看到了4個子介面,分別是 Engine,Host,Context,Wrapper,我們從繼承關系上可以知道他們都是容器,那么他們到底有啥區別呢?我看看他們的注釋是怎么說的,

/**
 If used, an Engine is always the top level Container in a Catalina
 * hierarchy. Therefore, the implementation's <code>setParent()</code> method
 * should throw <code>IllegalArgumentException</code>.
 *
 * @author Craig R. McClanahan
 */
public interface Engine extends Container {
    //省略代碼
}
/**
 * <p>
 * The parent Container attached to a Host is generally an Engine, but may
 * be some other implementation, or may be omitted if it is not necessary.
 * </p><p>
 * The child containers attached to a Host are generally implementations
 * of Context (representing an individual servlet context).
 *
 * @author Craig R. McClanahan
 */
public interface Host extends Container {
//省略代碼

}
/*** </p><p>
 * The parent Container attached to a Context is generally a Host, but may
 * be some other implementation, or may be omitted if it is not necessary.
 * </p><p>
 * The child containers attached to a Context are generally implementations
 * of Wrapper (representing individual servlet definitions).
 * </p><p>
 *
 * @author Craig R. McClanahan
 */
public interface Context extends Container, ContextBind {
    //省略代碼
}
/**</p><p>
 * The parent Container attached to a Wrapper will generally be an
 * implementation of Context, representing the servlet context (and
 * therefore the web application) within which this servlet executes.
 * </p><p>
 * Child Containers are not allowed on Wrapper implementations, so the
 * <code>addChild()</code> method should throw an
 * <code>IllegalArgumentException</code>.
 *
 * @author Craig R. McClanahan
 */
public interface Wrapper extends Container {

    //省略代碼
}

上面的注釋翻譯過來就是,Engine是最高級別的容器,其子容器是Host,Host的子容器是Context,WrapperContext的子容器,所以這4個容器的關系就是父子關系,也就是Engine>Host>Context>Wrapper,我們再看看Tomcat類的原始碼:

//部分原始碼,其余部分省略,
public class Tomcat {
//設定連接器
     public void setConnector(Connector connector) {
        Service service = getService();
        boolean found = false;
        for (Connector serviceConnector : service.findConnectors()) {
            if (connector == serviceConnector) {
                found = true;
            }
        }
        if (!found) {
            service.addConnector(connector);
        }
    }
    //獲取service
       public Service getService() {
        return getServer().findServices()[0];
    }
    //設定Host容器
     public void setHost(Host host) {
        Engine engine = getEngine();
        boolean found = false;
        for (Container engineHost : engine.findChildren()) {
            if (engineHost == host) {
                found = true;
            }
        }
        if (!found) {
            engine.addChild(host);
        }
    }
    //獲取Engine容器
     public Engine getEngine() {
        Service service = getServer().findServices()[0];
        if (service.getContainer() != null) {
            return service.getContainer();
        }
        Engine engine = new StandardEngine();
        engine.setName( "Tomcat" );
        engine.setDefaultHost(hostname);
        engine.setRealm(createDefaultRealm());
        service.setContainer(engine);
        return engine;
    }
    //獲取server
       public Server getServer() {

        if (server != null) {
            return server;
        }

        System.setProperty("catalina.useNaming", "false");

        server = new StandardServer();

        initBaseDir();

        // Set configuration source
        ConfigFileLoader.setSource(new CatalinaBaseConfigurationSource(new File(basedir), null));

        server.setPort( -1 );

        Service service = new StandardService();
        service.setName("Tomcat");
        server.addService(service);
        return server;
    }

    //添加Context容器
      public Context addContext(Host host, String contextPath, String contextName,
            String dir) {
        silence(host, contextName);
        Context ctx = createContext(host, contextPath);
        ctx.setName(contextName);
        ctx.setPath(contextPath);
        ctx.setDocBase(dir);
        ctx.addLifecycleListener(new FixContextListener());

        if (host == null) {
            getHost().addChild(ctx);
        } else {
            host.addChild(ctx);
        }

    //添加Wrapper容器
         public static Wrapper addServlet(Context ctx,
                                      String servletName,
                                      Servlet servlet) {
        // will do class for name and set init params
        Wrapper sw = new ExistingStandardWrapper(servlet);
        sw.setName(servletName);
        ctx.addChild(sw);

        return sw;
    }

}

閱讀TomcatgetServer()我們可以知道,Tomcat的最頂層是Server,Server 就是Tomcat的實體,一個Tomcat一個Server;通過getEngine()我們可以了解到 Server 下面是 Service,而且是多個,一個 Service 代表我們部署的一個應用,而且我們還可以知道,Engine容器,一個service只有一個;根據父子關系,我們看setHost()原始碼可以知道,host容器有多個;同理,我們發現addContext()原始碼下,Context也是多個;addServlet()表明Wrapper容器也是多個,而且這段代碼也暗示了,其實WrapperServlet是一層意思,另外我們根據setConnector原始碼可以知道,連接器(Connector)是設定在service下的,而且是可以設定多個連接器(Connector),

根據上面分析,我們可以小結下:Tomcat 主要包含了2個核心組件,連接器(Connector)和容器(Container),用圖表示如下:

一個Tomcat是一個Server,一個Server下有多個service,也就是我們部署的多個應用,一個應用下有多個連接器(Connector)和一個容器(Container),容器下有多個子容器,關系用圖表示如下:

Engine下有多個Host子容器,Host下有多個Context子容器,Context下有多個Wrapper子容器,

總結

Spring Boot 的啟動是通過new SpringApplication()實體來啟動的,啟動程序主要做如下幾件事情:> 1. 配置屬性 > 2. 獲取監聽器,發布應用開始啟動事件 > 3. 初始化輸入引數 > 4. 配置環境,輸出banner > 5. 創建背景關系 > 6. 預處理背景關系 > 7. 重繪背景關系 > 8. 再重繪背景關系 > 9. 發布應用已經啟動事件 > 10. 發布應用啟動完成事件

而啟動 Tomcat 就是在第7步中“重繪背景關系”;Tomcat 的啟動主要是初始化2個核心組件,連接器(Connector)和容器(Container),一個 Tomcat 實體就是一個 Server,一個 Server 包含多個 Service,也就是多個應用程式,每個 Service 包含多個連接器(Connetor)和一個容器(Container),而容器下又有多個子容器,按照父子關系分別為:Engine,Host,Context,Wrapper,其中除了 Engine 外,其余的容器都是可以有多個,

下期展望

本期文章通過SpringBoot的啟動來窺探了Tomcat的內部結構,下一期,我們來分析下本次文章中的連接器(Connetor)和容器(Container)的作用,敬請期待,

推薦去我的博客閱讀更多:

1.Java JVM、集合、多執行緒、新特性系列教程

2.Spring MVC、Spring Boot、Spring Cloud 系列教程

3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程

4.Java、后端、架構、阿里巴巴等大廠最新面試題

覺得不錯,別忘了點贊+轉發哦!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/174391.html

標籤:Java

上一篇:我終于看懂了HBase,太不容易了...

下一篇:初識Nginx——前后端發布、Nginx反向代理

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more