Properties JBoss AS 7特定于应用程序的属性文件

Properties JBoss AS 7特定于应用程序的属性文件,properties,jboss7.x,Properties,Jboss7.x,我有几个独立的javaee模块(warweb应用程序和jarejb模块),我将它们部署在jboss7.1.1as上。 我想: 将这些模块的配置集中在一个*.properties文件中 使此文件在类路径中可用 使此文件的安装/配置尽可能简单。理想情况下,只需将其放在某个JBoss文件夹中,如:${JBoss_HOME}/standalone/configuration 无需重新启动应用程序服务器即可对此文件进行更改 这可能吗 我已经找到了这个链接:,它解释了更好的方法是创建静态JBoss模块。但是

我有几个独立的javaee模块(warweb应用程序和jarejb模块),我将它们部署在jboss7.1.1as上。 我想:

  • 将这些模块的配置集中在一个*.properties文件中
  • 使此文件在类路径中可用
  • 使此文件的安装/配置尽可能简单。理想情况下,只需将其放在某个JBoss文件夹中,如:${JBoss_HOME}/standalone/configuration
  • 无需重新启动应用程序服务器即可对此文件进行更改
  • 这可能吗


    我已经找到了这个链接:,它解释了更好的方法是创建静态JBoss模块。但是,在我部署的每个应用程序模块中,我都必须依赖于这个静态模块,这是我试图避免的一种耦合。

    也许一个简单的解决方案是从单例或静态类读取文件

    private static final String CONFIG_DIR_PROPERTY = "jboss.server.config.dir"; 
    
    private static final String PROPERTIES_FILE = "application-xxx.properties";
    
    private static final Properties PROPERTIES = new Properties();
    
    static {
        String path = System.getProperty(CONFIG_DIR_PROPERTY) + File.separator + PROPERTIES_FILE;  
        try {  
            PROPERTIES.load(new FileInputStream(path));
        } catch (MalformedURLException e) {
           //TODO 
        } catch (IOException e) {
           //TODO
        } 
    }
    

    下面是一个仅使用CDI的完整示例,摘自本文

    这种配置也适用于JBossAS7

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

    $ echo 'docs.dir=/var/documents' >> .standalone/configuration/application.properties
    
  • 将系统属性添加到WildFly配置文件

    $ ./bin/jboss-cli.sh --connect
    [standalone@localhost:9990 /] /system-property=application.properties:add(value=${jboss.server.config.dir}/application.properties)
    
  • 这将向服务器配置文件(standalone.xml或domain.xml)添加以下内容:

  • 创建生产者方法;这将生成要注入的对象

    import javax.enterprise.inject.Produces;
    import javax.enterprise.inject.spi.InjectionPoint;
    import javax.inject.Inject;
    
    public class ApplicationPropertyProducer {
    
        @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);
        }
    }
    
  • 最后,将属性注入一个CDIBean

    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;
        }
    }
    
  • 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();
    }
    
    import javax.enterprise.inject.Produces;
    import javax.enterprise.inject.spi.InjectionPoint;
    import javax.inject.Inject;
    
    public class ApplicationPropertyProducer {
    
        @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);
        }
    }
    
    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;
        }
    }