【问题标题】:Add config file based on deploymentName根据deploymentName添加配置文件
【发布时间】:2014-02-12 21:16:01
【问题描述】:

我正在尝试加载特定于 JBoss 服务器上的部署的配置文件。这个想法是我可以在同一台服务器上对同一应用程序(示例、培训、测试、开发)进行多次部署。而且每个部署都有不同的配置。

现在我的方向是通过 JNDI 从 jboss 获取部署名称。这是我的 grails-app/conf/Config.groovy 的一部分

appCtx = new InitialContext().lookup("java:app")
if(appCtx) {
  deploymentName = appCtx.lookup("AppName")
  grails.config.locations << "classpath:${deploymentName}-config.properties"
  grails.config.locations << "classpath:${deploymentName}-config.groovy"
}

然后,如果我将战争文件命名为training.war,将其粘贴到deployments 文件夹中,那么它应该从中获取配置

configurations/training-config.properties

我遇到的问题是打包时出错...

Need to specify class name in environment or system property,
or as an applet parameter, or in an application resource file:
java.naming.factory.initial

有人对如何解决这个问题有想法吗?或者是否有更简单的方法?

我正在使用 JBoss 7.1.1

【问题讨论】:

    标签: java grails jboss jndi


    【解决方案1】:

    我想通了...就像将代码放在 try/catch 块中一样简单...

    import javax.naming.Context
    import javax.naming.InitialContext
    try {
      Context appCtx = (Context)(new InitialContext().lookup("java:app"))
    
      // JBoss's deployment name
      // you can see this at Profile >> Container >> Naming >> applications
      deploymentName = appCtx.lookup("AppName")
    
      grails.config.locations << "classpath:${deploymentName}-config.properties"
      grails.config.locations << "classpath:${deploymentName}-config.groovy"
      if(System.properties["${deploymentName}.config.location"]) {
        grails.config.locations << "file:" +
          System.properties["${deploymentName}.config.location"]
      } else if(System.getenv("${deploymentName}.config.location")) {
        grails.config.locations << "file:" + 
          System.getenv("${deploymentName}.config.location")
      }
    }catch(Exception ex) {
      // Initial Context does not exist
      // aka, not deployed
    }
    

    修改后的代码还允许指定部署名称的系统属性或环境变量。这可以通过 JBoss 7.1.1 进行配置。对于已部署的 training.war 应用程序...

    配置文件>>常规配置>>系统属性>>添加

    像这样输入密钥...

    training.config.location
    

    还有价值/路径。如果在 Windows 上,则双重转义路径

    E:\\Servers\\jboss\\jboss7.1.1\\standalone\\configurations\\training-config.properties
    

    然后将war文件部署到jboss。应该选择配置。 :D

    通过this blog 向 Mike 提供支持,以提出系统属性/环境变量的想法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2018-04-05
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多