{"id":305,"date":"2009-11-20T19:13:55","date_gmt":"2009-11-20T13:43:55","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=305"},"modified":"2010-06-09T15:50:04","modified_gmt":"2010-06-09T10:20:04","slug":"accessing-spring-beans-by-implementing-applicationcontextaware-in-grails-artefacts","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/accessing-spring-beans-by-implementing-applicationcontextaware-in-grails-artefacts\/","title":{"rendered":"Accessing Spring Beans by Implementing ApplicationContextAware in Grails Artefacts"},"content":{"rendered":"<p>Many a times, it is required to inject a Spring Bean into a Grails artefact. It has happened a few times with us in our projects that we needed to inject a TagLib bean in the service layer for code re-use for sending out e-mails. This is how we achieved it<\/p>\n<p><code>MyService implements org.springframework.context.ApplicationContext<\/code><\/p>\n<p>Import the classes import org.springframework.context.ApplicationContext and org.codehaus.groovy.grails.commons.ConfigurationHolder<\/p>\n<p>Define a method as given below<\/p>\n<blockquote><p>public def getMyTag() {<br \/>\nreturn ConfigurationHolder.config.applicationContext.getBean(&#8220;MyTagLib&#8221;)<br \/>\n}<\/p><\/blockquote>\n<p>where MyTagLib is a TagLib class is a custom TagLib class. For ApplicationTagLib, which contains the tags like createLink() ,replace &#8220;MyTagLib&#8221; with &#8220;ApplicationTagLib&#8221; as the argument in the getBean() method.<\/p>\n<p>We can access the methods\/closures in the bean with a statement like myTag.methodName()<\/p>\n<p>One thing to note is that writing something like<\/p>\n<blockquote><p><code>def myTag = ConfigurationHolder.config.applicationContext.getBean(\"MyTagLib\")<\/code><\/p><\/blockquote>\n<p>will not work because applicationContext would not have been initialized then and an exception will be thrown.<br \/>\nHope this Helps.<\/p>\n<p>S Vivek Krishna<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many a times, it is required to inject a Spring Bean into a Grails artefact. It has happened a few times with us in our projects that we needed to inject a TagLib bean in the service layer for code re-use for sending out e-mails. This is how we achieved it MyService implements org.springframework.context.ApplicationContext Import [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/305"}],"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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}