2013-03-12 78 views
7

我在JBoss 7.1.1 AS上部署了几个独立的Java EE模块(WAR Web应用程序和JAR EJB模块)。 我想:JBoss AS 7特定于应用程序的属性文件

  1. 将这些模块的配置集中在一个* .properties文件中。
  2. 使该文件在类路径中可用。
  3. 保持此文件的安装/配置尽可能简单。理想情况下,只需将它放在一些JBoss文件夹中,如$ {JBOSS_HOME}/standalone/configuration。
  4. 无需重新启动应用程序服务器即可更改此文件。

这可能吗?

我已经找到了这个链接:How to put an external file in the classpath,它解释了最好的方法是制作静态的JBoss模块。但是,我必须在我部署的每个应用程序模块中对此静态模块进行依赖,这是我试图避免的一种耦合。

回答

0

./standalone.sh --server-config = standalone -gdrais.xml --properties =/home/user/programs/jboss/jboss-eap-6.0/standalone/configuration/file.properties -b 0.0。 0.0

5

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

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 
    } 
} 
2

这里只使用CDI,从这个site采取一个完整的例子。

此配置也适用于JBoss AS7。

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

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

    $ ./bin/jboss-cli.sh --connect 
    [[email protected]:9990 /] /system-property=application.properties:add(value=${jboss.server.config.dir}/application.properties) 
    

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

<system-properties> 
    <property name="application.properties" value="${jboss.server.config.dir}/application.properties"/> 
</system-properties> 
  • 创建单加载和存储应用程序宽属性的会话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); 
        } 
    } 
    
  • 创建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(); 
    } 
    
  • 创建生产者方法;此产生的对象将被注入

    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); 
        } 
    } 
    
  • 最后的财产注入到你的CDI豆之一

    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; 
        } 
    }