wildfly:从配置目录中读取属性 [英] wildfly: reading properties from configuration directory
问题描述
我正在尝试从wildfly配置文件夹中的属性文件中读取部署特定信息。我试过这个:
I'm trying to read deployment specific information from a properties file in my wildfly configuration folder. I tried this:
@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
}
}
但显然这不起作用,因为配置文件夹不再在类路径中。现在我找不到一个简单的方法来做到这一点。我最喜欢的是这样的:
But apparently this is not working since the configuration folder is not in the classpath anymore. Now I can't find an easy way to do it. My favorite would be something like this:
@InjectProperties("my.properties")
protected Properties props;
到目前为止,我在网上找到的唯一解决方案是制作我自己的OSGi模块,但我相信那里必须是一种更简单的方法(一个没有OSGi!)。谁有人可以告诉我怎么做?
The only solution I found on the web so far involves making my own OSGi module, but I believe there must be an easier way to do it (one without OSGi!). Can anyone show me how?
推荐答案
如果你想从配置目录中显式读取文件(例如 $ WILDFLY_HOME / standalone / configuration
或 domain / configuration
)系统属性中包含路径。只需执行 System.getProperty(jboss.server.config.dir);
并将您的文件名附加到该文件名即可获取该文件。
If you want to explicitly read a file from the configuration directory (e.g. $WILDFLY_HOME/standalone/configuration
or domain/configuration
) there's a system property with the path in it. Simply do System.getProperty("jboss.server.config.dir");
and append your file name to that to get the file.
你不会把它读作资源,所以...
You wouldn't read it as a resource though, so...
String fileName = System.getProperty("jboss.server.config.dir") + "/my.properties";
try(FileInputStream fis = new FileInputStream(fileName)) {
properties.load(fis);
}
然后会为你加载文件。
此外,由于WildFly不再提供OSGi支持,我不知道如何创建OSGi模块可以帮助你。
Also, since WildFly doesn't ship with OSGi support anymore, I don't know how creating an OSGi module would help you here.
这篇关于wildfly:从配置目录中读取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!