wildfly:从配置目录读取属性
问题内容:
我正在尝试从我的wildfly配置文件夹中的属性文件中读取特定于部署的信息。我尝试了这个:
@Singleton
@Startup
public class DeploymentConfiguration {
protected Properties props;
@PostConstruct
public void readConfig() {
props = new Properties();
try {
props.load(getClass().getClassLoader().getResourceAsStream("my.properties"));
} catch (IOException e) {
// ... whatever
}
}
但是显然这不起作用,因为配置文件夹不再位于类路径中。现在我找不到一种简单的方法。我最喜欢的是这样的:
@InjectProperties("my.properties")
protected Properties props;
到目前为止,我在网上找到的唯一解决方案是制作自己的OSGi模块,但是我相信必须有一种更简单的方法来实现(一个没有OSGi!)。谁能告诉我如何?
问题答案:
如果要从配置目录(例如$WILDFLY_HOME/standalone/configuration
或domain/configuration
)中显式读取文件,则有一个系统属性,其中包含路径。只需System.getProperty("jboss.server.config.dir");
添加您的文件名并将其附加到该文件名即可。
但是,您不会将其作为资源阅读,所以…
String fileName = System.getProperty("jboss.server.config.dir") + "/my.properties";
try(FileInputStream fis = new FileInputStream(fileName)) {
properties.load(fis);
}
然后将为您加载文件。
另外,由于WildFly不再提供OSGi支持,因此我不知道创建OSGi模块将如何为您提供帮助。