Grails3中的外部属性文件

Grails3中的外部属性文件,grails,Grails,我需要从Grails3中的外部文件属性读取配置。在grails 2.x中,我将该文件链接到: grails.config.locations = ["classpath:config.properties"] 在config.groovy中,但该文件在Grails3中不存在 您对解决这个问题有什么想法吗?似乎没有现成的外部化配置:因为Grails 3是基于Spring Boot构建的,所以您可以使用Spring Boot机制来实现外部化属性。即,使用spring.config.location

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

grails.config.locations = ["classpath:config.properties"]
在config.groovy中,但该文件在Grails3中不存在


您对解决这个问题有什么想法吗?

似乎没有现成的外部化配置:

因为Grails 3是基于Spring Boot构建的,所以您可以使用Spring Boot机制来实现外部化属性。即,使用
spring.config.location
命令行参数或
spring\u BOOT\u location
环境变量

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

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
我使用它的方式是设置一个环境变量,如下所示:

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

一个很好的特性是,您可以在捆绑在应用程序中的属性文件中保留一些属性,但如果有一些属性您不想包括(例如密码),则可以在外部配置文件中专门设置这些属性。

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

You can use

def cfg = new ConfigSlurper.parse(getClass().classLoader.getResource('path/myExternalConfigfile.groovy'))
java -jar app-0.3.jar
在当前目录中,我创建了一个application.yml文件,其中有一行:

org.xyz.app.title: 'XYZZY'
我还使用此文件指定数据库和其他此类信息。

请查看

Grails3中的外部配置 在使用Grails3时,我意识到我不再能够使用
config.groovy
文件中的标准
Grails.config.locations
属性指定外部配置

原因是显而易见的!Grails3中现在没有
Config.groovy
。我们现在使用
application.yml
来配置属性。但是,也不能使用此文件指定外部配置

什么是黑客?

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

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

例如,由此:

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

dataSource:
  username: root
  password: mysql
  driverClassName: "com.mysql.jdbc.Driver"
首先,我需要将这个文件放在应用程序的根目录中。例如,我的Grails 3应用程序具有以下结构,外部配置文件
app config.yml
已就位:

[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
到您的
应用程序.groovy
文件

bootRun {
  // local.config.location is just a random name. You can use yours.
  jvmArgs = ['-Dlocal.config.location=app-config.yml']
}
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
方法

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

代码和指南摘自以下链接,修改很少:


  • 来源:

    从Grails3中的外部位置读取属性文件时,我遇到了同样的问题。我发现这有助于我从外部位置读取属性。它还具有读取.yml、.groovy文件的功能

    按照文档中提到的步骤操作,它将起作用。 步骤如下:

  • 在build.gradle中添加依赖项:

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

    config:
        locations:
            - file:///opt/abc/application.yml
    
  • 在外部位置创建文件。在我的例子中,
    /opt/abc/application.yml

  • 构建应用程序并运行


  • 你的问题是,Config.groovy怎么了?它在Spring 3.x中被重命名为application.groovy。是的,但我不知道如何使用外部配置文件,就像我在grails 2.x中所做的那样。在“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中不再有效+