Java加载资源文件几种方法
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加载资源文件几种方法的更多相关文章
- Java加载资源文件的两种方法
处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...
- java加载properties文件的六种方法总结
java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...
- java加载资源文件
className.class.getResourceAsStream 用法: 第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件c ...
- Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)
Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1. 内联样式 ...
- java加载properties文件的六中基本方式实现
java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...
- Maven,预加载资源文件
预加载资源文件需要先启用功能: <build> <resources> <resource> <directory>src/main/resources ...
- js 动态加载事件的几种方法总结
本篇文章主要是对js 动态加载事件的几种方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 有些时候需要动态加载javascript事件的一些方法往往我们需要在 JS 中动态添 ...
- 动态加载资源文件(ResourceDictionary)
原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...
- Spring boot 国际化自动加载资源文件问题
Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...
随机推荐
- Retrofit结合RxJava使用指南
Retrofit结合RxJava使用指南 Retrofit是一个当前很流行的网络请求库, 官网的介绍是: "Type-safe HTTP client for Android and Jav ...
- Android Weekly Notes Issue #219
Android Weekly Issue #219 August 21st, 2016 Android Weekly Issue #219 ARTICLES & TUTORIALS Andro ...
- 百度地图隐藏BMKAnnotationView
BMKAnnotationview.hidden 失效,只能移除Annotation BMKPinAnnotationView设置setHidden为yes时不能隐藏
- UIBezierPath-完善曲线
override func draw(_ rect: CGRect) { let path = UIBezierPath() // 起点 path.move(to: CGPoint(x: , y: ) ...
- Socket--Android王国的外交发言人
Socket:原意"插座",在Java语言中为"套接字" 用于描述IP地址和端口号,是通信链的句柄,我们可以通过它向网络发送请求或者应答网络请求; 它是支持TC ...
- git与github安装、配置、pull、push
操作系统是Ubuntu 16.04 LTS 64bit 1 安装git (1)安装 sudo apt-get install git-core (2)一些全局变量的初始化 在本地建立一个文件夹,然后做 ...
- [Erlang 0124] Erlang Unicode 两三事 - 补遗
最近看了Erlang User Conference 2013上patrik分享的BRING UNICODE TO ERLANG!视频,这个分享很好的梳理了Erlang Unicode相关的问题,基本 ...
- ASP.NET MVC 身份认证
身份认证的好处就是, 如果这个页面没有登录, 刷新后会自动跳到登录页要求登录,保证了应用程序的安全.而Forms 身份认证是web下最常用的,如何配置呢?见下(基于mvc 4) 1.在webconfi ...
- Memcached初探
一.Memcached是什么 Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度. Memcached基于 ...
- Oracle学习笔记十四 内置程序包
扩展数据库的功能 为 PL/SQL 提供对 SQL 功能的访问 用户 SYS 拥有所有程序包 是公有同义词 可以由任何用户访问 一些内置程序包 程序包名称 说明 STANDARD和DBMS_STAND ...