主頁 > 後端開發 > tomcat8.5.57原始碼閱讀筆記4 - daemon.start()

tomcat8.5.57原始碼閱讀筆記4 - daemon.start()

2020-09-16 12:16:15 後端開發

Bootstrap#start()

daemon = bootstrap, 所以呼叫的還是 org.apache.catalina.startup.Bootstrap#start()

public void start() throws Exception {
    if (catalinaDaemon == null) {
        init();
    }

    Method method = catalinaDaemon.getClass().getMethod("start", (Class[]) null);
    // 反射呼叫 Catalina#start()
    method.invoke(catalinaDaemon, (Object[]) null);
}

反射呼叫了 Catalina#start() 方法, 進行啟動作業.

 

Catalina#start()

public void start() {
    if (getServer() == null) {
        load();
    }
    if (getServer() == null) {
        log.fatal("Cannot start server. Server instance is not configured.");
        return;
    }

    //獲取當前納秒值
    long t1 = System.nanoTime();

    // Start the new server
    try {
        //StandardServer#start(), 最終呼叫的是 StandardServer#startInternal()
        getServer().start();
    } catch (LifecycleException e) {
        log.fatal(sm.getString("catalina.serverStartFail"), e);
        try {
            getServer().destroy();
        } catch (LifecycleException e1) {
            log.debug("destroy() failed for failed Server ", e1);
        }
        return;
    }

    //再次獲取當前納秒值, 計算啟動花了多長時間
    long t2 = System.nanoTime();
    if(log.isInfoEnabled()) {
        log.info("Server startup in " + ((t2 - t1) / 1000000) + " ms");
    }

    // Register shutdown hook
    // 注冊回呼方法, 用于安全關閉服務
    if (useShutdownHook) {
        if (shutdownHook == null) {
            shutdownHook = new CatalinaShutdownHook();
        }
        Runtime.getRuntime().addShutdownHook(shutdownHook);

        // If JULI is being used, disable JULI's shutdown hook since
        // shutdown hooks run in parallel and log messages may be lost
        // if JULI's hook completes before the CatalinaShutdownHook()
        LogManager logManager = LogManager.getLogManager();
        if (logManager instanceof ClassLoaderLogManager) {
            ((ClassLoaderLogManager) logManager).setUseShutdownHook(
                    false);
        }
    }

    // Bootstrap中會設定await為true,其目的在于讓tomcat在shutdown埠阻塞監聽關閉命令
    if (await) {
        await();
        stop();
    }
}

這里繼續呼叫了 Server 的 start 方法. 實際呼叫的是 org.apache.catalina.util.LifecycleBase#start(),

然后此方法中呼叫了抽象方法 startInternal(), 也就是 StandardServer#startInternal()

 

StandardServer#startInternal()

protected void startInternal() throws LifecycleException {
    // 事件通知 CONFIGURE_START_EVENT -> configure_start
    fireLifecycleEvent(CONFIGURE_START_EVENT, null);
    // 修改tomcat狀態為 STARTING, 這一步也會進行事件通知 START_EVENT -> start
    setState(LifecycleState.STARTING);

    //NamingResourcesImpl#startInternal()   全域資源
    //在server.xml中配置了該變數, pathname="conf/tomcat-users.xml"
    globalNamingResources.start();

    // Start our defined Services
    synchronized (servicesLock) {
        for (Service service : services) {
            // StandardService.startInternal()
            service.start();
        }
    }
}

service.start() 最終呼叫的是 StandardService.startInternal()

 

StandardService.startInternal()

protected void startInternal() throws LifecycleException {

    if(log.isInfoEnabled())
        log.info(sm.getString("standardService.start.name", this.name));
    setState(LifecycleState.STARTING);

    // Start our defined Container first
    if (engine != null) {
        synchronized (engine) {
            //StandardEngine#start() 最終呼叫 StandardEngine#startInternal()
            //這一句代碼, 就將 Host, Context, Wrapper 的 start 執行到了
            //host是通過 server.xml 決議得到
            //context是通過掃描檔案的方式決議得到, 有三種方式: xml配置方式, war包方式, 檔案夾方式
            //wrapper也是通過掃描jar包的方式得到
            engine.start();
        }
    }

    // 啟動Executor執行緒池, 默認情況下, 是空, 通過修改 server.xml中的tomcatThreadPool來改變
    synchronized (executors) {
        for (Executor executor: executors) {
            executor.start();
        }
    }

    // 啟動 MapperListener, 進行注冊功能
    // 最侄訓呼叫 MapperListener#startInternal() 方法
    //* 這個方法主要干了兩件事情
    //* 1. 將 MapperListener 注冊到容器和子容器(Host, Context, Wrapper)的 listeners 和 lifecycleListeners 中
    //* 2. 注冊 host, context, wrapper
    mapperListener.start();

    // Start our defined Connectors second
    synchronized (connectorsLock) {
        for (Connector connector: connectors) {
            try {
                // If it has already failed, don't try and start it
                if (connector.getState() != LifecycleState.FAILED) {
                    //最終呼叫 Connector.startInternal()
                    connector.start();
                }
            } catch (Exception e) {
                log.error(sm.getString(
                        "standardService.connector.startFailed",
                        connector), e);
            }
        }
    }
}

這里和初始化的時候很像, 初始化的時候, 是對他們分別初始化, 而這里, 是對他們分別啟動

1. engine.start() : 啟動 engine, 里面會啟動 Host, Context, Wrapper. 最侄訓呼叫 StandardEngine#startInternal() 

2. executor.start() : 啟動執行緒池, 默認情況下, 這個執行緒池為空.

3. mapperListener.start() : 將MapperListener注冊到 Host,Context,Wrapper 的監聽器中, 然后對 Host, Context, Wrapper 進行映射

4. connector.start() : 啟動連接器

 

StandardEngine#startInternal() 

  

protected synchronized void startInternal() throws LifecycleException {

    // Log our server identification information
    if(log.isInfoEnabled())
        log.info( "Starting Servlet Engine: " + ServerInfo.getServerInfo());

    // Standard container startup
    //通過決議 server.xml 檔案, 可以拿到 engine下面有一個 host, 也就是說, StandardEngine.children有一個host
    super.startInternal();
}

super.startInternal() 呼叫的是父類 org.apache.catalina.core.ContainerBase#startInternal() 方法:

protected synchronized void startInternal() throws LifecycleException {

    // Start our subordinate components, if any
    logger = null;
    getLogger();
    //集群客戶端
    Cluster cluster = getClusterInternal();
    if (cluster instanceof Lifecycle) {
        //如果有集群的話, 則會啟動集群
        ((Lifecycle) cluster).start();
    }
    //
    Realm realm = getRealmInternal();
    if (realm instanceof Lifecycle) {
        ((Lifecycle) realm).start();
    }

    // Start our child containers, if any
    // 把子容器的啟動步驟放在執行緒中處理,默認情況下執行緒池只有一個執行緒處理任務佇列
    //StandardEngine 呼叫的時候, 這里拿到的 children 有一個值: StandardHost[localhost]
    // <Engine name="Catalina" defaultHost="localhost">
    //      <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" startStopThreads="1">
    // </engine>
    Container children[] = findChildren();
    List<Future<Void>> results = new ArrayList<>();
    for (Container child : children) {
        //StartChild 的 call方法, 呼叫的是 child.start() 方法, 最侄訓呼叫 StandardHost.startInternal
        results.add(startStopExecutor.submit(new StartChild(child)));
    }

    MultiThrowable multiThrowable = null;

    // 阻塞當前執行緒,直到子容器start完成
    for (Future<Void> result : results) {
        try {
            result.get();
        } catch (Throwable e) {
            log.error(sm.getString("containerBase.threadedStartFailed"), e);
            if (multiThrowable == null) {
                multiThrowable = new MultiThrowable();
            }
            multiThrowable.add(e);
        }

    }
    if (multiThrowable != null) {
        throw new LifecycleException(sm.getString("containerBase.threadedStartFailed"),
                multiThrowable.getThrowable());
    }

    // Start the Valves in our pipeline (including the basic), if any
    // 啟用 Pipeline --> StandardPipeline#startInternal()
    if (pipeline instanceof Lifecycle) {
        ((Lifecycle) pipeline).start();
    }

    //StandardHost呼叫時, 激發 STARTING 監聽器 HostConfig,最侄訓呼叫 HostConfig#start() 方法 - 這一步很關鍵
    setState(LifecycleState.STARTING);

    // Start our thread
    // 開啟ContainerBackgroundProcessor執行緒用于呼叫子容器的backgroundProcess方法,
    // 默認情況下backgroundProcessorDelay=-1,不會啟用該執行緒
    threadStart();
}

1. 通過Future框架異步呼叫 StandardHost#startInternal() 方法, 并阻塞等待所有當前 engine 下所有的 Host 啟動完成

2. setState() 時, 會激發監聽器 HostConfig. 這個HostConfig 是在 決議 Server.xml 的時候, 創建并系結的.

 

StandardHost#startInternal()

protected synchronized void startInternal() throws LifecycleException {
    // Set error report valve
    // errorValve默認使用 ErrorReportValve
    String errorValve = getErrorReportValveClass();
    if ((errorValve != null) && (!errorValve.equals(""))) {
        try {
            boolean found = false;
            // 如果所有的閥門中已經存在這個實體,則不進行處理,否則添加到 Pipeline 中
            Valve[] valves = getPipeline().getValves();
            for (Valve valve : valves) {
                if (errorValve.equals(valve.getClass().getName())) {
                    found = true;
                    break;
                }
            }
            // 如果未找到則添加到 Pipeline 中,注意是添加到 basic valve 的前面
            // 默認情況下,first valve 是 AccessLogValve,basic 是 StandardHostValve
            if(!found) {
                Valve valve =
                    (Valve) Class.forName(errorValve).getConstructor().newInstance();
                getPipeline().addValve(valve);
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error(sm.getString(
                    "standardHost.invalidErrorReportValveClass",
                    errorValve), t);
        }
    }
    // 呼叫父類 ContainerBase,完成統一的啟動動作
    super.startInternal();
}

這里的 startInternal() 就是上面的 org.apache.catalina.core.ContainerBase#startInternal() 方法.

但是此時, host 的 children 是空的, 所以里面并沒有能夠呼叫 StandardContext#startInternal()

 

HostConfig#lifecycleEvent()

HostConfig 實作了 LifecycleListener 介面. 所以實際上, 他是一個監聽器. 

setState() 的時候, 會激發 HostConfig的監聽器方法 lifecycleEvent(), 

public void lifecycleEvent(LifecycleEvent event) {

    // Identify the host we are associated with
    try {
        host = (Host) event.getLifecycle();
        if (host instanceof StandardHost) {
            setCopyXML(((StandardHost) host).isCopyXML());
            setDeployXML(((StandardHost) host).isDeployXML());
            setUnpackWARs(((StandardHost) host).isUnpackWARs());
            setContextClass(((StandardHost) host).getContextClass());
        }
    } catch (ClassCastException e) {
        log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e);
        return;
    }

    // Process the event that has occurred
    //判斷事件是否由 Host 發出,并且為 HostConfig 設定屬性
    if (event.getType().equals(Lifecycle.PERIODIC_EVENT)) {
        check();
    } else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
        beforeStart();
    } else if (event.getType().equals(Lifecycle.START_EVENT)) {
        start();
    } else if (event.getType().equals(Lifecycle.STOP_EVENT)) {
        stop();
    }
}

根據當前事件的狀態, 會呼叫這里的 start() 方法. 此start 方法中, 會執行 部署 webapp 的方法 : org.apache.catalina.startup.HostConfig#deployApps()

protected void deployApps() {
    File appBase = host.getAppBaseFile();
    File configBase = host.getConfigBaseFile();
    // 過濾出 webapp 要部署應用的目錄
    String[] filteredAppPaths = filterAppPaths(appBase.list());
    // Deploy XML descriptors from configBase
    // 1. xml部署 - 不推薦這么使用
    // <host><context docBase="D://abc/eee" path="sdm" reloadable="true"></context></host>
    // 部署 xml 描述檔案
    deployDescriptors(configBase, configBase.list());
    // Deploy WARs
    // 2. war包部署
    // 解壓 war 包,但是這里還不會去啟動應用
    deployWARs(appBase, filteredAppPaths);
    // Deploy expanded folders
    // 3. 目錄部署
    // 處理已經存在的目錄,前面解壓的 war 包不會再行處理
    deployDirectories(appBase, filteredAppPaths);
}

原始碼里面放的幾個 Context , 都是目錄結構的, 所以會走 deployDirectories() 方法

protected void deployDirectories(File appBase, String[] files) {

    if (files == null)
        return;

    ExecutorService es = host.getStartStopExecutor();
    List<Future<?>> results = new ArrayList<>();

    for (String file : files) {

        if (file.equalsIgnoreCase("META-INF"))
            continue;
        if (file.equalsIgnoreCase("WEB-INF"))
            continue;
        File dir = new File(appBase, file);
        if (dir.isDirectory()) {
            ContextName cn = new ContextName(file, false);

            if (isServiced(cn.getName()) || deploymentExists(cn.getName()))
                continue;

            results.add(es.submit(new DeployDirectory(this, cn, dir)));
        }
    }

    for (Future<?> result : results) {
        try {
            result.get();
        } catch (Exception e) {
            log.error(sm.getString(
                    "hostConfig.deployDir.threaded.error"), e);
        }
    }
}

1. 通過除錯, 能看到:

這里的每一個檔案夾, 轉換之后, 就是一個 Context .

  

 2. 這里又出現了 Future . 這次提交的是 DeployDirectory, 是 HostConfig 的一個內部類. 看一下他的 run 方法:

private static class DeployDirectory implements Runnable {

    private HostConfig config;
    private ContextName cn;
    private File dir;

    public DeployDirectory(HostConfig config, ContextName cn, File dir) {
        this.config = config;
        this.cn = cn;
        this.dir = dir;
    }

    @Override
    public void run() {
        config.deployDirectory(cn, dir);
    }
}

實際呼叫的, 還是 HostConfig 的方法:

protected void deployDirectory(ContextName cn, File dir) {
    long startTime = 0;
    // Deploy the application in this directory
    if( log.isInfoEnabled() ) {
        startTime = System.currentTimeMillis();
        log.info(sm.getString("hostConfig.deployDir",
                dir.getAbsolutePath()));
    }

    Context context = null;
    File xml = new File(dir, Constants.ApplicationContextXml);
    File xmlCopy = new File(host.getConfigBaseFile(), cn.getBaseName() + ".xml");

    DeployedApplication deployedApp;
    boolean copyThisXml = isCopyXML();
    boolean deployThisXML = isDeployThisXML(dir, cn);
    try {
        if (deployThisXML && xml.exists()) {
            synchronized (digesterLock) {
                try {
                    //StandardContext
                    context = (Context) digester.parse(xml);
                } catch (Exception e) {
                   ......
                } finally {
                    digester.reset();
                    if (context == null) {
                        context = new FailedContext();
                    }
                }
            }

            if (copyThisXml == false && context instanceof StandardContext) {
                // Host is using default value. Context may override it.
                copyThisXml = ((StandardContext) context).getCopyXML();
            }

            if (copyThisXml) {
                Files.copy(xml.toPath(), xmlCopy.toPath());
                context.setConfigFile(xmlCopy.toURI().toURL());
            } else {
                context.setConfigFile(xml.toURI().toURL());
            }
        } else if (!deployThisXML && xml.exists()) {
            // Block deployment as META-INF/context.xml may contain security
            // configuration necessary for a secure deployment.
            log.error(sm.getString("hostConfig.deployDescriptor.blocked",
                    cn.getPath(), xml, xmlCopy));
            context = new FailedContext();
        } else {
            context = (Context) Class.forName(contextClass).getConstructor().newInstance();
        }

        // 實體化 ContextConfig,作為 LifecycleListener 添加到 Context 容器中,這和 StandardHost 的套路一樣,都是使用 XXXConfig
        Class<?> clazz = Class.forName(host.getConfigClass());
        LifecycleListener listener = (LifecycleListener) clazz.getConstructor().newInstance();
        context.addLifecycleListener(listener);

        context.setName(cn.getName());
        context.setPath(cn.getPath());
        context.setWebappVersion(cn.getVersion());
        context.setDocBase(cn.getBaseName());
        // 實體化 StandardContext 之后,為 Host 添加子節點
        // 這里呼叫的是 StandardHost#addChild()
        host.addChild(context);
    } catch (Throwable t) {
        ......
    } finally {
        ......
    }
    ......
}

這里出現了 host.addChild(context) 方法, 需要回到 StandardHost 類中去看

//org.apache.catalina.core.StandardHost#addChild
public void addChild(Container child) {

    if (!(child instanceof Context))
        throw new IllegalArgumentException
            (sm.getString("standardHost.notContext"));

    //加入一個監聽器
    child.addLifecycleListener(new MemoryLeakTrackingListener());

    // Avoid NPE for case where Context is defined in server.xml with only a
    // docBase
    Context context = (Context) child;
    if (context.getPath() == null) {
        ContextName cn = new ContextName(context.getDocBase(), true);
        context.setPath(cn.getPath());
    }
    super.addChild(child);
}

接著看父類中的 addChild()

//org.apache.catalina.core.ContainerBase#addChild
public void addChild(Container child) {
    if (Globals.IS_SECURITY_ENABLED) {
        PrivilegedAction<Void> dp =
            new PrivilegedAddChild(child);
        AccessController.doPrivileged(dp);
    } else {
        addChildInternal(child);
    }
}

private void addChildInternal(Container child) {

    if( log.isDebugEnabled() )
        log.debug("Add child " + child + " " + this);
    synchronized(children) {
        if (children.get(child.getName()) != null)
            throw new IllegalArgumentException("addChild:  Child name '" +
                                               child.getName() +
                                               "' is not unique");
        child.setParent(this);  // May throw IAE
        children.put(child.getName(), child);
    }

    // Start child
    // Don't do this inside sync block - start can be a slow process and
    // locking the children object can cause problems elsewhere
    try {
        if ((getState().isAvailable() ||
                LifecycleState.STARTING_PREP.equals(getState())) &&
                startChildren) {
            //addChild的時候, 呼叫啟動方法, 決議Server.xml時, 不會進此方法
            //StandardHost的時候, 會進此方法, 呼叫的是 StandardContext.start() -> StandardContext.startInternal()
            //StandardContext的時候, 會進此方法, 呼叫的是 StandardWrapper.start() -> StandardWrapper.startInternal()
            child.start();
        }
    } catch (LifecycleException e) {
        log.error("ContainerBase.addChild: start: ", e);
        throw new IllegalStateException("ContainerBase.addChild: start: " + e);
    } finally {
        fireContainerEvent(ADD_CHILD_EVENT, child);
    }
}

此時 child 是 StandardContext. 所以會呼叫

org.apache.catalina.util.LifecycleBase#start() :

public final synchronized void start() throws LifecycleException {

    if (LifecycleState.STARTING_PREP.equals(state) || LifecycleState.STARTING.equals(state) ||
            LifecycleState.STARTED.equals(state)) {

        if (log.isDebugEnabled()) {
            Exception e = new LifecycleException();
            log.debug(sm.getString("lifecycleBase.alreadyStarted", toString()), e);
        } else if (log.isInfoEnabled()) {
            log.info(sm.getString("lifecycleBase.alreadyStarted", toString()));
        }

        return;
    }

    if (state.equals(LifecycleState.NEW)) {
        //results.add(startStopExecutor.submit(new StartChild(child)));
        //異步執行 StandardHost.start() 方法時, 會走這里
        init();
    } else if (state.equals(LifecycleState.FAILED)) {
        stop();
    } else if (!state.equals(LifecycleState.INITIALIZED) &&
            !state.equals(LifecycleState.STOPPED)) {
        invalidTransition(Lifecycle.BEFORE_START_EVENT);
    }

    try {
        setStateInternal(LifecycleState.STARTING_PREP, null, false);
        startInternal();
        if (state.equals(LifecycleState.FAILED)) {
            // This is a 'controlled' failure. The component put itself into the
            // FAILED state so call stop() to complete the clean-up.
            stop();
        } else if (!state.equals(LifecycleState.STARTING)) {
            // Shouldn't be necessary but acts as a check that sub-classes are
            // doing what they are supposed to.
            invalidTransition(Lifecycle.AFTER_START_EVENT);
        } else {
            setStateInternal(LifecycleState.STARTED, null, false);
        }
    } catch (Throwable t) {
        // This is an 'uncontrolled' failure so put the component into the
        // FAILED state and throw an exception.
        handleSubClassException(t, "lifecycleBase.startFail", toString());
    }
}

StandardContext 就是在這個方法中進行初始化, 以及啟動的.

 

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

標籤:Java

上一篇:tomcat8.5.57原始碼閱讀筆記3 - daemon.load(args)

下一篇:tomcat8.5.57原始碼閱讀筆記4.1 - StandardContext的init和start

標籤雲
其他(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