Getting application configuration in doWithSpring closure using Grails 3 application

Grails 3 allows authors to use start hooks similar to those provided by Grails 2 plugins. I look at the definition of beans in a doWithSpring closure, and I would like to pass values ​​to a new bean based on some configuration values. However, I cannot figure out how to get the grailsApplication instance or application configuration. How do you do this with Grails 3?

+5
source share
2 answers

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

You can simply refer to the config property.

Similarly, you can refer to the inherited property grailsApplication , which is defined in https://github.com/grails/grails-core/blob/9f78cdf17e140de37cfb5de6671131df3606f2fe/grails-core/src/main/groovy/grails/plugins/lugin .

I hope this helps.

+2
source

Under Grails 3, I took the recommendation of Jeff Scott Brown and used GrailsApplicationAware instead:

Here's how you are going to configure the bean:

So, in your new plugin descriptor, you need to change the grails 2 def doWithSpring style to ClosureDoWithSpring, as shown below:

Notice that in Grails 2 we introduced grailsApplication, in grails 3 all we do is declare a bean:

 /* def doWithSpring = { sshConfig(SshConfig) { grailsApplication = ref('grailsApplication') } } */ Closure doWithSpring() { {-> sshConfig(SshConfig) } } 

Now, to get your plugin configuration:

Src / main / groovy / grails / plugin / remotessh / sshconfig sshconfig.groovy

 package grails.plugin.remotessh import grails.core.GrailsApplication import grails.core.support.GrailsApplicationAware class SshConfig implements GrailsApplicationAware { GrailsApplication grailsApplication public ConfigObject getConfig() { return grailsApplication.config.remotessh ?: '' } } 

grails.plugin.remotessh.RemoteSsh.groovy:

 String Result(SshConfig ac) throws InterruptedException { Object sshuser = ac.config.USER ?: '' Object sshpass = ac.config.PASS ?: '' ... 

Now your configuration object is passed to the groovy src classes. The end user application would pass the sshConfig bean as follows:

 class TestController { def sshConfig def index() { RemoteSSH rsh = new RemoteSSH() .... def g = rsh.Result(sshConfig) } 

Edited to add, just found this :) which is a relevant or recurring question:

http://grails.1312388.n4.nabble.com/Getting-application-config-in-doWithSpring-closure-with-a-Grails-3-application-td4659165.html

+1
source

All Articles