Java載入資源檔幾種方法

來源:互聯網
上載者:User

標籤:ade   read   current   ssl   tag   推薦   分隔字元   pat   ati   

from: http://andyzhu.blog.51cto.com/4386758/775836/

import java.net.URL;  import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;  public class TestMain {     public static void main(String[] args) {          // ############################################################################################################         // 1:使用本類的Class類的getResource()方法         // 在當前包尋找資源(指定相對路徑,其他均返回null。)         URL filePathUrl1 = TestMain.class.getResource("beans_sameLocation.xml");          // 在根尋找資源(需要檔案分隔字元"/",其他均返回null。)         URL filePathUrl2 = TestMain.class.getResource("/beans.xml");          // 在不同包內尋找資源(指定相對路徑(需要檔案分隔字元"/"),其他均返回null。)         URL filePathUrl3 = TestMain.class.getResource("/test/spring/beanpost/file/beans_diffLocation.xml");          // ############################################################################################################         // 2:使用本類的Class類的ClassLoader類的getResource()方法         // 在相同包內尋找資源,總是返回null。         // URL filePathUrl3 =         // TestMain.class.getClassLoader().getResource("beans_sameLocation.xml");          // 在根尋找資源,指定相對路徑,其他均返回null。         URL filePathUrl4 = TestMain.class.getClassLoader().getResource("beans.xml");          // 在不同包內尋找資源,指定相對路徑,其他均返回null。         URL filePathUrl5 = TestMain.class.getClassLoader().getResource("test/spring/beanpost/file/beans_diffLocation.xml");          // ############################################################################################################         // 3:使用ClassLoader類的getSystemResource()方法         // 在指定包內尋找資源,指定相對路徑,其他均返回null。         URL filePathUrl6 = ClassLoader.getSystemResource("test/spring/beanpost/beans_sameLocation.xml");         // 同上         URL filePathUrl7 = ClassLoader.getSystemClassLoader().getResource("test/spring/beanpost/beans_sameLocation.xml");          // 在根尋找,指定相對路徑,其他均返回null。         URL filePathUrl8 = ClassLoader.getSystemResource("beans.xml");         // 同上         URL filePathUrl9 = ClassLoader.getSystemClassLoader().getResource("beans.xml");          // ############################################################################################################         // 4:使用Thread載入資源(推薦此方法)         // 在指定包內尋找資源,(相對路徑),其他均返回null。         filePathUrl6 = Thread.currentThread().getContextClassLoader().getResource("test/spring/beanpost/beans_sameLocation.xml");          // 在根尋找,(相對路徑),其他均返回null。         filePathUrl7 = Thread.currentThread().getContextClassLoader().getResource("beans.xml");          // 在不同包內尋找資源,(相對路徑),其他均返回null。         filePathUrl8 = Thread.currentThread().getContextClassLoader().getResource("test/spring/beanpost/file/beans_diffLocation.xml");          // ############################################################################################################          System.out.println(filePathUrl1.getFile());         System.out.println(filePathUrl2.getFile());         System.out.println(filePathUrl3.getFile());         System.out.println(filePathUrl4.getFile());         System.out.println(filePathUrl5.getFile());         System.out.println(filePathUrl6.getFile());         System.out.println(filePathUrl7.getFile());         System.out.println(filePathUrl8.getFile());         System.out.println(filePathUrl9.getFile());         System.out.println("----------------------------------------------------------------------------------------");         System.getProperties().list(System.out);         System.out.println("----------------------------------------------------------------------------------------");          ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");         Animal animal = (Animal) ac.getBean("animal");         System.out.println(animal.speak());         animal.setAge(88);          Animal animal0 = (Animal) ac.getBean("animal");         System.out.println(animal0.speak());          ApplicationContext ac1 = new ClassPathXmlApplicationContext("beans.xml");         Animal animal1 = (Animal) ac1.getBean("animal");         System.out.println(animal1.speak());     } } 

 

Java載入資源檔幾種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.