关于class.getResourceAsStream(String name)与
class.getClassLoader().getResourceAsStream(String name)的路径name问题
首先用类加载资源文件的方式可以有以下三种:
(包结构图)
1) InputStream inStream = DaoFactory.class.getResourceAsStream("dao.properties");
2) inStream=DaoFactory.class.getResourceAsStream("/com/jdbc/dao/dao.properties")
3) inStream=DaoFactory.class.getClassLoader().getResourceAsStream("com/jdbc/dao/dao.properties");
第一种和第二种方式是采用Class对象去加载,第三种采用ClassLoader对象去加载资源文件,之所以Class对象也可以加载资源文件是因为Class类封装的ClassLoader的getResourceAsStream方法。从Class类中的源码中可以看出:
之所以这样做无疑还是方便客户端的调用,省的每次获取ClassLoader才能加载资源文件的麻烦。
路径问题:
1. 第三种是最本质的做法,前两种也是基于第三种去实现的。JVM会使用Bootstrap Loader去加载资源文件。所以路径还是这种相对于工程的根目录即"com/jdbc/dao/dao.properties"(不需要“/”)。
2. 第一种是采用的相对路径,资源文件相对于当前这个类即(DaoFactory类)的位置,这种方式Class对象做了一些处理。即resloveName(name)这个方法。最终name还是会转换成适合第三方式的name参数
3. 第二种是采用绝对路径,绝对路径是相对于classpath根目录的路径(工程里src目录对应bin目录(存放.class文件的目录))。
(Class类中resolveName方法)
- 大小: 29 KB
- 大小: 22.9 KB
- 大小: 10.4 KB
分享到:
相关推荐
12 InputStream getResourceAsStream(String path) 返回指定资源的输入流 13 RequestDispatcher getRequestDispatcher(String uripath) 返回指定资源的RequestDispatcher对象 14 Servlet getServlet(String ...
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m....
String poolName = name.substring(0, name.lastIndexOf(".")); String url = props.getProperty(poolName + ".url"); if (url == null) { log("没有为连接池" + poolName + "指定URL"); continue; } String ...
* result中要指定关联关系<param name="inputName">inputStream * */ System.out.println("download action is invoked!!"); String realPath=ServletActionContext.getServletContext().getRealPath("/"+...
Document document = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName)); Element root = document.getRootElement(); List list = XPath.selectNodes(root, "/beans/bean"); for...
RepositoryService.getResourceAsStream(java.lang.String deploymentId, java.lang.String resourceName) * resourceName 可以通过 deploymentId 动态获得 RepositoryService repositoryService = ...
.getResourceAsStream(DBINFO_FIIE_NAME); try { infos.load(is); } catch (Exception e) { e.printStackTrace(); throw new ExceptionInInitializerError("属性文件加载错误"); } finally { ...
The default behavior of this method is to return addHeader(String name, String value) on the wrapped response object. addHeader(String, String) - Method in interface javax.servlet....
InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);XML 配置文件(configuration XML)中包含了对 ...
prop.load(DButil.class.getResourceAsStream("/db_config.properties")); info = new DButil(); info.driver = prop.getProperty("driver"); info.dbName = prop.getProperty("dbName"); info.host = prop....
} /** * 进行用户身份验证时,设置用户名和密码 */ public void setNamePass(String name, String pass) { sendUserName = name; sendUserPass = pass; } /** * 设置邮件主题 * @param mailSubject * @return */ ...
String userDaoClass = prop.getProperty("userDaoClass"); Class clazz = Class.forName(userDaoClass); userDao = (UserDao) clazz.newInstance(); } catch (Throwable e) { throw new ...
模板定义在类路径下新建一个jxl-excel.xml的模板配置文件,输入如下的配置文件内容,即可定义一个excel模板。<?xml version="1.0" encoding="UTF-8" ?> name="testRead"> <titleRow> <...
String[] mapName=new String[]{"牛家村","冰火岛 ","大智岛","襄阳城","蓬莱岛","竞技场"}; boolean updateMan; static Graphics g1; Random rand; Player mapPlayer,attPlayer; boolean isWeather; int ...