外部化 Grails 数据源配置 [英] Externalizing Grails Datasource configuration
问题描述
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屋!