【问题标题】:External properties file in grails 3grails 3中的外部属性文件
【发布时间】:2015-03-29 23:01:23
【问题描述】:

我需要从 grails 3 中的外部文件属性中读取配置。在 grails 2.x 中,我将文件链接到:

grails.config.locations = ["classpath:config.properties"]

在config.groovy中,但是这个文件在grails 3中不存在。

你有什么解决办法吗?

【问题讨论】:

  • 您的问题是,Config.groovy 发生了什么?它在 Spring 3.x 中重命名为 application.groovy。
  • 是的,但我不知道如何使用外部配置文件,就像我在 grails 2.x 中所做的那样。

标签: grails


【解决方案1】:

看看https://gist.github.com/reduardo7/d14ea1cd09108425e0f5ecc8d3d7fca0

Grails 3 中的外部配置

Grails 3上工作我意识到我不能再使用Config.groovy 文件中的标准grails.config.locations 属性来指定外部配置。

原因很明显! Grails 3 中现在没有Config.groovy。相反,我们现在使用application.yml 来配置属性。但是,您也不能使用此文件指定外部配置!

什么破解?

现在 Grails 3 使用 Spring 的属性源概念。为了使外部配置文件能够工作,我们现在需要做一些额外的事情。

假设我想用我的外部配置文件覆盖application.yml 文件中的一些属性。

例如,从这个:

  dataSource:
    username: sa
    password:
    driverClassName: "org.h2.Driver"

到这里:

dataSource:
  username: root
  password: mysql
  driverClassName: "com.mysql.jdbc.Driver"

首先我需要将此文件放在应用程序的根目录中。例如,我使用外部配置文件 app-config.yml 遵循 Grails 3 应用程序的结构:

[human@machine tmp]$ tree -L 1 myapp
myapp
├── app-config.yml // <---- external configuration file!
├── build.gradle
├── gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
├── grails-app
└── src

现在,要启用读取此文件,只需添加以下内容:

到您的build.gradle 文件

bootRun {
  // local.config.location is just a random name. You can use yours.
  jvmArgs = ['-Dlocal.config.location=app-config.yml']
}

到您的Application.groovy 文件

package com.mycompany

import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
import org.springframework.context.EnvironmentAware
import org.springframework.core.env.Environment
import org.springframework.core.env.PropertiesPropertySource
import org.springframework.core.io.FileSystemResource
import org.springframework.core.io.Resource
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
    public final static String LOCAL_CONFIG_LOCATION = "local.config.location"

    static void main(String[] args) {
        GrailsApp.run(Application, args)
    }

    @Override
    void setEnvironment(Environment environment) {
        String configPath = System.properties[LOCAL_CONFIG_LOCATION]

        if (!configPath) {
           throw new RuntimeException("Local configuration location variable is required: " + LOCAL_CONFIG_LOCATION)
        }

        File configFile = new File(configPath)
        if (!configFile.exists()) {
           throw new RuntimeException("Configuration file is required: " + configPath)
        }

        Resource resourceConfig = new FileSystemResource(configPath)
        YamlPropertiesFactoryBean ypfb = new YamlPropertiesFactoryBean()
        ypfb.setResources([resourceConfig] as Resource[])
        ypfb.afterPropertiesSet()
        Properties properties = ypfb.getObject()
        environment.propertySources.addFirst(new PropertiesPropertySource(LOCAL_CONFIG_LOCATION, properties))
    }
}

注意Application 类实现了EnvironmentAware 接口并覆盖了它的setEnvironment(Environment environment):void 方法。

现在这就是在 Grails 3 应用程序中重新启用外部配置文件所需的全部内容。

代码和指南摘自以下链接,稍作修改:

  1. http://grails.1312388.n4.nabble.com/Grails-3-External-config-td4658823.html
  2. https://groups.google.com/forum/#!topic/grails-dev-discuss/_5VtFz4SpDY

来源:https://gist.github.com/ManvendraSK/8b166b47514ca817d36e

【讨论】:

  • 在“grails war”之后,“app-config.yml”文件不会在目录中,并且不会加载配置。有一个解决方法:在“src/main”目录上创建其他名为“resources”的目录(src/main/resources)并将配置文件(app-config.yml)放在那里。用这个加载文件:def url = getClass().classLoader.getResource("app-config.yml")def file = new File(url.toURI())Resource resourceConfig = new FileSystemResource(file)这样你就可以在生产和开发模式下加载外部配置文件了。
  • 这似乎不再适用于 grails 4.0.2+
【解决方案2】:

因为 Grails 3 是基于 Spring Boot 构建的,所以您可以使用 Spring Boot 机制来实现外部化属性。即,使用spring.config.location 命令行参数或SPRING_BOOT_LOCATION 环境变量。 Here's the Spring documentation page on it.

文档为命令行参数提供的示例如下:

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

我一直使用它的方式是设置一个环境变量,像这样:

export SPRING_CONFIG_LOCATION="/home/user/application-name/application.yml"

其中一个不错的功能是您可以在应用程序中捆绑的属性文件中保留一些属性,但是如果您不想包含某些属性(例如密码),可以在其中专门设置这些属性外部配置文件。

【讨论】:

    【解决方案3】:

    从 .jar 文件运行时,我发现 Spring Boot 在当前目录中查找 application.yml 文件。

    java -jar app-0.3.jar
    

    在当前目录下,我创建了一个application.yml文件,一行一行:

    org.xyz.app.title: 'XYZZY'
    

    我还使用此文件来指定数据库和其他此类信息。

    【讨论】:

      【解决方案4】:

      我在从 Grails 3 中的外部位置读取属性文件时遇到了同样的问题。我发现这个 plugin 可以帮助我从外部位置读取属性。它还具有读取 .yml、.groovy 文件的功能。

      按照文档中提到的步骤操作即可。 步骤如下:

      1. 在 build.gradle 中添加依赖:

        dependencies {compile 'org.grails.plugins:external-config:1.2.2'}
        
      2. 将此添加到application.yml grails:

        config:
            locations:
                - file:///opt/abc/application.yml
        
      3. 在外部位置创建文件。在我的情况下/opt/abc/application.yml

      4. 构建应用程序并运行。

      【讨论】:

        【解决方案5】:
        You can use
        
        def cfg = new ConfigSlurper.parse(getClass().classLoader.getResource('path/myExternalConfigfile.groovy'))
        

        【讨论】:

          【解决方案6】:

          【讨论】:

            猜你喜欢
            • 2018-08-05
            • 2013-01-28
            • 2016-12-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-15
            相关资源
            最近更新 更多