Java项目读取resources资源文件路径那点事

  今天在Java程序中读取resources资源下的文件,因为对Java结构了解不透彻,遇到不少坑。正常在Java工程中读取某路径下的文件时,能够采用绝对路径和相对路径,绝对路径没什么好说的,相对路径,即相对于当前类的路径。在本地工程和服务器中读取文件的方式有所不一样,如下图配置文件为例:java

 

(1)本地读取资源文件

  Java类中须要读取properties中的配置文件,能够采用文件(File)方式进行读取:web

File file = new File("src/main/resources/properties/test.properties");
InputStream in = new FileInputStream(file);

  注意:当在IDEA中运行(不部署在服务器上),能够读取到该文件;spring

  缘由:JavaWeb项目部署服务器中,会将项目打包成Jar包或者war包,此时就不会存在 src/main/resources 目录,JVM会在编译项目时,主动将 java文件编译成 class文件 和 resources 下的静态文件放在 target/classes目录下;服务器

  理解:Java文件只有编译成 class文件才会被JVM执行,本地执行时会,当前项目即为Java进程的工做空间,虽然class文件在target/classes目录下,可是target/classes不是class文件运行的目录,只是存放的目录,运行目录仍是在IDEA的模块下,因此运行时会找到 src/main/resources 资源文件!this

(2)服务器(Tomcat)读取资源文件

  当工程部署到Tomcat中时,按照上边方式,则会抛出异常:FileNotFoundException。缘由:Java工程打包部署到Tomcat中时,properties的路径变到顶层(classes下),这是由Maven工程结构决定的。由Maven构建的web工程,主代码放在src/main/java路径下,资源放在src/main/resources路径下,当构建jar包 或 war包时,JVM虚拟机会自动编译java文件为class文件存放在 target/classes目录下,resource资源下的文件会原封不动的拷贝一份到 target/classes 目录下:spa

  

  方式一:此时读取资源文件时,采用流(Stream)的方式读取,并经过JDK中Properties类加载,能够方便的获取到配置文件中的信息:指针

InputStream in = this.getClass().getResourceAsStream("/properties/test.properties");
Properties properties = new Properties();
properties.load(in);
properties.getProperty("name");

重点理解:class.getResourceAStream() 与 class.getClassLoader().getResorceAsStream() 的区别code

  

1) InputStream inStream = PropertiesTest.class.getResourceAsStream("test.properties"); 

​2) inStream = PropertiesTest.class.getResourceAsStream("/com/test/demo/test.properties") 

3) inStream = PropertiesTest.class.getClassLoader().getResourceAsStream("com/test/demo/test.properties");

 1)第一种和第二种方式采用 Class 对象去加载,第三种方式采用 ClassLoader 对象去加载资源文件,之因此 Class 能够加载资源文件,是由于 Class 类封装的 ClassLoader 的 getResourceAsStream() 方法,从 Class 类中的源码能够看出:component

public InputStream getResourceAsStream(String name) {
        name = resolveName(name);
        ClassLoader cl = getClassLoader0();
        if (cl==null) {
            // A system class.
            return ClassLoader.getSystemResourceAsStream(name);
        }
        return cl.getResourceAsStream(name);
 }

 理由:​​之因此这样作无疑仍是方便客户端的调用,省的每次获取ClassLoader才能加载资源文件的麻烦!server

 2).class 是获取当前类的 class 对象,getClassLoader()是获取当前的类加载器,什么是类加载器?简单点说,就是用来加载java类的,类加载器就是负责把class文件加载进内存中,并建立一个java.lang.Class类的一个实例,也就是class对象,而且每一个类的类加载器都不相同,getResourceAsStream(path)是用来获取资源的,由于这是ClassLoader(类加载器)获取资源,而类加载器默认是从 classPath 下获取资源的,由于这下面有class文件。因此这段代码总的意思是经过类加载器在 classPath 目录下获取资源,而且是以流的形式。咱们知道在Java中全部的类都是经过加载器加载到虚拟机中的,并且类加载器之间存在父子关系,就是子知道父,父不知道子,这样不一样的子加载的类型之间是没法访问的(虽然它们都被放在方法区中),因此在这里经过当前类的加载器来加载资源也就是保证是和类类型是同一个加载器加载的。 

(3)class.getClassLoader().getResourceAsStream() 和 class.getResouceAsStream() 的区别

  a)class.getClassLoader().getResourceAsStream(String name)  默认从classpath中找文件(文件放在resources目录下),name不能带"/",不然会抛空指针。采用相对路径, "/"就至关于当前进程的根目录,即项目根目录;

inStream = PropertiesTest.class.getClassLoader().getResourceAsStream("com/test/demo/test.properties");

      b)class.getResourceAsStream(String name) 是采用绝对路径,绝对路径是相对于 classpath 根目录的路径,"/" 就表明着 classpath,因此 name 属性须要前面加上 "/";

 

inStream = PropertiesTest.class.getResourceAsStream("/com/test/demo/test.properties")

  方式二:采用Spring注解

  若是工程中使用Spring,能够经过注解的方式获取配置信息,但须要将配置文件放到Spring配置文件中扫描后,才能将配置信息放入上下文。

<context:component-scan base-package="com.xxxx.service"/>
 <context:property-placeholder location="classpath:properties/xxx.properties" ignore-unresolvable="true"/>

  而后在程序中能够使用 @Value进行获取properties文件中的属性值,以下:

@Value("${xxxt.server}")
 private static String serverUrl;

  方式三:采用Spring配置

  也能够在Spring配置文件中读取属性值,赋予类成员变量

<?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
      
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="location" value="classpath:properties/xxx.properties"/>
      </bean>
     
     <bean id="service" class="com.xxxx.service.ServiceImpl">
<property name="serverUrl" value="${xxxt.server}" /> </bean> </beans>

  天天进步一点点,继续前进......

相关文章
相关标签/搜索