<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • 在Java的反射中Class.forName和ClassLoader有哪些區(qū)別

            這篇文章給大家分享的是有關(guān)在Java的反射中Class.forName和ClassLoader有哪些區(qū)別的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

            創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計、成都做網(wǎng)站與策劃設(shè)計,廣水網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:廣水等地區(qū)。廣水做網(wǎng)站價格咨詢:028-86922220

            解釋

            在java中Class.forName()和ClassLoader都可以對類進行加載。ClassLoader就是遵循 雙親委派模型 最終調(diào)用啟動類加載器的類加載器,實現(xiàn)的功能是“通過一個類的全限定名來獲取描述此類的二進制字節(jié)流”,獲取到二進制流后放到JVM中。Class.forName()方法實際上也是調(diào)用的CLassLoader來實現(xiàn)的。

            Class.forName(String className);這個方法的源碼是

             @CallerSensitive
             public static Class<?> forName(String className)
             throws ClassNotFoundException {
             Class<?> caller = Reflection.getCallerClass();
             return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
             }

            最后調(diào)用的方法是forName0這個方法,在這個forName0方法中的第二個參數(shù)被默認設(shè)置為了true,true代表是否對加載的類進行初始化,對類進行初始化,代表會執(zhí)行類中的靜態(tài)代碼塊,以及對靜態(tài)變量的賦值等操作。

            也可以調(diào)用Class.forName(String name, boolean initialize,ClassLoader loader)方法來手動選擇在加載類的時候是否要對類進行初始化。Class.forName(String name, boolean initialize,ClassLoader loader)的源碼如下:

             /* @param name fully qualified name of the desired class
             * @param initialize if {@code true} the class will be initialized.
             * See Section 12.4 of <em>The Java Language Specification</em>.
             * @param loader class loader from which the class must be loaded
             * @return class object representing the desired class
             *
             * @exception LinkageError if the linkage fails
             * @exception ExceptionInInitializerError if the initialization provoked
             * by this method fails
             * @exception ClassNotFoundException if the class cannot be located by
             * the specified class loader
             *
             * @see java.lang.Class#forName(String)
             * @see java.lang.ClassLoader
             * @since 1.2
             */
             @CallerSensitive
             public static Class<?> forName(String name, boolean initialize,
             ClassLoader loader)
             throws ClassNotFoundException
             {
             Class<?> caller = null;
             SecurityManager sm = System.getSecurityManager();
             if (sm != null) {
             // Reflective call to get caller class is only needed if a security manager
             // is present. Avoid the overhead of making this call otherwise.
             caller = Reflection.getCallerClass();
             if (sun.misc.VM.isSystemDomainLoader(loader)) {
             ClassLoader ccl = ClassLoader.getClassLoader(caller);
             if (!sun.misc.VM.isSystemDomainLoader(ccl)) {
             sm.checkPermission(
             SecurityConstants.GET_CLASSLOADER_PERMISSION);
             }
             }
             }
             return forName0(name, initialize, loader, caller);
             }

            源碼中的注釋只摘取了一部分,其中對參數(shù)initialize的描述是: if {@code true} the class will be initialized. 意思就是說:如果參數(shù)為true,則加載的類將會被初始化。

            下面還是舉例來說明結(jié)果吧:

            一個含有靜態(tài)代碼塊、靜態(tài)變量、賦值給靜態(tài)變量的靜態(tài)方法的類

            public class ClassForName {
            
             //靜態(tài)代碼塊
             static {
             System.out.println("執(zhí)行了靜態(tài)代碼塊");
             }
             //靜態(tài)變量
             private static String staticFiled = staticMethod();
            
             //賦值靜態(tài)變量的靜態(tài)方法
             public static String staticMethod(){
             System.out.println("執(zhí)行了靜態(tài)方法");
             return "給靜態(tài)字段賦值了";
             }
            }

            測試方法:

            public class MyTest {
             @Test
             public void test44(){
            
             try {
             Class.forName("com.test.mytest.ClassForName");
             System.out.println("#########分割符(上面是Class.forName的加載過程,下面是ClassLoader的加載過程)##########");
             ClassLoader.getSystemClassLoader().loadClass("com.test.mytest.ClassForName");
             } catch (ClassNotFoundException e) {
             e.printStackTrace();
             }
            
             }
            }

            運行結(jié)果:

            執(zhí)行了靜態(tài)代碼塊
            執(zhí)行了靜態(tài)方法
            #########分割符(上面是Class.forName的加載過程,下面是ClassLoader的加載過程)##########

            根據(jù)運行結(jié)果得出Class.forName加載類是將類進了初始化,而ClassLoader的loadClass并沒有對類進行初始化,只是把類加載到了虛擬機中。

            在我們熟悉的Spring框架中的IOC的實現(xiàn)就是使用的ClassLoader。

            而在我們使用JDBC時通常是使用Class.forName()方法來加載數(shù)據(jù)庫連接驅(qū)動。這是因為在JDBC規(guī)范中明確要求Driver(數(shù)據(jù)庫驅(qū)動)類必須向DriverManager注冊自己。

            以MySQL的驅(qū)動為例解釋:

            public class Driver extends NonRegisteringDriver implements java.sql.Driver { 
             // ~ Static fields/initializers 
             // --------------------------------------------- 
             
             // 
             // Register ourselves with the DriverManager 
             // 
             static { 
             try { 
             java.sql.DriverManager.registerDriver(new Driver()); 
             } catch (SQLException E) { 
             throw new RuntimeException("Can't register driver!"); 
             } 
             } 
             
             // ~ Constructors 
             // ----------------------------------------------------------- 
             
             /** 
             * Construct a new driver and register it with DriverManager 
             * 
             * @throws SQLException 
             * if a database error occurs. 
             */ 
             public Driver() throws SQLException { 
             // Required for Class.forName().newInstance() 
             } 
            }

            我們看到Driver注冊到DriverManager中的操作寫在了靜態(tài)代碼塊中,這就是為什么在寫JDBC時使用Class.forName()的原因了。

            感謝各位的閱讀!關(guān)于“在Java的反射中Class.forName和ClassLoader有哪些區(qū)別”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

            分享題目:在Java的反射中Class.forName和ClassLoader有哪些區(qū)別
            地址分享:http://www.jbt999.com/article4/pdpcie.html

            成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、品牌網(wǎng)站制作、面包屑導(dǎo)航響應(yīng)式網(wǎng)站、手機網(wǎng)站建設(shè)、定制網(wǎng)站

            廣告

            聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

            外貿(mào)網(wǎng)站建設(shè)

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 黄色成人视频免费观看 | 国产在线精品视频豆花 | 奇米成人网站 | 特级西西444Ww高清大胆 | 高清精品在线 |