Java加載資源文件幾種方法


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()); 
    } 
} 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM