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

 

posted @ 2016-11-26 23:58  liqipeng  阅读(3110)  评论(0编辑  收藏  举报