Friday, May 22, 2015

Grails use an external properties file for datasource configuration

Grails use an external properties file for datasource configuration


1.configure the DataSource.groovy file like this:



import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.support.PropertiesLoaderUtils

def properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource('datasource.properties'))

dataSource
pooled = true
driverClassName = properties.getProperty("dataSource.driverClassName")
username = properties.getProperty("dataSource.username")
password = properties.getProperty("dataSource.password")
url = properties.getProperty("dataSource.url")

hibernate
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'

// environment specific settings
environments
development
dataSource
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''


test
dataSource
dbCreate = "update"


production
dataSource
dbCreate = "update"



2.Create the external database configuration properties file, below is the contents of datasource.properties


dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/a5go?useUnicode=true&characterEncoding=utf-8
dataSource.username=root
dataSource.password=password


Grails use an external properties file for datasource configuration

Thursday, May 21, 2015

use an external properties file in grails

use an external properties file in grails


1.Create the properties file.


dbCreate=update
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF8
username=root
password=password

2.add following line in the Config.groovy:


grails.config.locations = ["classpath:test1.properties", "classpath:Test2.properties"]

if set variables in config use following line


def props = new Properties()

url= props.getProperty("url")

3.in controller/services


import org.codehaus.groovy.grails.commons.ConfigurationHolder


def grailsApplication


def test1= ConfigurationHolder.config.test

def url= grailsApplication.config.url


use an external properties file in grails