{"id":31210,"date":"2014-10-07T07:00:41","date_gmt":"2014-10-07T04:00:41","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=31210"},"modified":"2014-10-06T20:24:06","modified_gmt":"2014-10-06T17:24:06","slug":"di-containers-are-code-polluters","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html","title":{"rendered":"DI Containers are Code Polluters"},"content":{"rendered":"<p>While <a href=\"http:\/\/martinfowler.com\/articles\/injection.html\">dependency injection<\/a> (aka, &#8220;DI&#8221;) is a natural technique of composing objects in OOP (known long before the term was <a href=\"http:\/\/www.martinfowler.com\/articles\/injection.html\">introduced by Martin Fowler<\/a>), <a href=\"http:\/\/www.spring.io\">Spring IoC<\/a>, <a href=\"https:\/\/code.google.com\/p\/google-guice\/\">Google Guice<\/a>, <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/tutorial\/doc\/giwhl.html\">Java EE6 CDI<\/a>, <a href=\"http:\/\/square.github.io\/dagger\/\">Dagger<\/a> and other <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dependency_injection\">DI frameworks<\/a> turn it into an anti-pattern.<\/p>\n<p>I&#8217;m not going to discuss obvious arguments against &#8220;setter injections&#8221; (like in <a href=\"http:\/\/www.springbyexample.org\/examples\/intro-to-ioc-basic-setter-injection.html\">Spring IoC<\/a>) and &#8220;field injections&#8221; (like in <a href=\"http:\/\/picocontainer.codehaus.org\/annotated-field-injection.html\">PicoContainer<\/a>). These mechanisms simply violate basic principles of object-oriented programming and encourage us to create incomplete, mutable objects, that get stuffed with data during the course of application execution. Remember: ideal objects <a href=\"%7B%20%%20post_url%202014\/jun\/2014-06-09-objects-should-be-immutable%20%%7D\">must be immutable<\/a> and <a href=\"%7B%20%%20post_url%202014\/sep\/2014-09-16-getters-and-setters-are-evil%20%%7D\">may not contain setters<\/a>.<\/p>\n<p>&nbsp;<br \/>\nInstead, let&#8217;s talk about &#8220;constructor injection&#8221; (like in <a href=\"https:\/\/github.com\/google\/guice\/wiki\/Injections#constructor-injection\">Google Guice<\/a>) and its use with <strong>dependency injection containers<\/strong>. I&#8217;ll try to show why I consider these containers a redundancy, at least.<\/p>\n<h2>What is Dependency Injection?<\/h2>\n<p>This is what dependency injection is (not really different from a plain old object composition):<\/p>\n<pre class=\" brush:java\">public class Budget {\r\n  private final DB db;\r\n  public Budget(DB data) {\r\n    this.db = data;\r\n  }\r\n  public long total() {\r\n    return this.db.cell(\r\n      \"SELECT SUM(cost) FROM ledger\"\r\n    );\r\n  }\r\n}<\/pre>\n<p>The object <code>data<\/code> is called a &#8220;dependency&#8221;.<\/p>\n<p>A <code>Budget<\/code> doesn&#8217;t know what kind of database it is working with. All it needs from the database is its ability to fetch a cell, using an arbitrary SQL query, via method <code>cell()<\/code>. We can instantiate a <code>Budget<\/code> with a PostgreSQL implementation of the <code>DB<\/code> interface, for example:<\/p>\n<pre class=\" brush:java\">public class App {\r\n  public static void main(String... args) {\r\n    Budget budget = new Budget(\r\n      new Postgres(\"jdbc:postgresql:5740\/main\")\r\n    );\r\n    System.out.println(\"Total is: \" + budget.total());\r\n  }\r\n}<\/pre>\n<p>In other words, we&#8217;re &#8220;injecting&#8221; a dependency into a new object <code>budget<\/code>.<\/p>\n<p>An alternative to this &#8220;dependency injection&#8221; approach would be to let <code>Budget<\/code> decide what database it wants to work with:<\/p>\n<pre class=\" brush:java\">public class Budget {\r\n  private final DB db = new Postgres(\"jdbc:postgresql:5740\/main\");\r\n  \/\/ class methods\r\n}<\/pre>\n<p>This is very dirty and leads to 1) code duplication, 2) inability to reuse, and 3) inability to test, etc. No need to discuss why. It&#8217;s obvious.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Thus, dependency injection via a constructor is an amazing technique. Well, not even a technique, really. More like a feature of Java and all other object-oriented languages. It&#8217;s expected that almost any object will want to encapsulate some knowledge (aka, a &#8220;state&#8221;). That&#8217;s what constructors are for.<\/p>\n<h2>What is a DI Container?<\/h2>\n<p>So far so good, but here comes the dark side \u2014 a dependency injection container. Here is how it works (let&#8217;s use Google Guice as an example):<\/p>\n<pre class=\" brush:java\">import javax.inject.Inject;\r\npublic class Budget {\r\n  private final DB db;\r\n  @Inject\r\n  public Budget(DB data) {\r\n    this.db = data;\r\n  }\r\n  \/\/ same methods as above\r\n}<\/pre>\n<p>Pay attention: the constructor is annotated with <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/inject\/Inject.html\"><code>@Inject<\/code><\/a>.<\/p>\n<p>Then, we&#8217;re supposed to configure a container somewhere, when the application starts:<\/p>\n<pre class=\" brush:java\">Injector injector = Guice.createInjector(\r\n  new AbstractModule() {\r\n    @Override\r\n    public void configure() {\r\n      this.bind(DB.class).toInstance(\r\n        new Postgres(\"jdbc:postgresql:5740\/main\")\r\n      );\r\n    }\r\n  }\r\n);<\/pre>\n<p>Some frameworks even allow us to configure the injector in an XML file.<\/p>\n<p>From now on, we are not allowed to instantiate <code>Budget<\/code> through the <code>new<\/code> operator, like we did before. Instead, we should use the injector we just created:<\/p>\n<pre class=\" brush:java\">public class App {\r\n  public static void main(String... args) {\r\n    Injection injector = \/\/ as we just did in the previous snippet\r\n    Budget budget = injector.getInstance(Budget.class);\r\n    System.out.println(\"Total is: \" + budget.total());\r\n  }\r\n}<\/pre>\n<p>The injection automatically finds out that in order to instantiate a <code>Budget<\/code> it has to provide an argument for its constructor. It will use an instance of class <code>Postgres<\/code>, which we instantiated in the injector.<\/p>\n<p>This is the right and recommended way to use Guice. There are a few even darker patterns, though, which are possible but not recommended. For example, you can make your injector a singleton and use it right inside the <code>Budget<\/code> class. These mechanisms are considered wrong even by DI container makers, however, so let&#8217;s ignore them and focus on the recommended scenario.<\/p>\n<h2>What Is This For?<\/h2>\n<p>Let me reiterate and summarize the scenarios of <strong>incorrect usage<\/strong> of dependency injection containers:<\/p>\n<ul>\n<li>Field injection<\/li>\n<li>Setter injection<\/li>\n<li>Passing injector as a dependency<\/li>\n<li>Making injector a global singleton<\/li>\n<\/ul>\n<p>If we put all of them aside, all we have left is the constructor injection explained above. And how does that help us? Why do we need it? Why can&#8217;t we use plain old <code>new<\/code> in the main class of the application?<\/p>\n<p>The container we created simply adds more lines to the code base, or even more files, if we use XML. And it doesn&#8217;t add anything, except an additional complexity. We should always remember this if we have the question: &#8220;What database is used as an argument of a Budget?&#8221;<\/p>\n<h2>The Right Way<\/h2>\n<p>Now, let me show you a real life example of using <code>new<\/code> to construct an application. This is how we create a &#8220;thinking engine&#8221; in <a href=\"http:\/\/www.rultor.com\">rultor.com<\/a> (full class is in <a href=\"https:\/\/github.com\/yegor256\/rultor\/blob\/1.34\/src\/main\/java\/com\/rultor\/agents\/Agents.java\"><code>Agents.java<\/code><\/a>):<\/p>\n<pre class=\" brush:java\">final Agent agent = new Agent.Iterative(\r\n  new Array(\r\n    new Understands(\r\n      this.github,\r\n      new QnSince(\r\n        49092213,\r\n        new QnReferredTo(\r\n          this.github.users().self().login(),\r\n          new QnParametrized(\r\n            new Question.FirstOf(\r\n              new Array(\r\n                new QnIfContains(\"config\", new QnConfig(profile)),\r\n                new QnIfContains(\"status\", new QnStatus(talk)),\r\n                new QnIfContains(\"version\", new QnVersion()),\r\n                new QnIfContains(\"hello\", new QnHello()),\r\n                new QnIfCollaborator(\r\n                  new QnAlone(\r\n                    talk, locks,\r\n                    new Question.FirstOf(\r\n                      new Array(\r\n                        new QnIfContains(\r\n                          \"merge\",\r\n                          new QnAskedBy(\r\n                            profile,\r\n                            Agents.commanders(\"merge\"),\r\n                            new QnMerge()\r\n                          )\r\n                        ),\r\n                        new QnIfContains(\r\n                          \"deploy\",\r\n                          new QnAskedBy(\r\n                            profile,\r\n                            Agents.commanders(\"deploy\"),\r\n                            new QnDeploy()\r\n                          )\r\n                        ),\r\n                        new QnIfContains(\r\n                          \"release\",\r\n                          new QnAskedBy(\r\n                            profile,\r\n                            Agents.commanders(\"release\"),\r\n                            new QnRelease()\r\n                          )\r\n                        )\r\n                      )\r\n                    )\r\n                  )\r\n                )\r\n              )\r\n            )\r\n          )\r\n        )\r\n      )\r\n    ),\r\n    new StartsRequest(profile),\r\n    new RegistersShell(\r\n      \"b1.rultor.com\", 22,\r\n      \"rultor\",\r\n      IOUtils.toString(\r\n        this.getClass().getResourceAsStream(\"rultor.key\"),\r\n        CharEncoding.UTF_8\r\n      )\r\n    ),\r\n    new StartsDaemon(profile),\r\n    new KillsDaemon(TimeUnit.HOURS.toMinutes(2L)),\r\n    new EndsDaemon(),\r\n    new EndsRequest(),\r\n    new Tweets(\r\n      this.github,\r\n      new OAuthTwitter(\r\n        Manifests.read(\"Rultor-TwitterKey\"),\r\n        Manifests.read(\"Rultor-TwitterSecret\"),\r\n        Manifests.read(\"Rultor-TwitterToken\"),\r\n        Manifests.read(\"Rultor-TwitterTokenSecret\")\r\n      )\r\n    ),\r\n    new CommentsTag(this.github),\r\n    new Reports(this.github),\r\n    new RemovesShell(),\r\n    new ArchivesDaemon(\r\n      new ReRegion(\r\n        new Region.Simple(\r\n          Manifests.read(\"Rultor-S3Key\"),\r\n          Manifests.read(\"Rultor-S3Secret\")\r\n        )\r\n      ).bucket(Manifests.read(\"Rultor-S3Bucket\"))\r\n    ),\r\n    new Publishes(profile)\r\n  )\r\n);<\/pre>\n<p>Impressive? This is a true object composition. I believe this is how a proper object-oriented application should be instantiated.<\/p>\n<p>And DI containers? In my opinion, they just add unnecessary noise.<\/p>\n<h2>Related Posts<\/h2>\n<p>You may also find these posts interesting:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2014\/09\/getterssetters-evil-period.html\">Getters\/Setters. Evil. Period.<\/a><\/li>\n<li><a href=\"\/2014\/09\/10\/anti-patterns-in-oop.html\">Anti-Patterns in OOP<\/a><\/li>\n<li><a href=\"\/2014\/06\/19\/avoid-string-concatenation.html\">Avoid String Concatenation<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2014\/09\/objects-should-be-immutable.html\">Objects Should Be Immutable<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2014\/09\/why-null-is-bad.html\">Why NULL is Bad?<\/a><\/li>\n<\/ul>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/www.yegor256.com\/2014\/10\/03\/di-containers-are-evil.html\">DI Containers are Code Polluters<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\/\">JCG partner<\/a> Yegor Bugayenko at the <a href=\"http:\/\/www.yegor256.com\/\">About Programming<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>While dependency injection (aka, &#8220;DI&#8221;) is a natural technique of composing objects in OOP (known long before the term was introduced by Martin Fowler), Spring IoC, Google Guice, Java EE6 CDI, Dagger and other DI frameworks turn it into an anti-pattern. I&#8217;m not going to discuss obvious arguments against &#8220;setter injections&#8221; (like in Spring IoC) &hellip;<\/p>\n","protected":false},"author":593,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[124],"class_list":["post-31210","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-dependency-injection"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>DI Containers are Code Polluters<\/title>\n<meta name=\"description\" content=\"While dependency injection (aka, &quot;DI&quot;) is a natural technique of composing objects in OOP (known long before the term was introduced by Martin Fowler),\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"DI Containers are Code Polluters\" \/>\n<meta property=\"og:description\" content=\"While dependency injection (aka, &quot;DI&quot;) is a natural technique of composing objects in OOP (known long before the term was introduced by Martin Fowler),\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/yegor256\" \/>\n<meta property=\"article:published_time\" content=\"2014-10-07T04:00:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yegor Bugayenko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/yegor256\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yegor Bugayenko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html\"},\"author\":{\"name\":\"Yegor Bugayenko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f3ff9c97ecd948f271ebc5ead401d02d\"},\"headline\":\"DI Containers are Code Polluters\",\"datePublished\":\"2014-10-07T04:00:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html\"},\"wordCount\":730,\"commentCount\":19,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"Dependency Injection\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html\",\"name\":\"DI Containers are Code Polluters\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2014-10-07T04:00:41+00:00\",\"description\":\"While dependency injection (aka, \\\"DI\\\") is a natural technique of composing objects in OOP (known long before the term was introduced by Martin Fowler),\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/di-containers-are-code-polluters.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"DI Containers are Code Polluters\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f3ff9c97ecd948f271ebc5ead401d02d\",\"name\":\"Yegor Bugayenko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"caption\":\"Yegor Bugayenko\"},\"description\":\"Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.\",\"sameAs\":[\"http:\\\/\\\/www.yegor256.com\\\/\",\"https:\\\/\\\/www.facebook.com\\\/yegor256\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/yegor256\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/yegor256\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yegor-bugayenko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"DI Containers are Code Polluters","description":"While dependency injection (aka, \"DI\") is a natural technique of composing objects in OOP (known long before the term was introduced by Martin Fowler),","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html","og_locale":"en_US","og_type":"article","og_title":"DI Containers are Code Polluters","og_description":"While dependency injection (aka, \"DI\") is a natural technique of composing objects in OOP (known long before the term was introduced by Martin Fowler),","og_url":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/yegor256","article_published_time":"2014-10-07T04:00:41+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Yegor Bugayenko","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/yegor256","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yegor Bugayenko","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html"},"author":{"name":"Yegor Bugayenko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f3ff9c97ecd948f271ebc5ead401d02d"},"headline":"DI Containers are Code Polluters","datePublished":"2014-10-07T04:00:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html"},"wordCount":730,"commentCount":19,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["Dependency Injection"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html","url":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html","name":"DI Containers are Code Polluters","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2014-10-07T04:00:41+00:00","description":"While dependency injection (aka, \"DI\") is a natural technique of composing objects in OOP (known long before the term was introduced by Martin Fowler),","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/di-containers-are-code-polluters.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"DI Containers are Code Polluters"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f3ff9c97ecd948f271ebc5ead401d02d","name":"Yegor Bugayenko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","caption":"Yegor Bugayenko"},"description":"Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.","sameAs":["http:\/\/www.yegor256.com\/","https:\/\/www.facebook.com\/yegor256","https:\/\/www.linkedin.com\/in\/yegor256","https:\/\/x.com\/https:\/\/twitter.com\/yegor256"],"url":"https:\/\/www.javacodegeeks.com\/author\/yegor-bugayenko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/31210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/593"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=31210"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/31210\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=31210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=31210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=31210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}