{"id":1889,"date":"2019-02-18T12:00:00","date_gmt":"2019-02-18T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/what-is-dependency-injection-with-java-examples\/"},"modified":"2026-07-05T03:22:15","modified_gmt":"2026-07-05T01:22:15","slug":"what-is-dependency-injection-with-java-examples","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/what-is-dependency-injection-with-java-examples\/","title":{"rendered":"What is Dependency Injection? (with java examples)"},"content":{"rendered":"<p>We are going to cover the concept of dependency injection (DI) in a very simple\u00a0 and clear manner. First let&#8217;s take a simple definition.<\/p>\n<p><strong>Content<\/strong><\/p>\n<ol>\n<li><a href=\"#t1\">What is Dependency Injection?<\/a><\/li>\n<li><a href=\"#t2\">Problem with Conventional Method<\/a><\/li>\n<li><a href=\"#t3\">Types of Dependency Injection<\/a><\/li>\n<li><a href=\"#t4\">How Dependency Injection Works<\/a><\/li>\n<li><a href=\"#t5\">Concept of Inversion of Control (IoC)<\/a><\/li>\n<li><a href=\"#t6\">Benefit of Dependency Injection<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4 id=\"t1\">1. What is Dependency Injection?<\/h4>\n<p>Dependency Injection is a design pattern whereon object provides another object with dependencies. So the easiest way to understand it to to use an example. But let&#8217;s first understand what dependency means.<\/p>\n<p>Well, dependency means something needs something else to function. For example, a computer needs a hard disk, keyboard, memory etc.<\/p>\n<p>The computer class is represented below.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">public <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Computer<\/span> {\n\tprivate HardDisk harddisk <span style=\"color: #333333;\">=<\/span> new HitachiHD();\n\tprivate RAM ram <span style=\"color: #333333;\">=<\/span> new SDRAM();\n\t\n\t<span style=\"color: #333333;\">\/\/<\/span>additional codes\n\n}\n<\/pre>\n<p style=\"text-align: center;\">Class with no Dependency Injection<\/p>\n<p>&nbsp;<\/p>\n<p>If you look at the computer class, you will notice that the the computer class creates its own HardDisk and RAM. You do this using the new keyword<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t2\">2. The Problem with Conventional\u00a0Method<\/h4>\n<p>The computer object uses the HitachiHD (it depends on the HitachiHD). Now assuming that in future we want to use SeagateHD. It means you need to recreate the class with the new SeagateHD. The could be very difficult if the class in not simple or there are many classes.<\/p>\n<p>This problem is solved by dependency injection. With Dependency injection, you don&#8217;t need to create this dependent classes inside the class. You simply get them. They are injected at runtime.<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t3\">3. Types of Dependency Injection<\/h4>\n<p>Basically, there are three types of dependency injection as outlined below.<\/p>\n<p><strong>a. Constructor Injection<\/strong><\/p>\n<p>In this case, the dependencies are provided through a class constructor<\/p>\n<p><strong>b. Setter Injection<\/strong><\/p>\n<p>In this type, the client provides a setter method that is used to inject the dependency into the class that requires it.<\/p>\n<p><strong>c. Interface Injection<\/strong><\/p>\n<p>Here, the dependency (dependent class) provides a method that can be called. The method takes the client that requires the dependency and provides the client with the required dependency.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Computer<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> HardDisk harddisk<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> RAM ram<span style=\"color: #333333;\">;<\/span>\n\t\n\t<span style=\"color: #888888;\">\/*<\/span>\n<span style=\"color: #888888;\">\t * With dependency injection, you don't need to instantiate<\/span>\n<span style=\"color: #888888;\">\t * the dependencies<\/span>\n<span style=\"color: #888888;\">\t * They are instantiated in another code and provided to this class<\/span>\n<span style=\"color: #888888;\">\t * when needed<\/span>\n<span style=\"color: #888888;\">\t *\/<\/span>\n\t\n\t\n\t<span style=\"color: #888888;\">\/\/Constructor based DI<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Computer<\/span><span style=\"color: #333333;\">(<\/span>HardDisk harddisk<span style=\"color: #333333;\">,<\/span> RAM ram<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">harddisk<\/span> <span style=\"color: #333333;\">=<\/span> harddisk<span style=\"color: #333333;\">;<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">ram<\/span> <span style=\"color: #333333;\">=<\/span> ram<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t\n\t<span style=\"color: #888888;\">\/\/Setter based DI<\/span>\n\t<span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setHardDisk<\/span><span style=\"color: #333333;\">(<\/span>HardDisk harddisk<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">harddisk<\/span> <span style=\"color: #333333;\">=<\/span> harddisk<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\n\t<span style=\"color: #888888;\">\/\/other codes\t<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p style=\"text-align: center;\">Class with Dependency Injection<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t4\">4. How\u00a0 Dependency Injection\u00a0 works<\/h4>\n<p>So you can see that in this class, there is new instantiation of new dependencies. It is then the work of DI to:<\/p>\n<ul>\n<li>Create (instantiate) new objects<\/li>\n<li>Determine which classes require these objects<\/li>\n<li>Provide them with these object when needed<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>If therefore, there are changes in these objects, you don&#8217;t have to re-write the class. Dependency Injection takes care of it! It doesn&#8217;t have to concern the class. The class just gets the updated object.<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t5\">5. Concept of Inversion of Control(IoC)<\/h4>\n<p>This is the principle behind dependency injection. It simply states that a class should not configure its dependencies statically (tight-coupling). The configuration should be done by another class from the outside (loose-coupling). So we are decoupling the class from it&#8217;s dependencies.<\/p>\n<p>According to this principle, a class should focus on performing required action and not on creating objects. Object creation should happen elsewhere.<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t6\">6. Some Benefits of Dependency Injection<\/h4>\n<p><b>Easier Code Maintainability:<\/b> this means that code can easily be extended without having to modify all the codes.<\/p>\n<p><strong>Boiler-plate Code Reduction<\/strong>: so code to instantiate new\u00a0 dependent objects are completed move away from the class.<\/p>\n<p><strong>Separation of Concerns<\/strong>: each code could concentrate on handling specific tasks<\/p>\n<p><strong>Easier Unit Testing:<\/strong> with DI, it is easier to test each component of the code. Mock objects could easily be used in place of other required code units.<\/p>\n<p>&nbsp;<\/p>\n<h4>Challenges of Dependency Injection<\/h4>\n<p><strong>Learning Curve<\/strong>: It take some time to learn and master the concept of DI<\/p>\n<p><strong>Overuse:<\/strong> Dependency Injection would actually not be very useful for very small applications. So if overused could add unnecessary complexity to the codes<\/p>\n<p><strong>Runtime Errors:<\/strong> Errors that could have been trapped in compile time are shifted to run-time<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are going to cover the concept of dependency injection (DI) in a very simple\u00a0 and clear manner. First let&#8217;s take a simple definition. Content &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-1889","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1889"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1889\/revisions"}],"predecessor-version":[{"id":2057,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1889\/revisions\/2057"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}