读取多模块项目中的属性文件
read properties file in multi module project
你好,我有一个项目有两个模块,结构如下
项目<br>
│
└────模块一
│ |---abc.jsp
│<br>
│
├────模块二
│----src.main。
|
|---java。
| |---com.xyz.comp
| │------------Action.java
|<br>
|
└────资源
|---com.xyz.comp
│ prop.properties
现在我的 Module1 依赖于 Module2 的 war(Module2 是一个独立的 war 文件)。我的问题是 Module1 的 abc.jsp 提交给 Module2 的 Action.java。其中当我尝试访问 prop.properties 时给出空指针异常。
public static void test(){
Properties properties = new Properties();
String propfilename = "com/xyz/comp/prop.properties";
try {
ClassLoader contextClassLoader = Action.class.getClassLoader();
InputStream prpoStream= contextClassLoader.getResourceAsStream(propfilename );
properties.load(propertiesStream);
// bunch of other code
} catch (Exception e) {
}
}
propStream 始终为空,因为它找不到文件。我给的路径是错误的还是因为这是从另一个模块调用的类加载器无法访问该模块文件?
问题是资源文件(您通常放在 src/main/resources
中的那些)最终进入 war 文件的 WEB-INF/classes
子目录。
现在,如果您尝试将 propfilename
设置为:
String propfilename = "WEB-INF/classes/com/xyz/comp/prop.properties"
它仍然无法可靠地工作(想到 JWS),因为您正在使用 Action
class 中的 classloader ,它不存在于您尝试读取的 jar/war 中。
The proper way of doing this is to introduce a third module/dependency
where you put your shared resources and have the other modules depend
on that.
对于 JWS(Java Web Start)和其他使用类似 class 加载策略的框架,您可以使用 "Anchor" 方法。
class加载的锚方法
由于获取给定 class 的 class 加载程序通常需要您事先加载 class,一个技巧是放置一个虚拟 class在只包含资源的 jar 中,例如 properties
文件。假设您在 jar 文件中具有此结构:
org/example/properties/a.properties
org/example/properties/b.properties
org/example/properties/c.properties
只需在罐子中放入一个假人 class,使其看起来像这样:
org/example/properties/Anchor.class
org/example/properties/a.properties
org/example/properties/b.properties
org/example/properties/c.properties
然后,您现在可以从其他代码执行此操作并确保class加载按预期工作:
Properties properties = new Properties();
String propFile = "org/example/properties/a.properties";
ClassLoader classLoader = Anchor.class.getClassLoader();
InputStream propStream = classLoader.getResourceAsStream(propFile );
properties.load(propStream);
这是一种更可靠的方法。
试试这个:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
你好,我有一个项目有两个模块,结构如下
项目<br>
│
└────模块一
│ |---abc.jsp
│<br>
│
├────模块二
│----src.main。
|
|---java。
| |---com.xyz.comp
| │------------Action.java
|<br>
|
└────资源
|---com.xyz.comp
│ prop.properties
现在我的 Module1 依赖于 Module2 的 war(Module2 是一个独立的 war 文件)。我的问题是 Module1 的 abc.jsp 提交给 Module2 的 Action.java。其中当我尝试访问 prop.properties 时给出空指针异常。
public static void test(){
Properties properties = new Properties();
String propfilename = "com/xyz/comp/prop.properties";
try {
ClassLoader contextClassLoader = Action.class.getClassLoader();
InputStream prpoStream= contextClassLoader.getResourceAsStream(propfilename );
properties.load(propertiesStream);
// bunch of other code
} catch (Exception e) {
}
}
propStream 始终为空,因为它找不到文件。我给的路径是错误的还是因为这是从另一个模块调用的类加载器无法访问该模块文件?
问题是资源文件(您通常放在 src/main/resources
中的那些)最终进入 war 文件的 WEB-INF/classes
子目录。
现在,如果您尝试将 propfilename
设置为:
String propfilename = "WEB-INF/classes/com/xyz/comp/prop.properties"
它仍然无法可靠地工作(想到 JWS),因为您正在使用 Action
class 中的 classloader ,它不存在于您尝试读取的 jar/war 中。
The proper way of doing this is to introduce a third module/dependency where you put your shared resources and have the other modules depend on that.
对于 JWS(Java Web Start)和其他使用类似 class 加载策略的框架,您可以使用 "Anchor" 方法。
class加载的锚方法
由于获取给定 class 的 class 加载程序通常需要您事先加载 class,一个技巧是放置一个虚拟 class在只包含资源的 jar 中,例如 properties
文件。假设您在 jar 文件中具有此结构:
org/example/properties/a.properties
org/example/properties/b.properties
org/example/properties/c.properties
只需在罐子中放入一个假人 class,使其看起来像这样:
org/example/properties/Anchor.class
org/example/properties/a.properties
org/example/properties/b.properties
org/example/properties/c.properties
然后,您现在可以从其他代码执行此操作并确保class加载按预期工作:
Properties properties = new Properties();
String propFile = "org/example/properties/a.properties";
ClassLoader classLoader = Anchor.class.getClassLoader();
InputStream propStream = classLoader.getResourceAsStream(propFile );
properties.load(propStream);
这是一种更可靠的方法。
试试这个:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();