Grails(3)Guide Book Chapter 4 Configuration

Grails(3)GuideBookChapter4Configuration

4.3TheDataSource

IfweuseadatabaseotherthanH2weneedaJDBCdriver.It'sthebesttouseIvytoresolvethejar.

ThedependencyfortheMySQLdriverlikethis:

grails.project.dependency.resolution={

inherits("global")

log"warn"

repositories{

grailsPlugins()

grailsHome()

grailsCentral()

mavenCentral()

}

dependencies{

runtime'mysql:mysql-connector-java:5.1.16'

}

}

Ifwecan'tuseIvythenjustputtheJARinourproject'slibdirectory.

Databaseconfigurationfileisher:

grails-app/conf/DataSource.groovy

Taketheseconfigurationasinstance

dataSource{

pooled=true

dbCeate="update"

url="jdbc:mysql://localhost/yourDB"

driverClassname="com.mysql.jdbc.Driver"

dialect=org.hibernate.dialect.MySQL5InnoDBDialect

username="yourUser"

password="yourPassword"

}

dbCreate----create,create-drop,update,validate

create-Dropstheexistingschema.Createstheschemaonstartup.

create-drop-Sameascreate,dropsthetableswhentheapplicationshutsdown.

update-Createsmissingtablesandindexes.

validate-Makenochangestomydatabase,justreportswarning.

4.3.1DataSourcesandEnvironments

Justnotehere,Grails'DataSourcedefinitionis'environmentaware'.

4.3.2JNDIDataSources

ReferringtoaJNDIDataSource

dataSource{

jndiname="java:comp/env/myDataSource"

}

4.3.3AutomaticDatabaseMigration

4.3.4Transaction-awareDataSourceProxy

4.3.5DatabaseConsole

Config.groovy

environments{

production{

grails.serverURL="http://www.sillycat.com"

grails.dbconsole.enabled=true

grails.dbconsole.urlRoot='/admin/dbconsole'

}

development{

grails.serverURL="http://localhost:8080/${appname}"

}

test{

grails.serverURL="http://localhost:8080/${appname}"

}

}

4.3.6MultipleDatasources

BydefaultalldomainclassesshareasingleDataSourceandasingledatabase.

ConfiguringAdditionalDataSources

Ingrails-app/conf/DataSource.groovy

envionments{

development{

dataSource{

dbCreate="create-drop"

url="jdbc:h2:mem:devDb"

}

dataSource_lookup{

dialect=org.hibernate.dialect.MySQLInnoDBDialect

driverClassName='com.mysql.jdbc.Driver'

username='lookup'

password='111111'

url='jdbc:mysql://localhost/lookup'

dbCreate='update'

}

}

…snip…

production{

dataSource{

dbCreate="update"

url="jdbc:h2:prodDb"

}

dataSource_lookup{

dialect=org.hibernate.dialect.Oracle10gDialect

driverClassName='oracle.jdbc.driver.OracleDrvier'

username='lookup'

password='111111'

url='jdbc:oracle:thin:@localhost:1521:lookup'

dbCreate='update'

}

}

}

ConfiguringDomainClasses

IfadomainclasshasnoDataSourceconfiguration,itdefaultstothestandard'dataSource'.Ifwewanttochangethat,weneedtosetthedatasourcepropertyinthemappingblocktoconfigureanon-defaultDataSource.

classZipCode{

Stringcode

staticmapping={

datasource'lookup'

}

}

Adomainclasscanalsouse2ormoreDataSources.

staticmapping={

datasources(['lookup','auditing'])

}

DEFAULTisthespecialnameforthedefaultDatasource

staticmapping={

datasources(['lookup','DEFAULT'])

}

Haha,adomaincanalsoconfiguredtousealltheDataSources

staticmapping={

datasource'ALL'

}

NamespacesandGORMMethods

IfadomainclassusesmorethanoneDataSourcethenwecanusethenamespaceimpliedbyeachDataSourcenametomakeGORMcallsforaparticularDataSource.

defzipCode=ZipCode.auditing.get(42)

…snip…

zipCode.auditing.save()

HibernateMappedDomainClasses

Services

classDataService{

staticdatasource='lookup'

…snip...

}

4.4ExternalizedConfiguration

Inthisexample,wearelosingconfigurationfiles(bothJavaPropertiesfilesandConfigSlurperconfigurations)fromdifferentplacesontheclasspathandfileslocatedinUSER_HOME.

SettinginConfig.groovy

grails.config.locations=[

"classpath:${appName}-config.properties",

"classpath:${appName}-config.groovy",

"file:${userHome}/.grails/${appName}-config.properties",

"file:${userHome}/.grails/${appName}-config.groovy"

]

4.5Versioning

VersioningBasics

Whenwecreatetheproject,theversionis0.1.

>grailsset-version0.2

DetectingVersionsatRuntime

defversion=grailsApplication.metadata['app.version']

WithincontrollersthereisanimplicitgrailsApplicationvariablethatcanbeused.

defgrailsVersion=grailsApplication.metadata['app.grails.version']

alternatively

importgrails.util.GrailsUtil

defgrailsVersion=GrailsUtil.gailsVersion

4.6ProjectDocumentation

>grailsdoc

4.7DependencyResolution

grails-app/conf/BuildConfig.groovy

grails.project.dependency.resolution={

dependencies{

runtime'mysql:mysql-connector-java:5.1.16'

}

plugins{

compile":hibernate:$grailsVersion"

compile":jquery:1.6.1.1"

compile":resources:1.0"

build":tomcat:$grailsVersion"

}

}

4.7.1ConfigurationsandDependencies

Grailsfeaturesfivedependencyresolutionconfigurations(orrathersayscopes)

buildDependenciesforthebuildsystemonly

compileDependenciesforthecompilestep

runtimeDependenciesneededatruntimebutnotforcompilation

testDependenciesneededfortestingbutnotatruntime

providedDependenciesneededatdevelopmenttime,butnotduringWARdeployment

Disablingtransitivedependencyresolution

Bydefault,GrailswillnotonlygettheJARsandpluginsthatyoudeclare,butitwillalsogettheirtransitivedependencies.Wecandisabletransitivedependencyresolution.

runtime('com.mysql:mysql-connector-java:5.1.16',

'net.sf.ehcache:ehcache:1.6.1'){

transitive=false

}

Alternatively

runtimegroup:'com.mysql',

name:'mysql-connector-java',

version:'5.1.16',

transitive:false

Excludingspecifictransitivedependencies

runtime('com.mysql:mysql-connector-java:5.1.16',

'net.sf.ehcache:ehcache:1.6.1'){

excludes"xml-apis","commons-logging"

}

alternatively

runtime(…snip..){

excludes([group:'xml-apis',name:'xml-apis'],

[name:'commons-logging'])

}

WherearetheJARs

Thedefaultplaceisuser.home/.grails/ivy-cache,wecanchangeinsettings.groovy

grails.dependency.cache.dir="${userHome}/.my-dependency-cache"

References:

http://grails.org/doc/latest/guide/conf.html

http://grails.org/doc/latest/guide/index.html

相关推荐