在多模块项目中读取属性文件 [英] read properties file in multi module project

查看:133
本文介绍了在多模块项目中读取属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,该项目包含两个具有以下结构的模块

Hi I have a project which has two modules with the following structure

project
│ └───Module1 │ |---abc.jsp │
│ ├───Module2 │----src.main. | |---java. | |---com.xyz.comp | │----------Action.java |
| └───resources |---com.xyz.comp │ prop.properties

project
│ └───Module1 │ |---abc.jsp │
│ ├───Module2 │----src.main. | |---java. | |---com.xyz.comp | │----------Action.java |
| └───resources |---com.xyz.comp │ prop.properties

现在,我的Module1依赖于Module2的war(Module2是独立的war文件).我的问题是Module1的abc.jsp提交给Module2的Action.java.在我尝试访问prop.properties时,其中给出了空指针异常.

Now My Module1 has a dependency on war of Module2(Module2 is an independent war file). My problem is that the abc.jsp of Module1 submits to the Action.java of Module2. In which when I try to access prop.properties gives null pointer exception.

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始终为null,因为它找不到文件.我输入的路径错误还是因为从另一个模块调用了该路径,所以类加载器无法访问该模块文件?

The propStream is always null because it cannot find the file. Am i giving the path wrong or since this is called from another module the classloader does not have access to this module files?

推荐答案

问题是资源文件(通常放在src/main/resources中的文件)最终出现在war文件的WEB-INF/classes子目录中.

The problem is that resources files (the ones you normally put in src/main/resources) wind up in the WEB-INF/classes subdirectory of the war-file.

现在,如果您尝试将propfilename设置为:

Now, if you try to set your propfilename to:

String propfilename = "WEB-INF/classes/com/xyz/comp/prop.properties" 

它仍然可靠地仍然无法正常工作(想到JWS),因为您正在使用Action类中的类加载器,而该类加载器在您尝试读取的jar/war中不存在

it will still not work reliably (JWS comes to mind) because you are using the classloader from the Action class, which is not present in the jar/war you are trying to read from.

执行此操作的正确方法是引入第三个模块/依赖项 您放置共享资源并让其他模块依赖的地方

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)和其他使用类似类加载策略的框架,可以使用锚点"方法.

For JWS (Java Web Start) and other frameworks that use similar classloading strategies, you can use the "Anchor" approach.

用于类加载的锚定方法

由于要掌握给定类的类加载器通常需要您事先加载该类,因此一个技巧是将一个哑类放入仅包含资源(例如properties文件)的jar中.假设您在jar文件中具有以下结构:

Since getting hold of the classloader for a given class usually requires you to have loaded the class beforehand, a trick is to put a dummy class in a jar that only contains resources, such as properties files. Let's say you have this structure in a jar-file:

org/example/properties/a.properties
org/example/properties/b.properties
org/example/properties/c.properties

只需在jar中放入一个虚拟类,使其看起来像这样:

Just throw in a dummy class in the jar, making it look like this:

org/example/properties/Anchor.class
org/example/properties/a.properties
org/example/properties/b.properties
org/example/properties/c.properties

然后,通过其他代码,您现在可以执行此操作,并确保类加载按预期进行:

then, from other code you can now do this and be sure that classloading works as expected:

Properties properties = new Properties();
String propFile = "org/example/properties/a.properties";

ClassLoader classLoader = Anchor.class.getClassLoader();
InputStream propStream = classLoader.getResourceAsStream(propFile );

properties.load(propStream);

这是一种更强大的方法.

This is a more robust way of doing it.

这篇关于在多模块项目中读取属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆