{"id":3941,"date":"2020-07-07T16:22:44","date_gmt":"2020-07-07T10:52:44","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3941"},"modified":"2025-12-15T05:26:42","modified_gmt":"2025-12-15T10:26:42","slug":"what-is-dependency-injection","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/what-is-dependency-injection\/","title":{"rendered":"What is Dependency Injection?"},"content":{"rendered":"\n<p>Dependency Injection is a pattern that is used to remove dependency from the programming code so that it becomes easy to manage and test the application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Dependency Lookup (DL): <\/strong><\/h2>\n\n\n\n<p>It is an approach where you get the resources after demand. There are various methods to get resources.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>To get the resource directly by calling a new keyword.<\/li><\/ol>\n\n\n\n<p>&nbsp;Example:&nbsp;<\/p>\n\n\n\n<p>A obj = new AImpl();<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Factory Method: By calling a static factory method.<\/li><\/ol>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>A obj = A.getA();&nbsp;&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Java Directory Naming Interface (JNDI):&nbsp;<\/li><\/ol>\n\n\n\n<p>Example:&nbsp;<\/p>\n\n\n\n<p>Context ctx = new InitialContext();&nbsp;&nbsp;<\/p>\n\n\n\n<p>Context environmentCtx = (Context) ctx.lookup(&#8220;java:comp\/env&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>A obj = (A)environmentCtx.lookup(&#8220;A&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Problems in Dependency Lookup:<\/strong><\/h2>\n\n\n\n<p>There are two main problems in this approach:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Tight coupling: If a resource is changed, a lot of modification is required in the programming code.<\/li><li>Not easy for testing: It creates a lot of problems while testing mainly in black-box testing.<\/li><\/ol>\n\n\n\n<p>Dependency Injections makes the code loosely coupled and easy for testing. An example is given below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Employee{&nbsp;&nbsp;\nAddress address;&nbsp;&nbsp;\nEmployee(Address address){&nbsp;&nbsp;\nthis.address=address;&nbsp;&nbsp;\n}&nbsp;&nbsp;\npublic void setAddress(Address address){&nbsp;&nbsp;\nthis.address=address;&nbsp;&nbsp;\n}&nbsp;\n}\n<\/pre>\n\n\n\n<p>There are two ways to perform dependency injection:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>By using Constructor<\/li><li>By using the Setter method<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Dependency Injection using Constructor: <\/strong><\/h2>\n\n\n\n<p>Here, the dependency can be injected using constructor and is done through the bean configuration file. The properties to be set are defined under &lt;constructor-arg&gt; tag in the bean configuration file.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Step 1: Book.java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Book {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;private int id;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;private String bookName;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public Book() {System.out.println(\"Java\");}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public Book(int id) {this.id = id;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public Book(String bookName) {&nbsp; this.bookName = bookName;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public Book(int id, String bookName) {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.id = id;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.bookName = bookName;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;void display(){&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(id+\" \"+bookName);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}\n<\/pre>\n\n\n\n<p>Step 2: applicationContext.java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&nbsp;&nbsp;\n&lt;beans&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;xmlns=\"https:\/\/www.springframework.org\/schema\/beans\"&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;xmlns:xsi=\"https:\/\/www.w3.org\/2001\/XMLSchema-instance\"&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;xmlns:p=\"https:\/\/www.springframework.org\/schema\/p\"&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;xsi:schemaLocation=\"https:\/\/www.springframework.org\/schema\/beans&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;https:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd\"&gt;&nbsp;&nbsp;\n&lt;bean id=\"book\" class=\"com.spring.example.Book\"&gt;&nbsp;&nbsp;\n&lt;constructor-arg value=\"1\" type=\"int\"&gt;&lt;\/constructor-arg&gt;&nbsp;&nbsp;\n&lt;\/bean&gt;&nbsp;&nbsp;\n&lt;\/beans&gt;\n<\/pre>\n\n\n\n<p>Step 3: Main.java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import org.springframework.beans.factory.BeanFactory;&nbsp;&nbsp;\nimport org.springframework.beans.factory.xml.XmlBeanFactory;&nbsp;&nbsp;\nimport org.springframework.core.io.*;&nbsp;&nbsp;\npublic class Main {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resource r=new ClassPathResource(\"applicationContext.xml\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BeanFactory factory=new XmlBeanFactory(r);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Book b=(Book)factory.getBean(\"book\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.display();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Dependency Injection Using Setter Method: <\/strong><\/h2>\n\n\n\n<p>Here, the dependency can be injected using setter\/getter methods and is done through the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-are-spring-bean-configurations\/\">bean configuration<\/a> file. The properties to be set are defined under the &lt;property&gt; tag in the bean configuration file.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Step 1: Book.java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Book {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;private int id;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;private String bookName;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;private String author;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public int getId() {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return id;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public void setId(int id) {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.id = id;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public String getBookName() {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return bookName;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public void setBookName(String bookName) {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.bookName = bookName;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public String getAuthor() {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return author;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public void setAuthor(String author) {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.author = author;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;void display(){&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(id+\" \"+bookName+\" \"+author);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}\n<\/pre>\n\n\n\n<p>Step 2: applicationContext.xml<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&nbsp;&nbsp;\n&lt;beans&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;xmlns=\"https:\/\/www.springframework.org\/schema\/beans\"&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;xmlns:xsi=\"https:\/\/www.w3.org\/2001\/XMLSchema-instance\"&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;xmlns:p=\"https:\/\/www.springframework.org\/schema\/p\"&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;xsi:schemaLocation=\"https:\/\/www.springframework.org\/schema\/beans&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;https:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd\"&gt;&nbsp;&nbsp;\n&lt;bean id=\"book\" class=\"com.spring.example.Book\"&gt;&nbsp;&nbsp;\n&lt;property name=\"id\"&gt;&nbsp;&nbsp;\n&lt;value&gt;1&lt;\/value&gt;&nbsp;&nbsp;\n&lt;\/property&gt;&nbsp;&nbsp;\n&lt;property name=\"bookName\"&gt;&nbsp;&nbsp;\n&lt;value&gt;The Complete Reference J2EE&lt;\/value&gt;&nbsp;&nbsp;\n&lt;\/property&gt;&nbsp;&nbsp;\n&lt;property name=\"author\"&gt;&nbsp;&nbsp;\n&lt;value&gt;Herbert Schildt&lt;\/value&gt;&nbsp;&nbsp;\n&lt;\/property&gt;&nbsp;&nbsp;\n&lt;\/bean&gt;&nbsp;&nbsp;\n&lt;\/beans&gt;\n<\/pre>\n\n\n\n<p>Step 3: Main.java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import org.springframework.beans.factory.BeanFactory;&nbsp;&nbsp;\nimport org.springframework.beans.factory.xml.XmlBeanFactory;&nbsp;&nbsp;\nimport org.springframework.core.io.*;&nbsp;&nbsp;\npublic class Main {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resource r=new ClassPathResource(\"applicationContext.xml\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BeanFactory factory=new XmlBeanFactory(r);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Book b=(Book)factory.getBean(\"book\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.display();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Difference between Setter Dependency Injection and Constructor Dependency Injection:<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Setter Dependency Injection (SDI)<\/td><td><a href=\"https:\/\/en.wikipedia.org\/wiki\/Dependency_injection\" rel=\"nofollow noopener\" target=\"_blank\">Constructor Dependency Injection<\/a> (CDI)<\/td><\/tr><tr><td>Readability is poor, as there is a lot of code in the application.<\/td><td>Readability is good as dependency is separately present in the code.<\/td><\/tr><tr><td>The bean should include setter and getter methods.<\/td><td>The bean should contain a matching constructor with argument; otherwise, BeanCreationException will be thrown.<\/td><\/tr><tr><td>It is preferred when properties are less and contain mutable objects.<\/td><td>It is preferred when properties are more and contain immutable objects.<\/td><\/tr><tr><td>There is a scope of circular dependency, and partial dependency is available.<\/td><td>There is no scope of circular dependency or partial dependency.<\/td><\/tr><tr><td>It requires @Autowired annotation to increase the coupling between the classes and DI containers.<\/td><td>It does not require the addition of @Autowired annotation.<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Dependency Injection is a pattern that is used to remove dependency from the programming code so that it becomes easy to manage and test the application. Dependency Lookup (DL): It is an approach where you get the resources after demand. There are various methods to get resources. To get the resource directly by calling a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3977,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[1075,1078,1076,1077],"class_list":["post-3941","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-constructor-dependency-injection","tag-dependency-lookup","tag-setter-dependency","tag-what-is-dependency-injection"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3941"}],"version-history":[{"count":1,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3941\/revisions"}],"predecessor-version":[{"id":32606,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3941\/revisions\/32606"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3977"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}