{"id":11691,"date":"2012-10-18T12:00:00","date_gmt":"2012-10-18T11:00:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=5dde766d3eaeffc4111002fb4babbdb6"},"modified":"2012-10-18T12:00:00","modified_gmt":"2012-10-18T11:00:00","slug":"inconsistent-dependency-injection-to-domains-with-grails-2","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2012\/10\/18\/inconsistent-dependency-injection-to-domains-with-grails-2\/","title":{"rendered":"Inconsistent Dependency Injection to domains with Grails"},"content":{"rendered":"<p>I&#8217;ve encountered strange behavior with a domain class in my project: services that should be injected were null. I&#8217;ve became suspicious as why is that? Services are injected properly in other domain classes so why this one is different?<\/p>\n<h3 id=\"constructors-experiment\">Constructors experiment<\/h3>\n<p>I&#8217;ve created an experiment. I&#8217;ve created empty LibraryService that should be injected and Book domain class like this:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/3910934.js?file=Book.groovy\"><\/script> <noscript><\/p>\n<pre><code>class Book {<br \/>    def libraryService<br \/><br \/>    String author<br \/>    String title<br \/>    int pageCount<br \/><br \/>    Book() {<br \/>        println(\"Finished constructor Book()\")<br \/>    }<br \/><br \/>    Book(String author) {<br \/>        this()<br \/>        this.@author = author<br \/>        println(\"Finished constructor Book(String author)\")<br \/>    }<br \/><br \/>    Book(String author, String title) {<br \/>        super()<br \/>        this.@author = author<br \/>        this.@title = title<br \/>        println(\"Finished constructor Book(String author, String title)\")<br \/>    }<br \/><br \/>    Book(String author, String title, int pageCount) {<br \/>        this.@author = author<br \/>        this.@title = title<br \/>        this.@pageCount = pageCount<br \/>        println(\"Finished constructor Book(String author, String title, int pageCount)\")<br \/>    }<br \/><br \/>    void logInjectedService() {<br \/>        println(\"    Service libraryService is injected? -> $libraryService\")<br \/>    }<br \/>}<\/code><\/pre>\n<p><\/noscript> <script src=\"https:\/\/gist.github.com\/3910934.js?file=LibraryService.groovy\"><\/script> <noscript><\/p>\n<pre><code>class LibraryService {<br \/>    def serviceMethod() {<br \/>    }<br \/>}<\/code><\/pre>\n<p><\/noscript> <\/p>\n<p><code>Book<\/code> has 4 explicit constructors. I want to check which constructor is injecting dependecies. This is my method that constructs <code>Book<\/code> objects and I called it in controller:  <script src=\"https:\/\/gist.github.com\/3910934.js?file=BookController.groovy\"><\/script> <noscript><\/p>\n<pre><code>class BookController {<br \/>    def index() {<br \/>        constructAndExamineBooks()<br \/>    }<br \/><br \/>    static constructAndExamineBooks() {<br \/>        println(\"Started constructAndExamineBooks\")<br \/>        Book book1 = new Book().logInjectedService()<br \/>        Book book2 = new Book(\"foo\").logInjectedService()<br \/>        Book book3 = new Book(\"foo\", 'bar').logInjectedService()<br \/>        Book book4 = new Book(\"foo\", 'bar', 100).logInjectedService()<br \/>        Book book5 = new Book(author: \"foo\", title: 'bar')<br \/>        println(\"Finished constructor Book(Map params)\")<br \/>        book5.logInjectedService()<br \/>    }<br \/>}<\/code><\/pre>\n<p><\/noscript> <\/p>\n<h3 id=\"analysis\">Analysis<\/h3>\n<p>Output looks like this:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/3910934.js?file=output.txt\"><\/script> <noscript><\/p>\n<pre><code>Started constructAndExamineBooks<br \/>Finished constructor Book()<br \/>    Service libraryService is injected? -> eu.spoonman.refaktor.LibraryService@2affcce2<br \/>Finished constructor Book()<br \/>Finished constructor Book(String author)<br \/>    Service libraryService is injected? -> eu.spoonman.refaktor.LibraryService@2affcce2<br \/>Finished constructor Book(String author, String title)<br \/>    Service libraryService is injected? -> null<br \/>Finished constructor Book(String author, String title, int pageCount)<br \/>    Service libraryService is injected? -> null<br \/>Finished constructor Book()<br \/>Finished constructor Book(Map params)<br \/>    Service libraryService is injected? -> eu.spoonman.refaktor.LibraryService@2affcce2<\/code><\/pre>\n<p><\/noscript> <\/p>\n<p>What do we see?<\/p>\n<ol>\n<li>\n<h4 id=\"\" style=\"float:right;color:#060\">&#10004;<\/h4>\n<p>Empty constructor injects dependencies.<\/li>\n<li style=\"clear:both\">\n<h4 id=\"-2\" style=\"float:right;color:#060\">&#10004;<\/h4>\n<p>Constructor that invokes empty constructor explicitly injects dependencies.<\/li>\n<li style=\"clear:both\">\n<h4 id=\"-3\" style=\"float:right;color:#600\">&#10008;<\/h4>\n<p>Constructor that invokes parent&#8217;s constructor explicitly <strong>does not inject dependencies.<\/strong><\/li>\n<li style=\"clear:both\">\n<h4 id=\"-4\" style=\"float:right;color:#600\">&#10008;<\/h4>\n<p>Constructor without any explicit call declared <strong>does not call empty constructor<\/strong> thus <strong>it does not inject dependencies<\/strong>. <\/p>\n<li style=\"clear:both\">\n<h4 id=\"-5\" style=\"float:right;color:#060\">&#10004;<\/h4>\n<p>Constructor provied by Grails with a map as a parameter invokes empty constructor and injects dependencies.<\/li>\n<\/ol>\n<h3 id=\"conclusion\">Conclusion<\/h3>\n<p><strong>Always explicitily invoke empty constructor in your Grail domain classes to ensure Dependency Injection!<\/strong> I didn&#8217;t know until today either!<\/p>\n","protected":false},"excerpt":{"rendered":"I&#8217;ve encountered strange behavior with a domain class in my project: services that should be injected were null. I&#8217;ve became suspicious as why is that? Services are injected properly in other domain classes so why this one is different?\nConstructors experiment\nI&#8217;ve created an experiment. I&#8217;ve created empty LibraryService that should be injected and Book domain class like this:\nclass Book {    def libraryService    String author    String title    int pageCount    Book() {        println(\"Finished constructor Book()\")    }    Book(String author) {        this()        this.@author = author        println(\"Finished constructor Book(String author)\")    }    Book(String author, String title) {        super()        this.@author = author        this.@title = title        println(\"Finished constructor Book(String author, String title)\")    }    Book(String author, String title, int pageCount) {        this.@author = author        this.@title = title        this.@pageCount = pageCount        println(\"Finished constructor Book(String author, String title, int pageCount)\")    }    void logInjectedService() {        println(\"    Service libraryService is injected? -&gt; $libraryService\")    }}\nclass LibraryService {    def serviceMethod() {    }}\nBook has 4 explicit constructors. I want to check which constructor is injecting dependecies. This is my method that constructs Book objects and I called it in controller:  \nclass BookController {    def index() {        constructAndExamineBooks()    }    static constructAndExamineBooks() {        println(\"Started constructAndExamineBooks\")        Book book1 = new Book().logInjectedService()        Book book2 = new Book(\"foo\").logInjectedService()        Book book3 = new Book(\"foo\", 'bar').logInjectedService()        Book book4 = new Book(\"foo\", 'bar', 100).logInjectedService()        Book book5 = new Book(author: \"foo\", title: 'bar')        println(\"Finished constructor Book(Map params)\")        book5.logInjectedService()    }}\nAnalysis\nOutput looks like this:\nStarted constructAndExamineBooksFinished constructor Book()    Service libraryService is injected? -&gt; eu.spoonman.refaktor.LibraryService@2affcce2Finished constructor Book()Finished constructor Book(String author)    Service libraryService is injected? -&gt; eu.spoonman.refaktor.LibraryService@2affcce2Finished constructor Book(String author, String title)    Service libraryService is injected? -&gt; nullFinished constructor Book(String author, String title, int pageCount)    Service libraryService is injected? -&gt; nullFinished constructor Book()Finished constructor Book(Map params)    Service libraryService is injected? -&gt; eu.spoonman.refaktor.LibraryService@2affcce2\nWhat do we see?\n\n\n&#10004;\nEmpty constructor injects dependencies.\n\n&#10004;\nConstructor that invokes empty constructor explicitly injects dependencies.\n\n&#10008;\nConstructor that invokes parent&#8217;s constructor explicitly does not inject dependencies.\n\n&#10008;\nConstructor without any explicit call declared does not call empty constructor thus it does not inject dependencies. \n\n&#10004;\nConstructor provied by Grails with a map as a parameter invokes empty constructor and injects dependencies.\n\nConclusion\nAlways explicitily invoke empty constructor in your Grail domain classes to ensure Dependency Injection! I didn&#8217;t know until today either!\n","protected":false},"author":37,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[50,42],"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11691"}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=11691"}],"version-history":[{"count":1,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11691\/revisions"}],"predecessor-version":[{"id":13243,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11691\/revisions\/13243"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=11691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=11691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=11691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}