java加载资源文件的几种方式

转载自: 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()); 
  •     } 

  •  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()); 
  •     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值