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. 怎样给div增加resize事件

    当浏览器窗口被调整到一个新的高度或宽度时,就会触发resize事件,这个事件在window上面触发,那么如何给div元素增加resize事件,监听div的高度或宽度的改变呢? 先来回答另一个问题,监听 ...

  2. Sublime Text通过插件编译Sass为CSS及中文编译异常解决

    虽然PostCSS才是未来,但是Sass成熟稳定,拥有一大波忠实的使用者,及开源项目,且最近Bootstrap 4 alpha也从Less转到Sass了.所以了解Sass还是非常有必要的. 基于快速开 ...

  3. [Android]使用Dagger 2依赖注入 - DI介绍(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5092083.html 使用Dagger 2依赖注入 - DI介 ...

  4. 【转】Android SDK Samples,学习Android的好方法

    转载地址:http://blog.csdn.net/rowland001/article/details/50886288 从今天开始呢,我要开始学习Google家自己出的Android代码示例,总觉 ...

  5. Oracle汉字转拼音package

    --函数GetHzFullPY(string)用于获取汉字字符串的拼音 --select GetHzFullPY('中华人民共和国') from dual; --返回:ZhongHuaRenMinGo ...

  6. Java实现压缩与解压缩

    import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...

  7. head/tail实现

         只实现了head/tail的基本功能,默认显示十行及-n参数.       一.使用带缓冲的系统调用.       write/read等系统调用是不带缓冲的,可以包装一层,使其带缓冲. t ...

  8. vi(vim)键盘图及其基本命令

    进入vi vi filename                打开或新建文件,并将光标置于第一行首 vi +n filename           打开文件,并将光标置于第 n行首 vi + fi ...

  9. node 异步回调解决方法之yield

    先看如何使用 使用的npm包为genny,npm 安装genny,使用 node -harmony 文件(-harmony 为使用es6属性启动参数) 启动项目 var genny= require( ...

  10. HOLOLENS 扫描特效 及得出扫描结果(SurfacePlane)

    HOLOLENS 扫描特效 及得出扫描结果(SurfacePlane) 要求只扫出地板和墙, 由于地板和墙面积较大 扫描结果 HOLOTOOLKIT老版本点在参数调节PlaneFinding.Find ...