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加载资源文件几种方法的更多相关文章

  1. Java加载资源文件的两种方法

    处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...

  2. java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...

  3. java加载资源文件

    className.class.getResourceAsStream 用法: 第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件c ...

  4. Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)

    Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1.       内联样式 ...

  5. java加载properties文件的六中基本方式实现

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...

  6. Maven,预加载资源文件

    预加载资源文件需要先启用功能: <build> <resources> <resource> <directory>src/main/resources ...

  7. js 动态加载事件的几种方法总结

    本篇文章主要是对js 动态加载事件的几种方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助   有些时候需要动态加载javascript事件的一些方法往往我们需要在 JS 中动态添 ...

  8. 动态加载资源文件(ResourceDictionary)

    原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

  9. Spring boot 国际化自动加载资源文件问题

    Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...

随机推荐

  1. 开发属于自己的yeoman脚手架(generator-reactpackage)

    自从前后端开始分离之后,前端项目工程化也显得越来越重要了,之前写过一篇搭建基于Angular+Requirejs+Grunt的前端项目教程,有兴趣的可以点这里去看 但是有些项目可以使用这种方式,但有些 ...

  2. [deviceone开发]-openPage的动画效果示例

    一.简介do_App的openPage支持16种过场动画,这个示例直观的展示16种动画的效果.适合初学者.二.效果图三.相关下载https://github.com/do-project/code4d ...

  3. 如果把带有html的标记的字符串从服务端传到页面上,需要对其进行编码。Ajax.JavaScriptStringEncode()

    controller: StringBuilder s = new StringBuilder(); string a = "<script>alert('我StringBuil ...

  4. 商业智能软件对比评测:FineBI和Tableau

    FineBI和Tableau是比较好的自助式商业智能软件,功能都很强大,是企业数据可视化不可或缺的利器,但两款产品还是有非常大的区别的,例如Tableau的功能全面且深入,更适合专业的数据分析人员,而 ...

  5. Microsoft Dynamics CRM 2013 Js Odata 查询

    实现功能:   在新建记录时,(大区,省区,城市)的值默认为当前用户的值.tips:字段均为lookup类型; function Default_region(){ var fromtype=Xrm. ...

  6. Android中使用Notification实现普通通知栏(Notification示例一)

    Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...

  7. Android InputType详解

    android:inputType 如果设置android:inputType = "number",则默认弹出的输入键盘为数字键盘,且输入的内容只能为数字. InputType文 ...

  8. JS导出excel 兼容ie、chrome、firefox

    运用js实现将页面中的table导出为excel文件,页面显示如下: 导出的excel文件显示如下: 实现代码: <!DOCTYPE html> <html> <head ...

  9. 多War项目中静态文件的共享方案

    [原创申明:文章为原创,欢迎非盈利性转载,但转载必须注明来源] 在互联网产品中,一般会有多个项目(Jar.WAR)组成一个产品线.这些WAR项目,因为使用相同的前端架构(jQuery.easyui等) ...

  10. linux命令-系统命令

    1.查看Linux磁盘空间大小 df -lh Filesystem 容量 已用 可用 已用% 挂载点 /dev/hda8 11G 6.0G 4.4G 58% / /dev/shm 236M 0 236 ...