wildfly:从配置目录中读取属性

Eas*_*her 13 java jboss java-ee cdi 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
    }
  }
Run Code Online (Sandbox Code Playgroud)

但显然这不起作用,因为配置文件夹不再在类路径中.现在我找不到一个简单的方法来做到这一点.我最喜欢的是这样的:

@InjectProperties("my.properties")
protected Properties props;
Run Code Online (Sandbox Code Playgroud)

到目前为止,我在网上找到的唯一解决方案是制作我自己的OSGi模块,但我相信必须有一种更简单的方法(没有OSGi!).谁能告诉我怎么样?

Joh*_*ent 33

如果要显式读取配置目录中的文件(例如$WILDFLY_HOME/standalone/configurationdomain/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);
}
Run Code Online (Sandbox Code Playgroud)

然后将为您加载该文件.

此外,由于WildFly不再提供OSGi支持,我不知道如何创建OSGi模块对您有所帮助.

  • 注意 jboss.server.config.dir 与 jboss.domain.config.dir 不同。如果您的应用程序同时部署到独立环境和域环境,则需要进行一些调整。 (2认同)

Chr*_*hie 7

以下是仅使用CDI的完整示例,取自此站点.

  1. 在WildFly配置文件夹中创建并填充属性文件

    $ echo 'docs.dir=/var/documents' >> .standalone/configuration/application.properties
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将系统属性添加到WildFly配置文件.

    $ ./bin/jboss-cli.sh --connect
    [standalone@localhost:9990 /] /system-property=application.properties:add(value=${jboss.server.config.dir}/application.properties)
    
    Run Code Online (Sandbox Code Playgroud)

这会将以下内容添加到服务器配置文件(standalone.xml或domain.xml):

<system-properties>
    <property name="application.properties" value="${jboss.server.config.dir}/application.properties"/>
</system-properties>
Run Code Online (Sandbox Code Playgroud)
  1. 创建用于加载和存储应用程序范围属性的单例会话bean

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    
    import javax.annotation.PostConstruct;
    import javax.ejb.Singleton;
    
    @Singleton
    public class PropertyFileResolver {
    
        private Logger logger = Logger.getLogger(PropertyFileResolver.class);
        private String properties = new HashMap<>();
    
        @PostConstruct
        private void init() throws IOException {
    
            //matches the property name as defined in the system-properties element in WildFly
            String propertyFile = System.getProperty("application.properties");
            File file = new File(propertyFile);
            Properties properties = new Properties();
    
            try {
                properties.load(new FileInputStream(file));
            } catch (IOException e) {
                logger.error("Unable to load properties file", e);
            }
    
            HashMap hashMap = new HashMap<>(properties);
            this.properties.putAll(hashMap);
        }
    
        public String getProperty(String key) {
            return properties.get(key);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建CDI限定符.我们将在我们希望注入的Java变量上使用此注释.

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    import javax.inject.Qualifier;
    
    @Qualifier
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR })
    public @interface ApplicationProperty {
    
        // no default meaning a value is mandatory
        @Nonbinding
        String name();
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建生产者方法; 这会生成要注入的对象

    import javax.enterprise.inject.Produces;
    import javax.enterprise.inject.spi.InjectionPoint;
    import javax.inject.Inject;
    
    public class ApplicaitonPropertyProducer {
    
        @Inject
        private PropertyFileResolver fileResolver;
    
        @Produces
        @ApplicationProperty(name = "")
        public String getPropertyAsString(InjectionPoint injectionPoint) {
    
            String propertyName = injectionPoint.getAnnotated().getAnnotation(ApplicationProperty.class).name();
            String value = fileResolver.getProperty(propertyName);
    
            if (value == null || propertyName.trim().length() == 0) {
                throw new IllegalArgumentException("No property found with name " + value);
            }
            return value;
        }
    
        @Produces
        @ApplicationProperty(name="")
        public Integer getPropertyAsInteger(InjectionPoint injectionPoint) {
    
            String value = getPropertyAsString(injectionPoint);
            return value == null ? null : Integer.valueOf(value);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 最后将属性注入您的一个CDI bean

    import javax.ejb.Stateless;
    import javax.inject.Inject;
    
    @Stateless
    public class MySimpleEJB {
    
        @Inject
        @ApplicationProperty(name = "docs.dir")
        private String myProperty;
    
        public String getProperty() {
            return myProperty;
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)


Har*_*ann 6

您可以做的最简单的操作是standalone.sh使用一个-P引用属性文件的选项来运行(您需要一个URL file:/path/to/my.properties或将该文件放入$WILDFLY_HOME/bin)。

然后,文件中的所有属性将作为系统属性加载。

要将配置属性注入到您的应用程序类中,请查看DeltaSpike Configuration,它支持不同的属性源,例如系统属性,环境变量,JNDI条目,并从您的应用程序中隐藏特定的源。

或者,为避免设置系统属性(从对部署到WildFly实例的所有应用程序可见的角度来看,这是全局的),还可以为DeltaSpike定义自定义属性源,以从任何给定位置读取属性文件,以及这些属性将在您的应用程序本地。