配置文件,资源文件的获取方式

来源:互联网 发布:中国少年编程网 编辑:程序博客网 时间:2024/05/29 15:26

web工程中读取资源文件:

servletContext读取web应用中的资源文件。

------------------------------

InputStream  is=this.getServletContex().getResourceAsStream("/WEB_INF/classes/XXXX.properties");

 

InputStream  is=this.getServletContex().getRealPath("/WEB_INF/classes/XXXX.properties");

 

FileInputStream fis =new FileInputStream(path);//此处path指的是相对路径,web应用中,相对java虚拟机。

----------------------------

如果读取资源文件的程序不是servlet ,应该用classload来加载,如java程序中获取资源文件。

通过类装载的方式读取资源文件

注意:

通过类装载的方式读取资源文件的内容,如果动态的更改资源文件的内容不会立即生效(类加载器在成功加载某个类之后,会把得到的 java.lang.Class类的实例缓存起来。下次再请求加载该类的时候,类加载器会直接使用缓存的类的实例,而不会尝试再次加载)。

如果要求立即生效,可以通过类装载的方式得到资源文件的位置,之后再通过操作文件的方式获得输入流。

 String path=obj.class.getClassLoader.getResource("XXXX.properties").getPath();

FileInputStream fis =new FileInputStream(path);

 props.load(is);

 

----------------------------classload方式------------------

Properties props = new Properties();

InputStream  is=obj.class.getClassLoader.getResourceAsStream("XXXX.properties");

 props.load(is);

 String dbSourceName = props.getProperty("DataSource");

这种方式通过类装载器将资源文件加载到内存中,之后再读取。如果文件太大会影响性能。

------------------------------------------------------------------

 

 

下面是之前看别人在广西联通项目中通过资源文件定义数据源,并读取资源文件的方式,存在一些不足:数据源配置信息与web层无关,不应使用ServletContextListener类型的实例对象(关联ServletContextEvent)方法getWebClassRootPath()来获取资源文件的路径。

 

 

----写完整路径:

    public Properties loadProperties(String propsfile) throws Exception {     
   
  Properties props = new Properties();
  try {
   FileInputStream stream;
   
   String path = WebVarLoader.getWebClassRootPath();
   try {
    stream = new FileInputStream(new File(path + propsfile));
   } catch (Exception e) {
    e.printStackTrace();
    stream = new FileInputStream(new File(propsfile));
   }
   props.load(stream);
  } catch (Exception e) {
   e.printStackTrace();
  }

  return props;
    }

 

    public Connection getConnection(String propsFile) throws Exception {
        if (propsFile == null || propsFile.length() == 0) {
            // propsFile = Global.jndiFile;
            System.out.println("propsFile is blank");
        }
        Properties props = loadProperties(propsFile);
        String dbSourceName = props.getProperty("DataSource");
        Context ctx = new InitialContext(props);
        javax.sql.DataSource dbSource = (javax.sql.DataSource) ctx.lookup(dbSourceName);
        if (dbSource == null) {
            throw new Exception("Cann't find the datasource " + dbSourceName);
        }
        return dbSource.getConnection();
    }

 

package bss.common.base.tools;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class WebVarLoader implements ServletContextListener {

 
 private static String path = "";

 
 private static String webClassRootPath = "";

 
 private static final String FILE_SEP = (String) System.getProperty("file.separator");

 
 public void contextDestroyed(ServletContextEvent arg0) {
  // TODO Auto-generated method stub
 }

 
 public void contextInitialized(ServletContextEvent event) {
  path = event.getServletContext().getRealPath("/");
  webClassRootPath = path + FILE_SEP + "WEB-INF" + FILE_SEP + "classes" + FILE_SEP;
  
  //FIXME delete below 2
  System.out.println("[DEBUG] path : "+path);
  System.out.println("[DEBUG] web_class_rootpath : "+ webClassRootPath);
 }

 
 public static String getWebClassRootPath() {
  return webClassRootPath;
 }

}

原创粉丝点击