关于war包里取properties文件

最近遇到web module里面取properties文件取不到。用的是比较常用的
ClassLoader.getSystemResource 方法。
后来google发现Class.getClassLoader().getResourceAsStream()可以用。两者略有区别,google结果如下

ClassLoader.getSystemResourceAsStream uses the system ClassLoader (ClassLoader.getSystemClassLoader) to search for the resource.
this.getClass().getClassLoader().getResourceAsStream() uses the same ClassLoader that loaded the class for "this" Object. That ClassLoader might be a child of the system ClassLoader, and thus could have access to resources the system ClassLoader does not have. But since it is a child of the System ClassLoader, it also will ask its parent (System) to help it find resources. So using this method will allow you to find things in that ClassLoader AND in the System ClassLoader.
The search order is defined in the javadoc for ClassLoader.getResource (parent(s) first, then child).
For example, say you were running some code from a Servlet. The System ClassLoader contains whatever you put in CLASSPATH when you started the Web Server, but the servlet class is probably loaded by another ClassLoader that the server created to handle the WebApp's WEB-INF/lib and WEB-INF/classes directories. Things in those directories would be accessible using that WebApp's ClassLoader, but would not be in CLASSPATH - so the System ClassLoader does not know about them.

你可能感兴趣的:(properties)