Getting application config in doWithSpring closure with a Grails 3 application

555 views
Skip to first unread message

Bud Byrd

unread,
Apr 17, 2015, 1:24:36 PM4/17/15
to grails-de...@googlegroups.com
I didn't get a lot of traction on stack overflow, so I figured I'd try asking here.

Grails 3 allows authors to use startup hooks similar to the ones provided to Grails 2 plugins. I'm looking at defining beans in the doWithSpring closure, and I'd like to pass values into a new bean based on some configuration values. I can't figure out, however, how to get the grailsApplication instance or the application configuration. How do you do this with Grails 3?

Jeff Scott Brown

unread,
Apr 17, 2015, 4:20:57 PM4/17/15
to grails-de...@googlegroups.com

> On Apr 17, 2015, at 12:24 PM, Bud Byrd <bud....@gmail.com> wrote:
>
> I didn't get a lot of traction on stack overflow, so I figured I'd try asking here.
>
> Grails 3 allows authors to use startup hooks similar to the ones provided to Grails 2 plugins. I'm looking at defining beans in the doWithSpring closure, and I'd like to pass values into a new bean based on some configuration values. I can't figure out, however, how to get the grailsApplication instance or the application configuration. How do you do this with Grails 3?


You can just refer to the “config” property.



JSB
--
Jeff Scott Brown
je...@jeffandbetsy.net

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

Jeff Scott Brown

unread,
Apr 17, 2015, 4:25:53 PM4/17/15
to grails-de...@googlegroups.com

> On Apr 17, 2015, at 3:20 PM, Jeff Scott Brown <je...@jeffandbetsy.net> wrote:
>
>
>> On Apr 17, 2015, at 12:24 PM, Bud Byrd <bud....@gmail.com> wrote:
>>
>> I didn't get a lot of traction on stack overflow, so I figured I'd try asking here.
>>
>> Grails 3 allows authors to use startup hooks similar to the ones provided to Grails 2 plugins. I'm looking at defining beans in the doWithSpring closure, and I'd like to pass values into a new bean based on some configuration values. I can't figure out, however, how to get the grailsApplication instance or the application configuration. How do you do this with Grails 3?
>
>
> You can just refer to the “config” property.


Your plugin should extend grails.plugins.Plugin which defines the getConfig() method. See https://github.com/grails/grails-core/blob/9f78cdf17e140de37cfb5de6671131df3606f2fe/grails-core/src/main/groovy/grails/plugins/Plugin.groovy#L65.

Bud Byrd

unread,
Apr 17, 2015, 11:03:05 PM4/17/15
to grails-de...@googlegroups.com
I'm sorry for not making my ask clear.  I'm speaking of the Application.groovy file located in grails-app/init, for grails applications (not plugins).  We now have the ability to use the same startup hooks in applications as we do in plugins (such as doWithSpring, etc).  I'd like to use these methods instead of relying on the spring resources file, but I can not find a way to use the application configuration in this file.

The Application.groovy file extends GrailsAutoConfiguration.

Thanks for any help!

Jeff Scott Brown

unread,
Apr 18, 2015, 12:03:47 AM4/18/15
to grails-de...@googlegroups.com

> On Apr 17, 2015, at 10:03 PM, Bud Byrd <bud....@gmail.com> wrote:
>
> I'm sorry for not making my ask clear. I'm speaking of the Application.groovy file located in grails-app/init, for grails applications (not plugins). We now have the ability to use the same startup hooks in applications as we do in plugins (such as doWithSpring, etc). I'd like to use these methods instead of relying on the spring resources file, but I can not find a way to use the application configuration in this file.
>
> The Application.groovy file extends GrailsAutoConfiguration.
>
> Thanks for any help!


It really depends on what it is you want to do with the config values. One thing you can do is refer to grails.util.Holders.config but that may not really be the best thing to do. The best solution depends on details you haven't provided but just as an example...

In your Application.doWithSpring method you could create the bean as per usual without any reference to the config and have the bean configure itself at post processing time.

// grails-app/init/myapp/Application.groovy
package myapp

import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration

class Application extends GrailsAutoConfiguration {

@Override
Closure doWithSpring() {{ ->
// create a bean...
someBeanName demo.SomeBean
}}

static void main(String[] args) {
GrailsApp.run(Application)
}
}


// src/main/groovy/demo/SomeBean.groovy
package demo

import grails.core.support.GrailsApplicationAware
import grails.core.GrailsApplication

class SomeBean implements GrailsApplicationAware {

GrailsApplication grailsApplication

String someStringConfigValue

void setGrailsApplication(GrailsApplication ga) {
def config = ga.config

// do whatever you want to do with the config here, for example…
someStringConfigValue = config.foo.bar.bing.baz
}
}

You could also write a bean post processor if you need to interact with a bunch of different beans. You have a number of options and knowing which one is best depends on what it is you are really trying to do.

It may be that we should look at providing direct access to the config in Application.doWithSpring, or maybe there is already a path there that I am overlooking.

I don't know if that is really going to help or not.

Bud Byrd

unread,
Apr 19, 2015, 11:22:20 AM4/19/15
to grails-de...@googlegroups.com
It does answer the question, at least from the "does it work the same as plugins" perspective.  It seems to me like this is missing functionality.  If applications are given what looks to be the same initialization methods as plugins, their usage should be as close to the same as possible.  It looks like it's almost there, except we don't get access to the grails application instance.

I prefer to inject configuration values into a bean when instantiating it, and not requiring the bean to load its own configuration.  Given that, I agree that using Holders is probably not the best thing to do, but is probably the simplest and most straightforward solution with the lack of direct grails application access.  I definitely think this should be a feature that gets added.

Any of these solutions above work, 
Reply all
Reply to author
Forward
0 new messages