从配置目录读取属性

从配置目录读取属性

本文介绍了Wildfly:从配置目录读取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 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/configurationdomain/配置)有一个带有路径的系统属性.只需执行 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:从配置目录读取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:05