I was recently working on a grails project and found that it is possible to read the configuration/properties defined in application.yml using spring annotation in Grails 3. Its possible in Grails 3 because it uses Spring boot under the hood. Here is an example:
import org.springframework.beans.factory.annotation.Value class ConfigController { @Value('${server.sessionTimeout}') String timeoutValue def index() { render "Timeout is: ${timeoutValue}" // Prints 'Timeout is 1200' } }
Content of application.yml:
server:
sessionTimeout: 1200
contextPath: /eportal
In the above method we are using value annotation to inject the config values from application.yml file.
The old way of retreiving the value of the configuration is:
def timeoutValue = grailsApplication.config.getProperty('server.sessionTimeout')
which is an obsolete way of doing it. So its more cleaner to user the annotation which is only available in Grails 3+.
That was all!
Thanks.
Recent Comments