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. Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】

    2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...

  2. JQuery效果-淡入淡出、滑动、动画

    一.JQuery Fading方法 JQuery 有四种fade方法 1.fadeIn() 淡入                       对应也有$(selector).fadeIn(speed, ...

  3. Linux 命令学习笔记

    文件基本操作 ls ,rm , mv , ln   ls ls [option] [files]   不带参数时,列出当前工作目录的内容 $ls   列出指定目录的内容 ls dir1 或个别文件 l ...

  4. 通过使用OpenVPN来构建一个VPN

    首先我们需要简单熟悉一下OpenVPN和VPN概念,方便我们在使用OpenVPN构建VPN时的操作~  VPN概述 VPN,即虚拟专用网络,其功能是:在公用网络上建立专用网络,进行加密通讯.在企业网络 ...

  5. swift学习笔记1——基础部分

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  6. 在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service

    在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service 1.在 /etc/rc.d/init.d/ 目录下创建一个名字和服务名完全相同的 shell 脚本文件 joyup ...

  7. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

  8. tomcat加密

    tomcat做虚拟主机的最好方法是复制,运行多个tomcat,避免tomcat挂掉,同时几个业务也挂掉 针对tomcat7,tomcat未实现 生成私钥证书文件: mkdir -p /usr/loca ...

  9. x86开启 HUGEPAGES

    HugePage,就是指的大页内存管理方式,在操作系统Linux环境中,内存是以页Page的方式进行分配,默认大小为4K,HugePage是传统4K Page的替代方案.顾名思义,是用HugePage ...

  10. Ubuntu 15.1 unity在顶部面板显示系统CPU/内存/网络速度

    全部的文件:http://files.cnblogs.com/files/xiaobo-Linux/ubuntu%E6%98%BE%E7%A4%BA%E7%B3%BB%E7%BB%9F%E7%BD%9 ...