{"id":20845,"date":"2015-06-09T21:05:55","date_gmt":"2015-06-09T15:35:55","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=20845"},"modified":"2015-06-11T11:27:32","modified_gmt":"2015-06-11T05:57:32","slug":"application-configurations-setting-in-grails-3","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/application-configurations-setting-in-grails-3\/","title":{"rendered":"Application configurations setting in Grails 3"},"content":{"rendered":"<p>Grails has done lot of changes with its <a href=\"http:\/\/www.tothenew.com\/grails-application-development\">latest release in Grails 3<\/a>, using configuration is one of them.<br \/>\nNow we can write application configurations in application.yml and application.groovy as well and to read these property we can need to write the following code.<\/p>\n<p>[code]<br \/>\nclass ExampleController {<\/p>\n<p>    GrailsApplication grailsApplication<\/p>\n<p>    def index() {<br \/>\n        render &quot;Old way ${grailsApplication.config.grails.project.groupId}&quot;<br \/>\n        render &quot;New way ${grailsApplication.config.getProperty(&#8216;grails.project.groupId&#8217;)}&quot;<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>[\/code]<\/p>\n<p>Since Grails 3 is built on Spring boot so we can use Spring way of configuration as well using a simple annotation.<\/p>\n<p>[code]<br \/>\nimport org.springframework.beans.factory.annotation.Value<\/p>\n<p>class ExampleController {<\/p>\n<p>    @Value(&#8216;${grails.project.groupId}&#8217;)<br \/>\n    String groupId<\/p>\n<p>    def index() {<br \/>\n        render &quot;Controller ${groupId}&quot;<br \/>\n    }<\/p>\n<p>[\/code]<\/p>\n<p>Using the annotation based configuration the application will read configuration from the environment, system properties and the local application configuration merging them into a single object. So if you have set the environment variable e.g; GRAILS_PROJECT_GROUPID it will override the local application configuration variable.<\/p>\n<p>Writting the same variable configuration in every controller isn&#8217;t a very nice way so we can create a service which will return the config variables to us like following<\/p>\n<p>[code]<br \/>\nimport org.springframework.beans.factory.annotation.Value<\/p>\n<p>class ApplicationConfigService {<br \/>\n    static transactional = false<\/p>\n<p>    @Value(&#8216;${grails.project.groupId}&#8217;)<br \/>\n    String groupId<br \/>\n}<\/p>\n<p>class ExampleController {<\/p>\n<p>    ApplicationConfigService appConfigService<\/p>\n<p>    def index() {<br \/>\n        render &quot;Service ${appConfigService.groupId}&quot;<br \/>\n    }<\/p>\n<p>    def setConfig(String value) {<br \/>\n        appConfigService.groupId = value<br \/>\n        render &quot;Config set to ${appConfigService.groupId}&quot;<br \/>\n    }<\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p>Now we have a separate code to read and set the application variable without any methods just simple annotations.<\/p>\n<p>Hope it helps<br \/>\nReference <a href=\"http:\/\/grails.github.io\/grails-doc\/latest\/guide\/conf.html#config\">http:\/\/grails.github.io\/grails-doc\/latest\/guide\/conf.html#config<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grails has done lot of changes with its latest release in Grails 3, using configuration is one of them. Now we can write application configurations in application.yml and application.groovy as well and to read these property we can need to write the following code. [code] class ExampleController { GrailsApplication grailsApplication def index() { render &quot;Old [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7,1],"tags":[1854,1509,1856,1855],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20845"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=20845"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20845\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=20845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=20845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=20845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}