{"id":16323,"date":"2014-12-10T18:14:44","date_gmt":"2014-12-10T12:44:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16323"},"modified":"2014-12-10T18:14:44","modified_gmt":"2014-12-10T12:44:44","slug":"defining-a-bean-by-calling-function-on-another-bean","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/defining-a-bean-by-calling-function-on-another-bean\/","title":{"rendered":"Defining a bean by calling function on another bean"},"content":{"rendered":"<p>In Grails, We can define custom beans in resources.groovy. A simple bean definition looks like<\/p>\n<p>[java]<br \/>\nmyClass(MyClass)<br \/>\n[\/java]<\/p>\n<p>Read more about them <a href=\"http:\/\/grails.org\/doc\/latest\/guide\/spring.html\">here<\/a>. <\/p>\n<p>Recently, I wanted to define a bean, where the bean&#8217;s constructor required me to call a method on another bean to be instantiated.<\/p>\n<p>To make it more clear, Let us consider a class A which has a method getApi() in it. Consider another class B, now to define class B as a spring bean we need to call getApi() method of class A (which itself is a spring bean) in the constructor of B.<\/p>\n<p>In code the dependency is like this<\/p>\n<p>[java]<br \/>\nA a = new A()<br \/>\nB b = new B(a.getApi())<br \/>\n[\/java]<\/p>\n<p>The only complication being both A and B needed to be spring defined singleton beans in resources.groovy.<\/p>\n<p>Lets see how we can solve this problem, In resources.groovy <\/p>\n<p>[java]<br \/>\na(A)<br \/>\nb(B) { beanDefinition -&gt;<br \/>\n        beanDefinition.constructorArgs = [&quot;#{a.getApi()}&quot;]<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>In the above code, we defined the constructor arguments for class B, as a list containing only 1 element, which is a method invoked on bean &#8220;a&#8221;. <\/p>\n<p>Now to use these 2 beans in a third class &#8220;C&#8221; is trivial, in resources.groovy just do<\/p>\n<p>[java]<\/p>\n<p>c(C) {<br \/>\n  a = ref(&quot;a&quot;)<br \/>\n  b = ref(&quot;b&quot;)<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>~~Cheers~~<\/p>\n<p>Sachin Anand<br \/>\nsachin[at]intelligrape[dot]com<br \/>\n@babasachinanand<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Grails, We can define custom beans in resources.groovy. A simple bean definition looks like [java] myClass(MyClass) [\/java] Read more about them here. Recently, I wanted to define a bean, where the bean&#8217;s constructor required me to call a method on another bean to be instantiated. To make it more clear, Let us consider a [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":19},"categories":[7],"tags":[1573,1569,1572,1570,1571,4841],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16323"}],"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\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16323"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16323\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}