{"id":3444,"date":"2019-05-25T05:42:39","date_gmt":"2019-05-25T05:42:39","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3444"},"modified":"2019-05-29T14:37:24","modified_gmt":"2019-05-29T14:37:24","slug":"core-spring-refresher","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/core-spring-refresher\/","title":{"rendered":"How, When, Why and What is Autowiring"},"content":{"rendered":"<p><strong class=\"ctaHeader3\">Why we need Dependency Injection?<\/strong><br \/>\nIts to overcome monotony behavior. If you are asking for Apple(from Apple.java) you would be served apple object. If you are asking for Mango(from Mango.java) you would be served mango object. Now in case, you are having Fruit and now you are asking Fruit(from Fruit.java) you would be served either apple or mango. Lets take a simple example as below<\/p>\n<ol>\n<li>I have a banking application where I have Account Interface with calculateIntereest Method<\/li>\n<li>I have different account types like SavingsAccount, PersonalLoanAccount and vehicleLoanAccount, HousingLoanAccount<\/li>\n<li>I have a Customer class where he would own a particular account type which wont be known until runtime<\/li>\n<\/ol>\n<p><strong>Accounts.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic interface Accounts \r\n{\r\n public Integer calculateInterest();\r\n}\r\n<\/pre>\n<p><strong>SavingsAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic class SavingsAccount implements Accounts \r\n{\r\n public Integer calculateInterest() \r\n {\r\n  return 8;\r\n }\r\n}\r\n<\/pre>\n<p><strong>Customer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic class Customer {\r\n Accounts accountType;\r\n\r\n public Customer(Accounts paccountType) {\r\n  this.accountType = paccountType;\r\n }\r\n\r\n public Accounts getAccountType() {\r\n  return accountType;\r\n }\r\n\r\n public void setAccountType(Accounts accountType) {\r\n  this.accountType = accountType;\r\n }\r\n}\r\n<\/pre>\n<p><strong>GetInterestFactory.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic class GetInterestFactory {\r\n Accounts objAccount = null;\r\n\r\n public static void main(String&#x5B;] args) {\r\n  GetInterestFactory objGetInterest = new GetInterestFactory();\r\n  objGetInterest.showInterestRate();\r\n }\r\n\r\n public void showInterestRate() {\r\n  String strAccType = &quot;Savings&quot;;\r\n\r\n  switch (strAccType) {\r\n   case &quot;Savings&quot;:\r\n    this.objAccount = new SavingsAccount();\r\n    break;\r\n   case &quot;Vehicle&quot;:\r\n    this.objAccount = new VehicleLoanAccount();\r\n    break;\r\n   default:\r\n    this.objAccount = new SavingsAccount();\r\n    break;\r\n  }\r\n\r\n  System.out.println(&quot;Interest Rate - &quot; + this.objAccount.calculateInterest());\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest Rate - 8\r\n<\/pre>\n<ol>\n<li>In the above java code we decide the Account Type only when the switch case is executed<\/li>\n<li>Until then the account type is kept as generic value using interface<\/li>\n<\/ol>\n<p>Now what spring does is the same code can be rewritten to  determine the Account Type during runtime in setters and constructors as below<\/p>\n<p><strong class=\"ctaHeader3\">Dependency Injection without Spring using Java Constructor<\/strong><\/p>\n<ol>\n<li>I have created a new class VehicleLoanAccount.java which has a different interest rate<\/li>\n<li>Now I am going to decide the account type in the Person.java in its constructor as below<\/li>\n<\/ol>\n<p><strong>VehicleLoanAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic class VehicleLoanAccount implements Accounts {\r\n public Integer calculateInterest() {\r\n  return 11;\r\n }\r\n\r\n}\r\n<\/pre>\n<p><em>Dependency Injection without Spring using Java Constructor<\/em><br \/>\n<strong>GetInterestConsWithoutSpring .java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic class GetInterestConsWithoutSpring {\r\n public static void main(String&#x5B;] args) {\r\n  Customer objPerson = new Customer(new SavingsAccount());\r\n  System.out.println(&quot;Interest Rate - &quot; + objPerson.getAccountType().calculateInterest());\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest Rate - 11\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">Dependency Injection without Spring using Java Setter<\/strong><\/p>\n<ol>\n<li>Now I am going to decide the account type in the GetInterest.java in its setter method <\/li>\n<li>I would be passing the value of the actual type inside the setter at runtime to decide the account type<\/li>\n<li>The Only thing which I have changed in the addition of new constructor to the Customer Class<\/li>\n<li>Setter method would be passed with specific account type during runtime<\/li>\n<\/ol>\n<p><strong>Customer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic class Customer {\r\n Accounts accountType;\r\n\r\n public Customer(Accounts paccountType) {\r\n  this.accountType = paccountType;\r\n }\r\n\r\n public Customer() {}\r\n\r\n public Accounts getAccountType() {\r\n  return accountType;\r\n }\r\n\r\n public void setAccountType(Accounts accountType) {\r\n  this.accountType = accountType;\r\n }\r\n}\r\n<\/pre>\n<p><em>Dependency Injection without Spring using Setter Method<\/em><br \/>\n<strong>GetInterestSettWithoutSpring .java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic class GetInterestSettWithoutSpring {\r\n public static void main(String&#x5B;] args) {\r\n  Customer customer = new Customer();\r\n  customer.setAccountType(new SavingsAccount());\r\n  System.out.println(&quot;Interest Rate - &quot; + customer.getAccountType().calculateInterest());\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest Rate - 8\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">Ways of Bean Injection using Spring<\/strong><br \/>\nIn Spring we can let the container create the bean in two ways<\/p>\n<ol>\n<li>Bean definition in XML<\/li>\n<li>Bean definition using @Component<\/li>\n<li>Bean definition using Java<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader3\">Bean definition in XML<\/strong><br \/>\n<em>beans.xml<\/em><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n\thttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\r\n\thttp:\/\/www.springframework.org\/schema\/context\r\n\thttp:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;constructor&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean  id=&quot;savingsType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n\t&lt;bean  id=&quot;vehicleLoanAccount&quot; class=&quot;com.mugil.core.VehicleLoanAccount&quot;\/&gt;\t\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p>\n<em>context:component-scan used for detecting bean<\/em><br \/>\n<strong class=\"ctaHeader3\">Bean definition using @Component<\/strong><br \/>\n<em>beans.xml<\/em><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n\thttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\r\n\thttp:\/\/www.springframework.org\/schema\/context\r\n\thttp:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd&quot;&gt;\r\n\t&lt;context:annotation-config \/&gt;\r\n\t&lt;context:component-scan base-package=&quot;com.mugil.core&quot;&gt;&lt;\/context:component-scan&gt;\t\t\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><em>@Component marking bean<\/em><br \/>\n<strong>Customer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.beans.factory.annotation.Qualifier;\r\n\r\n@Component\r\npublic class Customer \r\n{\r\n\r\n @Autowired\r\n @Qualifier(&quot;savings&quot;)\r\n Accounts accountType;\r\n.\r\n.\r\n.\r\n}\r\n<\/pre>\n<p><em>@Component marking bean<\/em><br \/>\n<strong>SavingsAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.beans.factory.annotation.Qualifier;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\n@Qualifier(&quot;savings&quot;)\r\npublic class SavingsAccount implements Accounts\r\n{\t\r\n\tpublic Integer calculateInterest() \r\n\t{\r\n\t\treturn 9;\r\n\t}\r\n}\r\n<\/pre>\n<p>\n<strong class=\"ctaHeader3\">Bean definition using Java<\/strong><\/p>\n<p><strong class=\"ctaHeader3\">Ways of Bean Injection using Spring<\/strong><br \/>\nNow, what if we do the same thing from XML and using annotations. Spring offers 3 ways by which dependency injection could be done<\/p>\n<ol>\n<li>XML\n<ol>\n<li><a href=\"#Constructor\">Constructor<\/a><\/li>\n<li><a href=\"#Setter\">Setter<\/a><\/li>\n<li>Autowiring\n<ol>\n<li><a href=\"#byType\">byType<\/a>\n    <\/li>\n<li><a href=\"#byName\">byName<\/a><\/li>\n<li><a href=\"#ConstructorAutoWire\">Constructor<\/a><\/li>\n<li><a href=\"#No\">No<\/a><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n<li>Annotation Based\n<ol>\n<li><a href=\"#byTypeAutowire\">byType<\/a>\n    <\/li>\n<li><a href=\"#byNameAutowire\">byName<\/a><\/li>\n<\/ol>\n<\/li>\n<li>Java Based<\/li>\n<\/ol>\n<p><strong><a name=\"Constructor\">Constructor Based &#8211; Dependency Injection using XML<\/a><\/strong><\/p>\n<ol>\n<li>In the below code beans are loaded when the application is deployed and the JVM starts<\/li>\n<li>The beans are uniquely identified using their IDS, in our case it is customer<\/li>\n<li>The parameter for constructor is defined inside constructor-arg in XML<\/li>\n<\/ol>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n   &lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot;&gt;\r\n      &lt;constructor-arg&gt;\r\n         &lt;bean id=&quot;savingAccount&quot; class=&quot;com.mugil.core.SavingsAccount&quot; \/&gt;\r\n      &lt;\/constructor-arg&gt;\r\n   &lt;\/bean&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>GetInterestConsWithSpring.xml<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\r\n\r\npublic class GetInterestConsWithSpring {\r\n public static void main(String&#x5B;] args) {\r\n  ApplicationContext context = new ClassPathXmlApplicationContext(&quot;SpringBeans.xml&quot;);\r\n\r\n  Customer customer = (Customer) context.getBean(&quot;customer&quot;);\r\n\r\n  System.out.println(&quot;Interest Rate - &quot; + customer.getAccountType().calculateInterest());\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest Rate - 8\r\n<\/pre>\n<p><strong><a name=\"Setter\">Setter Based &#8211; Dependency Injection using XML<\/a><\/strong><\/p>\n<ol>\n<li>For setter injection the only things we need to change is XML<\/li>\n<li>XML should be modified to take value by setter rather than constructor as before using property tag<\/li>\n<\/ol>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n   &lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot;&gt;\r\n      &lt;property name=&quot;accountType&quot; ref=&quot;savingAccount&quot; \/&gt;\r\n   &lt;\/bean&gt;\r\n   &lt;bean id=&quot;savingAccount&quot; class=&quot;com.mugil.core.SavingsAccount&quot; \/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>GetInterestSettWithSpring.xml<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\r\n\r\npublic class GetInterestSettWithSpring {\r\n public static void main(String&#x5B;] args) {\r\n  ApplicationContext context = new ClassPathXmlApplicationContext(\r\n   &quot;SpringBeans.xml&quot;);\r\n\r\n  Customer customer = (Customer) context.getBean(&quot;customer&quot;);\r\n\r\n  System.out.println(&quot;Interest Rate - &quot; + customer.getAccountType().calculateInterest());\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest Rate - 8\r\n<\/pre>\n<p><strong class=\"ctaHeader3\"><a name=\"byName\">Autowiring byName<\/a><\/strong><\/p>\n<ol>\n<li>In autoWiring byName the Name of the Instance Varable(accountType) and the ID of the bean in XML should be same<\/li>\n<li>If there is no bean matching the name is found it will throw NullPointerException<\/li>\n<\/ol>\n<p><strong>Customer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\npublic class Customer {\r\n Accounts accountType;\r\n\r\n public Customer(Accounts paccountType) {\r\n  this.accountType = paccountType;\r\n }\r\n\r\n public Customer() {}\r\n\r\n public Accounts getAccountType() {\r\n  return accountType;\r\n }\r\n\r\n public void setAccountType(Accounts accountType) {\r\n  this.accountType = accountType;\r\n }\r\n}\r\n<\/pre>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;byName&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;accountType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n\t&lt;bean id=&quot;vehicleLoanAccount&quot; class=&quot;com.mugil.core.VehicleLoanAccount&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest Rate - 9\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">What if Bean of Correct Name is notdefined in XML? Inour case it is account Type<\/strong><br \/>\n<strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;byName&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;savingAccount&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n\t&lt;bean id=&quot;vehicleLoanAccount&quot; class=&quot;com.mugil.core.VehicleLoanAccount&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nException in thread \"main\" java.lang.NullPointerException\r\n\tat com.mugil.core.GetInterestConstAutoWiringXML.main(GetInterestConstAutoWiringXML.java:13)\r\n<\/pre>\n<p><strong class=\"ctaHeader3\"><a name=\"byType\">Autowiring byType<\/a><\/strong><\/p>\n<ol>\n<li>In autoWiring is byType then there should be at least  one bean defined for the matching type, in our case it is Accounts<\/li>\n<li>If there is no bean defined of the type then it will throw null pointer exception<\/li>\n<li>If there is more than one matching bean of the same type is found it will throw No unique bean of type<\/li>\n<\/ol>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;byType&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;savingsType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest Rate - 9\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">What if No bean of right Type is defined in XML or More than one bean of same type defined?<\/strong><br \/>\n<strong>Beans.xml<\/strong><br \/>\nNo bean of right Type is defined in XML<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;byType&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nException in thread \"main\" java.lang.NullPointerException\r\n\tat com.mugil.core.GetInterestConstAutoWiringXML.main(GetInterestConstAutoWiringXML.java:13)\r\n<\/pre>\n<p><strong>Beans.xml<\/strong><br \/>\nMore than one bean of same type defined<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;byType&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;savingsType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n\t&lt;bean id=&quot;vehicleLoanAccount&quot; class=&quot;com.mugil.core.VehicleLoanAccount&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nException in thread \"main\" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customer' defined in class path resource [SpringBeans.xml]: Unsatisfied dependency expressed through bean property 'accountType': : No unique bean of type [com.mugil.core.Accounts] is defined: expected single matching bean but found 2: [accountType, vehicleLoanAccount]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.mugil.core.Accounts] is defined: expected single matching bean but found 2: [accountType, vehicleLoanAccount]\r\n<\/pre>\n<p><strong class=\"ctaHeader3\"><a name=\"ConstructorAutoWire\">Autowiring Contructor<\/a><\/strong><\/p>\n<ol>\n<li>In autoWiring using Constructor spring tries to find bean using type.In our case it is Account type<\/li>\n<li>If there is no bean defined of the type then spring will not guess and it will throw null pointer exception<\/li>\n<li>If there is more than one matching bean then spring will not guess and it will throw null pointer exception<\/li>\n<li>If there is more than one constructor spring wont guess the bean and it will throw null pointer exception<\/li>\n<\/ol>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;constructor&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;savingsType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest Rate - 9\r\n<\/pre>\n<p><strong>Beans.xml<\/strong><br \/>\nTwo bean of same type in constructor injection <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;constructor&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;savingsType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n&lt;bean id=&quot;vehicleLoanAccount&quot; class=&quot;com.mugil.core.VehicleLoanAccount&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Beans.xml<\/strong><br \/>\nNo bean of matching type in constructor injection <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;constructor&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nException in thread \"main\" java.lang.NullPointerException\r\n\tat com.mugil.core.GetInterestConstAutoWiringXML.main(GetInterestConstAutoWiringXML.java:13)\r\n<\/pre>\n<p><strong class=\"ctaHeader3\"><a name=\"no\">Autowiring using autodetect<\/a><\/strong><br \/>\nWhen the bean is configured to autowire by autodetect spring will attempt to autowire by constructor first.If no suitable constructor to bean is found then spring will attempt to autowire byType.<br \/>\n<strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;autodetect&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong class=\"ctaHeader3\"><a name=\"byTypeAutowire\">Annotation Based &#8211; byType<\/a><\/strong><\/p>\n<ol>\n<li>In Annotation based autowiring the beans would be injected by xml and would be available in container<\/li>\n<li>context:annotation-config is used to activate annotations in beans already registered in the application context (no matter if they were defined with XML or by package scanning)<\/li>\n<li>Now by using @Autowired tag we inject the bean as dependency where it is required.It is used either over variable in class or over setter or over constructor<\/li>\n<li>The default @Autowired decides the bean based on its type.If more than one bean if found it will throw no unique bean found exception.If no bean found it will throw nullpointer exception<\/li>\n<li>Incase of more than one bean of same type, we can narrow down the selection by using @Qualifier annotation and converting to byName @Autowiring<\/li>\n<\/ol>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n\thttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\r\n\thttp:\/\/www.springframework.org\/schema\/context\r\n\thttp:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd&quot;&gt;\r\n\r\n\t&lt;context:annotation-config \/&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;constructor&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;savingsType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Customer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\n\r\npublic class Customer \r\n{\r\n @Autowired\r\n private Accounts accountType;\r\n\r\n public Customer(Accounts paccountType) {\r\n  this.accountType = paccountType;\r\n }\r\n\r\n public Customer() {}\r\n\r\n public Accounts getAccountType() {\r\n  return accountType;\r\n }\r\n \r\n public void setAccountType(Accounts accountType) {\r\n  this.accountType = accountType;\r\n }\r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader3\"><a name=\"byNameAutowire\">Annotation Based &#8211; byName<\/a><\/strong><\/p>\n<ol>\n<li>In the below code we have two bean of same type<\/li>\n<li>Using @autowired would try to find bean byType.Since there are two beans it would throw no unique bean found exception<\/li>\n<li>Now we need to use @Qualifier passing the name (or) id of the bean as parameter<\/li>\n<li>Incase only Id of bean is there then same would be taken for name, If Name is there then name of bean would be given preference, incase no bean matches name then Id would be given preference<\/li>\n<\/ol>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n\thttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\r\n\thttp:\/\/www.springframework.org\/schema\/context\r\n\thttp:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd&quot;&gt;\r\n\r\n\t&lt;context:annotation-config \/&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;constructor&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean name=&quot;savings&quot; id=&quot;savingsType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n\t&lt;bean name=&quot;vehicle&quot; id=&quot;vehicleLoanAccount&quot; class=&quot;com.mugil.core.VehicleLoanAccount&quot;\/&gt;\t\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Customer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.beans.factory.annotation.Qualifier;\r\n\r\npublic class Customer \r\n{\r\n\r\n @Autowired\r\n @Qualifier(&quot;savings&quot;)\r\n Accounts accountType;\r\n.\r\n.\r\n.\r\n}\r\n<\/pre>\n<p>The below code will work by taking bean id into consideration despite the name doesn&#8217;t match.<br \/>\n<strong>Customer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.beans.factory.annotation.Qualifier;\r\n\r\npublic class Customer \r\n{\r\n\r\n @Autowired\r\n @Qualifier(&quot;savingsType&quot;)\r\n Accounts accountType;\r\n.\r\n.\r\n.\r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">What if there are two bean with the Same Name?<\/strong><br \/>\nIt will not throw exception during compilation but during runtime it will throw bean name already in use exception<br \/>\n<strong>Output<\/strong><\/p>\n<pre>\r\nException in thread \"main\" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name 'savings' is already used in this file\r\nOffending resource: class path resource [SpringBeans.xml]\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">What if there are two bean with the Same ID?<\/strong><\/p>\n<ol>\n<li>Eclipse will complain for violating ID should be unique and you are violating<\/li>\n<li>If you build the code still builds but when you run will endup with  Caused by: org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 69; cvc-id.2: There are multiple occurrences of ID value &#8216;savingsType&#8217;<\/li>\n<\/ol>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n\thttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\r\n\thttp:\/\/www.springframework.org\/schema\/context\r\n\thttp:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd&quot;&gt;\r\n\r\n\t&lt;context:annotation-config \/&gt;\r\n\t&lt;bean id=&quot;customer&quot; class=&quot;com.mugil.core.Customer&quot; autowire=&quot;constructor&quot;&gt;\t\t\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;savingsType&quot; class=&quot;com.mugil.core.SavingsAccount&quot;\/&gt;\r\n\t&lt;bean id=&quot;savingsType&quot; class=&quot;com.mugil.core.VehicleLoanAccount&quot;\/&gt;\t\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">Using @Component Scan to detect and load beans<\/strong><\/p>\n<ol>\n<li>We can make spring to detect beans on its own by using @component annotation rather then defining in XML with bean tags<\/li>\n<li>Using context:component-scan with base package pointed to beans package will load the bean marked with @component annotation<\/li>\n<li>If there is bean of one type then it would work fine during autowiring, if there is more than one bean of same type then we should uniquely identify the bean using @qualifier annotation<\/li>\n<li>@qualifier annotation should be used both in the place where the bean is referred and also in the place where it is defined.In our case it is Customer.java and SavingsAccount.java<\/li>\n<\/ol>\n<p><strong>Beans.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n\thttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\r\n\thttp:\/\/www.springframework.org\/schema\/context\r\n\thttp:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd&quot;&gt;\r\n\t&lt;context:annotation-config \/&gt;\r\n        &lt;context:component-scan base-package=&quot;com.mugil.core&quot;&gt;&lt;\/context:component-scan&gt;\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Customer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.beans.factory.annotation.Qualifier;\r\n\r\n@Component\r\npublic class Customer \r\n{\r\n\r\n @Autowired\r\n @Qualifier(&quot;savings&quot;)\r\n Accounts accountType;\r\n.\r\n.\r\n.\r\n}\r\n<\/pre>\n<p><strong>SavingsAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.core;\r\n\r\nimport org.springframework.beans.factory.annotation.Qualifier;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\n@Qualifier(&quot;savings&quot;)\r\npublic class SavingsAccount implements Accounts\r\n{\t\r\n\tpublic Integer calculateInterest() \r\n\t{\r\n\t\treturn 9;\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Why we need Dependency Injection? Its to overcome monotony behavior. If you are asking for Apple(from Apple.java) you would be served apple object. If you are asking for Mango(from Mango.java) you would be served mango object. Now in case, you are having Fruit and now you are asking Fruit(from Fruit.java) you would be served either&hellip; <a href=\"https:\/\/codethataint.com\/blog\/core-spring-refresher\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[188],"tags":[],"class_list":["post-3444","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3444"}],"version-history":[{"count":41,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3444\/revisions"}],"predecessor-version":[{"id":3486,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3444\/revisions\/3486"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}