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. 关于IOS中safari下的select下拉菜单,文字过长不换行的问题

    今天遇到下图这种问题,文字过长,显示不全.折腾了老半天,在网上搜了半天也找不到解决方案. 于是问了下同事,同事提到了<optgroup>,这个标签厉害. <optgroup> ...

  2. CSS3鼠标滑过动画线条边框特效

    基于CSS属性animation动画制作,效果流畅弹性十足 效果展示 http://hovertree.com/texiao/css3/32/ 源码下载:http://hovertree.com/h/ ...

  3. 几句话就能让你理解:this、闭包、原型链

    以下是个人对这三个老大难的总结(最近一直在学习原生JS,翻了不少书,不少文档,虽然还是新手,但我会继续坚持走我自己的路) 原型链 所有对象都是基于Object.prototype,Object.pro ...

  4. JavaScript 数据属性和访问器属性

    在JavaScript中对象被定义为"无序属性的集合,其属性可以包含基本值.对象或函数."通俗点讲,我们可以把对象理解为一组一组的名值对,其中值可以是数据或函数. 创建自定义对象通 ...

  5. Javascript前端和JAVA后端对加密库的处理实例

    前端加密 Javascript的加解密有开源的库,http://www.oschina.net/p/crypto-js/ 如下是具体的使用例子 <!DOCTYPE html> <ht ...

  6. 小程序https Android 安卓可以发request请求,IOS 苹果 发请求失败问题

    如果一个机器可以发送成功,一个机器发送失败,那多半是是域名的https支持的问题 那就用腾讯云的这个ssl测试工具检测下 https://www.qcloud.com/product/ssl#user ...

  7. iOS编码规范

      The official raywenderlich.com Objective-C style guide.   This style guide outlines the coding con ...

  8. Oracle发送邮件,支持HTML,多收件人,多附件

    Oracle发邮件,权限问题 - 创建 ACL BEGIN DBMS_NETWORK_ACL_ADMIN.CREATE_ACL(acl => 'email_server_permissions. ...

  9. HashMap源码分析

    最近一直特别忙,好不容易闲下来了.准备把HashMap的知识总结一下,很久以前看过HashMap源码.一直想把集合类的知识都总结一下,加深自己的基础.我觉的java的集合类特别重要,能够深刻理解和应用 ...

  10. 项目游戏开发日记 No.0x000002

    14软二杨近星(2014551622) 项目开发的开始, 到现在已经很久了, 软件工程的课也上了很久了, 不过, 我们的游戏现在依然还没有影子, 只能说...还是啥也不会... 从一开始, 兴致勃勃地 ...