外部化Grails数据源配置 [英] Externalizing Grails Datasource configuration

查看:240
本文介绍了外部化Grails数据源配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails 1.x允许通过设置 grails.config.locations 指令使用外部配置文件。是否有类似的方法可用于外部化Datasource.groovy中的数据库配置(不设置JNDI)?

Grails 1.x allows using external configuration files by setting the grails.config.locations directive. Is there a similar approach available for externalizing the database configuration in Datasource.groovy (without setting up JNDI)?

这将有助于能够配置数据库凭据简单的配置文件。

It would prove helpful to be able to configure DB credentials in a simple configuration file outside the application.

提前感谢!

推荐答案

>您可以使用grails.config.locations中指定的属性文件作为外部化数据源配置的方法。下面是我通常设置一个Grails项目:

You can use a properties file specified in the grails.config.locations as a way to externalize the datasource configuration. Below is how I typically set up a Grails project:

在我的DataSource.groovy中,我为生产环境指定:

In my DataSource.groovy I specify this for the production environment:

  ....
  ....
  production {
    dataSource {
        dbCreate = "update"
        driverClassName = "com.myorg.jdbcDriverNotExists"
        url = ""
        username = ""
        password = ""
    }
  }
  ....
  ....



在我的Config.groovy中指定一个外部属性文件:

I specify an external properties file in my Config.groovy:

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

在属性文件(存储在grails-app / conf /)中,我指定实际的数据源信息:

In the properties file (stored in grails-app/conf/) I specify the actual datasource info:

dataSource.driverClassName=oracle.jdbc.OracleDriver
dataSource.url=jdbc:oracle:thin:@host:port:sid
dataSource.username=sa
dataSource.password=secret

我也使用属性文件以覆盖Config.groovy中的其他值。当应用程序部署时,如果我必须修改数据源信息,我只是编辑/WEB-INF/classes/app-config.properties文件并重新启动。

I also use the properties file as a way to override other values that are in Config.groovy. When the app is deployed, if I have to modify the datasource info I just edit the /WEB-INF/classes/app-config.properties file and restart.

这篇关于外部化Grails数据源配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆