Java_web项目中在Java文件里面通过类装载器对资源文件读取

承接上一节:在eclipse完成对Java_web项目里面资源文件的读取 

我们首先在src目录下创建一个资源文件db.properties

内容如下:

url=127.0.0.1
name=root
password=root

之后我们创建一个继承于HttpServlet的Java文件

package test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletContextDemo2
 */
@WebServlet("/ServletContextDemo2")    //注意有了这个就不需要往web.xml文件里面添加路径映射
public class ServletContextDemo2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        UserDo user = new UserDo(); 
        user.update();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

UserDo类:

package test;

import java.io.InputStream;
import java.util.Properties;

public class UserDo {
    private static Properties dbconfig = new Properties();
    //采用静态代码块的方式加载配置文件信息。配置文件信息加载一次就可以了,所以使用静态代码块
    static {
        //使用类装载器来实现
        //类装载器会把src的所有Java文件都加载,那么src的资源文件也会被加载,所以就可以通过这种方式
        //来获取文件内容。又因为db.properties在src目录下,所以路径直接写文件名就可以了
        try {
        InputStream in = UserDo.class.getClassLoader().getResourceAsStream("db.properties");
        dbconfig.load(in);
        }catch(Exception e) {  //向上抛出异常
            throw new ExceptionInInitializerError(e);
        }
    }
    public void update() {
        // TODO Auto-generated method stub
        System.out.println(dbconfig.getProperty("url"));
    }
    
}

这样就可以在Java文件内读取资源文件信息。但是要注意类装载器只会执行一次,什么意思呢。

如果你运行服务器之后,这个时候资源文件已经被类装载器装载,尽管你改变了db.properties文件内容,但是只要你不重启服务器,那么通过类装载器访问到的db.properties文件内容还是之前的,并没有更新。

如果你想要实时获取资源文件内容,代码如下:

只需要修改UserDo类代码:

package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class UserDo {
    public void update() throws IOException {
        // TODO Auto-generated method stub
        Properties dbconfig = new Properties();
        String path = UserDo.class.getClassLoader().getResource("db.properties").getPath();
        FileInputStream in = new FileInputStream(path);
        dbconfig.load(in);
        System.out.println(dbconfig.getProperty("url"));
    }
    
}
原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/14089264.html