How exactly does groovy understand the static mapping in Grails?

133 views
Skip to first unread message

yogesh...@traderev.com

unread,
Jan 21, 2019, 3:14:00 PM1/21/19
to Grails Dev Discuss

I am new to Grails framework and I came across this code

static mapping = {
        cache true
    }

I know Grails is on Groovy, but I am not understanding how Groovy understands this? Is this a Closure and If yes, the this doesnt seem resemble Closure. As far as I know, if a method is passed a closure it is defined something like this:

def helloWorld = {
  println "Hello World"
}

But this static mapping doesnt seem to be Closure as well. How does Grails leverage Groovy to do this ORM mapping? I hope I am articulating my question well!

Jeff Scott Brown

unread,
Jan 21, 2019, 3:56:15 PM1/21/19
to Grails Dev Discuss
Yes, it is a closure.

static mapping = {
// code here
}

def helloWorld = {
// code here
}

Those are both closures. The closure is an object. The first one is a
variable being declared as a static member and the second is being
declared as an instance member. Those are similar to the following:

static name = ‘Jeff’

def name = ‘Jeff’

In those, the object is a String, not a Closure. The first one is a
static member and the second is an instance member.

Does that make sense?



JSB


--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
http://objectcomputing.com

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

yogesh...@traderev.com

unread,
Jan 21, 2019, 4:21:13 PM1/21/19
to Grails Dev Discuss
Thanks JSB,
So How does Grail pick this mapping? where is this function defined by grails? there must be some place where this closure is ultimately passed? Can I see that so that its clear tp me. 
does it look something like this

def mapping{
}

Mihai Glonț

unread,
Jan 21, 2019, 5:09:48 PM1/21/19
to grails-de...@googlegroups.com, yogesh...@traderev.com


On 21/01/2019 21:21, yogesh...@traderev.com wrote:
Thanks JSB,
So How does Grail pick this mapping? where is this function defined by grails? there must be some place where this closure is ultimately passed? Can I see that so that its clear tp me. 
does it look something like this

def mapping{
}

In very simple terms Grails considers URL mappings as one of its special kinds of artefacts, like domain classes or controllers and it has means of keeping an eye on the list of mappings for a given application.

When the UrlMappings plugin[1] is loaded it configures a number of beans[2], including UrlMappingsFactoryBean[3]. The latter uses the DefaultUrlMappingEvaluator[4] to interpret all those "mappings" closures and convert them into corresponding UrlMapping instances. You can see what url mappings are registered in an application by inspecting the grailsUrlMappingsHolderBean bean in a Grails console[5].

Hope this helps,

Mihai.

[1] https://docs.grails.org/latest/ref/Plug-ins/URL%20mappings.html
[2] https://github.com/grails/grails-core/blob/v3.3.9/grails-plugin-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPlugin.groovy#L120
[3] https://github.com/grails/grails-core/blob/v3.3.9/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/UrlMappingsHolderFactoryBean.java#L90
[4] https://github.com/grails/grails-core/blob/v3.3.9/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/DefaultUrlMappingEvaluator.java#L194
[5] https://docs.grails.org/latest/ref/Command%20Line/console.html


--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.
To post to this group, send email to grails-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/34a74b88-5132-4f16-bd8e-619e75e908b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff Scott Brown

unread,
Jan 21, 2019, 9:22:10 PM1/21/19
to grails-de...@googlegroups.com
On 21 Jan 2019, at 16:09, Mihai Glonț wrote:

> On 21/01/2019 21:21, yogesh...@traderev.com wrote:
>> Thanks JSB,
>> So How does Grail pick this mapping? where is this function defined
>> by
>> grails? there must be some place where this closure is ultimately
>> passed? Can I see that so that its clear tp me. 
>> does it look something like this
>>
>> |
>> defmapping{
>> }
>>
>> |
>
> In very simple terms Grails considers URL mappings as one of its
> special
> kinds of artefacts, like domain classes or controllers and it has
> means
> of keeping an eye on the list of mappings for a given application.
>
> When the UrlMappings plugin[1] is loaded it configures a number of
> beans[2], including UrlMappingsFactoryBean[3]. The latter uses the
> DefaultUrlMappingEvaluator[4] to interpret all those "mappings"
> closures
> and convert them into corresponding UrlMapping instances. You can see
> what url mappings are registered in an application by inspecting the
> grailsUrlMappingsHolderBean bean in a Grails console[5].
>
> Hope this helps,
>
> Mihai.
>

Judging from the sample code in the original question, I don’t think
the question is about URL mapping. I think it is about domain mapping.

yogesh...@traderev.com

unread,
Jan 21, 2019, 9:39:09 PM1/21/19
to Grails Dev Discuss
Yes,
So Where can I see the implementation of this domain mapping?
I am still not clear how Groovy or Grails understands closures for property/field?

Mihai Glonț

unread,
Jan 25, 2019, 5:25:17 PM1/25/19
to grails-de...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.
To post to this group, send email to grails-de...@googlegroups.com.

Jeff Scott Brown

unread,
Jan 27, 2019, 9:39:12 AM1/27/19
to grails-de...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages