What is Wiring in spring framework

What is Wiring in spring framework

  • Wiring is the process of injecting the dependencies of the bean.
  • Wiring can be done in two ways:
    • Explicit wiring
    • Implicit wiring or auto wiring

Explicit wiring:

  • In the case of explicit wiring, you have to specify the bean dependencies explicitly then the container will inject those dependencies.

<beans…>
<bean id=”ao” class=”…A”/>
<bean id=”bo” class=”…B”/>
<bean id=”hello” class=”..Hello>
<property name=”aobj” ref=”ao”/>
<property name=”bobj” ref=”bo”/>
</bean>
<beans>
Implicit wiring or Auto wiring:

  • In the case of Auto wiring, spring container can detect the bean dependencies automatically and injects those dependencies.<beans>
    <bean id=”ao” class=”..A”/>
    <bean id=”bo” class=”..B”/>
    <bean id=”hello” class=”..Hello” autowire=” xxx”/>
    </beans>

Following are possible values for an auto-wire attribute.

  1. byName
  2. byType
  3. constructor
  4. autodetect(Not in 3.0)

What is byName auto wiring:

  • The byname mode injects the object dependency according to the name of the bean in such case, property name and bean name must be same. It internally calls the setter method.
  • When an auto-wire attribute value is a byname then spring container checks whether any bean instance running in the container whose name (or id) is the same as bean property(variable) name or not.
  • When a bean is found with the matching name then it will be injected otherwise bean property remains uninjected.
  • Bean will be instantiated using the default constructor.
  • Dependent bean instances will be detected using a bean name.
  • Detected bean instance will be injected through setter injection only.

Example program (xadmin1) for the byName auto-wire. File Required:

  • xadmin.java
  • A.java
  • B.java
  • Hello.java
  • xadmin.xml

xadmin.java

package com.xadmin.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.classPathXmlApplicationContext;

public class xadmin
{
public static void main(String args[]);
ApplicationContext ctx= new ClassPathXmlApplicationContext(“xadmin.xml”);
Hello hello= (Hello) ctx.getBean(“hello”);
Hello.show();
}
}

A.java
package com.xadmin.spring;
public class A
{
private int a;
private string msg;
public void seta(int a)
{
This.a=a;
}
public void setMsg(string msh)
{
This.msg=msg;
}
public String toString()
{
Return “”+a+”\”+msg;
}
}

B.java
Package com.xadmin.spring;
public class B
{
private int b;
privatestring str;
public B(int b, string str){
This.b=b;
This.str=str;
}

public String toString()
{
return “”+b+”\t”+str;
}
}

Hello.java

package com.xadmin.spring;
public class Hello
{
private A aobj;
private B bobj;
public void setAobj(A aobj)
{
System.out.println(“Hello-setAobj()”);
This.aobj=aobj;
}
public void setBobj(B bobj)
{
System.out.println(“Hello-setBobj()”);
This.bobj=bobj;
}
public void show()
{
System.out.println(aobj);
System.out.println(bobj);
}
}

xadmin.xml

<beans>
<bean id=”aobj” class=”.com.xadmin.spring.A>
   <property name=”a” value=”10”/>
   <property name=”msg” value=”AAA”/>
</bean>
<bean id=”bobj” class=”.com.xadmin.spring.B>
   <constructor-arg value=”20”/>
  <constructor-arg value=”BBB”/>
</bean>
<bean id=”hello” class=”com.xadmin.spring.Hello” autowire=”byname”/>
</beans>

Explanation of the execution of the above program:
Container start-up steps:

  1. Loads A bean
  2. A bean instance will be created by calling the default constructor
  3. Calls setA() method to inject the value 10 into property a.
  4. Calls setMsg() method to inject the value AAA into property msg.
  5. Loads B bean
  6. B bean instance will be created by calling 2 arg constructor
  7. Loads Hello bean
  8. Hello, Bean instance will be created by calling the default constructor.
  9. Because of the byname auto-wire process, Container will do the following:
  •  Checks the dependencies available for Hello Bean
  • Currently, two dependencies are available for Hello bean called aobj and bobj.
  • Checks whether any bean is running in the container whose id is same as a property name.
  • Matching bean is found for aobj and bobj will be injected with that matching bean by calling setAobj() method.
  • Matching bean is not available for aobj and bobj, remains uninjected. i.e contains null only.

What is byType auto-wiring:

  • The byType mode injects the object dependency according to type. its property name and bean name can be different. It internally calls the setter method.
  • When an auto-wire attribute value is by Type then spring container checks whether any bean instance running in the container whose type is the same as bean property Data type or not.
  • In this you may get three cases:
    • When an exactly one bean is found with the matching data type then it will be injected. ( Refer xadmin2 example)
    • When no bean is found with the matching data type then bean property not be injected (Refer xadmin3 example)
    • When two or more beans are found with the matching data type the exception will be thrown called unsatisfiedDependedencyException (Refer xadmin4)
  • Bean will be instantiated using the default constructor
  • Dependent bean instance will be detected the using bean data type
  • Detected bean instances will be injected through setter injection only.

Example xadmin2:
File Required:

  • xadmin2.java
  • A.java
  • B.java
  • Hello.java
  • xadmin.xml

Refer xadmin2.java, A.java, B.java, and Hello.java same as the above xadmin1 example.
xadmin.xml

<beans>
<bean id=”ao” class=”.com.xadmin.spring.A>
   <property name=”a” value=”10”/>
   <property name=”msg” value=”AAA”/>
</bean>
<bean id=”bo” class=”.com.xadmin.spring.B>
   <constructor-arg value=”20”/>
  <constructor-arg value=”BBB”/>
</bean>
<bean id=”hello” class=”com.xadmin.spring.Hello” autowire=”byType”/>
</beans>

Example xadmin3:
File Required:

  • xadmin3.java
  • A.java
  • B.java
  • Hello.java
  • xadmin.xml

Refer xadmin3.java, A.java, B.java, and Hello.java same as the above xadmin1 example.
xadmin.xml

<beans>
<bean id=”hello” class=”com.xadmin.spring.Hello” autowire=”byType”/>
</beans>

Example xadmin4:
File Required:

  • xadmin4.java
  • A.java
  • B.java
  • Hello.java
  • xadmin.xml

Refer xadmin4.java, A.java, B.java, and Hello.java same as the above xadmin1 example.
xadmin.xml

<beans>
<bean id=”ao1” class=”.com.xadmin.spring.A>
   <property name=”a” value=”10”/>
   <property name=”msg” value=”AAA”/>
</bean>
<bean id=”ao2” class=”.com.xadmin.spring.A>
 <property name=”a” value=”10”/>
   <property name=”msg” value=”AAA”/>
</bean>
<bean id=”bo” class=”.com.xadmin.spring.B>
   <constructor-arg value=”20”/>
  <constructor-arg value=”BBB”/>
</bean>
<bean id=”hello” class=”com.xadmin.spring.Hello” autowire=”byType”/>
</beans>

What is constructor an auto-wiring:

  • The constructor mode injects the dependency by calling the constructor of the class.
  • When an auto-wire attribute value is a constructor then spring container checks whether any bean instance running in the container whose data type is the same as bean property data type or not.
  • Depending on the availability of bean instance, spring container identifies the matching constructor and invokes that constructor to inject the bean dependencies.
  • In this you may get three cases:
    • when no beans are found with the matching data type then bean property will not be injected.
    • When exactly one bean is found with the matching data type and without matching constructor then bean property will not be injected.
    • When exactly one bean is found with the matching data type and with matching constructor then it will be injected
    • When two or more beans are found with the matching data type then
      • the container will try to pick one bean from available multiple beans based on byName an auto-wire process first.
      • When one bean is not selected based on the byname auto-wire process then ignores multiple beans found with a given type.
  • Bean will be instantiated using a matching constructor.
  • Dependent bean instance will be detected using bean data type.
  • Detected bean instances will be injected through constructor injection

Autodetect(Not in 3.0)

  • Bean dependencies will be resolved through constructor or byType

Injecting subtypes into supertype reference:

  • When spring container is detecting the beans with the byType auto-wire process. It will find supertype and also subtype.

Consider the case:

class A()
class B extends A{ }
class Hello{
A aobj;
}

Case 1:

<bean id=”ao” class=”..A”/>
<bean id=”hello” class=”..Hello” autowire=”byType”/>
Ans: A instance available in spring container will be injected in to Hello.

Case 2:

<bean id=”ao” class=”..A”/>
<bean id=”bo” class=”..A”/>
<bean id=”hello” class=”..Hello” autowire=”byType”/>
Ans: It throws unsatisfied dependency exception
Two beans found with the A type one us ao and second is bo.

How to use P-Namespace(property Namespace)
P-Namespace is designed to reduce the size of the spring configuration document.
Public class Hello
{
……
}
class xadmin{
int a;
string str;
Hello hello;
}

Without p-Namespace:

<bean id=”hello” class=”….Hello/>
<bean id=”ao” class=”….A>
<property name=”a” value=”111”/>
<property name=”str” value=”I an Xadmin”/>
<property name=”hello” ref=”hello”/>
</bean>

With P-Namespace

<bean id=”ao” class=”….A” p:a=”111” p:str=”1 am xadmin” p:hello-ref=”hello”/>
Note: you must enable P-Namespace by adding the following in spring configuration document.
Xmlns:p=http://www.springframework.org/schema/p
Note: In Xmlns:p
P is namespace prefix which can be changed.
For example:
Xmlns:xadmin=http://www.springframework.org/schema/xadmin
<bean id=”ao” class=”….A” xadmin:a=”111” xadmin:str=”1 am xadmin” xadmin:hello-ref=”hello”/>

Some of Important interview Questions and Answers
Q1 What is wiring and how many ways are available?
Ans: Wiring is the process of injecting the dependencies of the bean. Wiring can be done into two ways: Explicit wiring and Implicit wiring or auto wiring

Q2. What is Auto Wiring?
Ans: In the case of Auto wiring, spring container can detect the bean dependencies automatically and injects those dependencies.

Q3. How can I implement XML based on auto-wiring?
Ans: Using auto wire attribute of bean tag.

Q4. What will happen when I specify the byname an auto-wire process?
Ans: The byname mode injects the object dependency according to the name of the bean in such case, property name and bean name must be same. It internally calls the setter method.

Q5. What will happen when I specify the byType an auto-wire process?
Ans: The byType mode injects the object dependency according to type. its property name and bean name can be different. It internally calls the setter method.

Q6. What will happen when I specify the constructor an auto-wire process?
Ans: The constructor mode injects the dependency by calling the constructor of the class.

Q7. What will happens with the following code?
public class Hai
{
int a;
String str;
public Hai(int a)
{
This.a=a;
}
public Hai(String str)
{
This.str=str;
}
<beans>
<bean id=”hai” class=”com.xadmin.spring.Hai”>
<constructor-arg value=”1234”/><bean>
<beans>
Ans: Create the Hai class instance by using matching constructor [Hai(String)]

Conclusion:
This article is aimed to provide a deeper knowledge of the wiring of the spring framework. we also learned the type of wiring we can perform in the spring with a full explanation of it. Please, feel free to drop a comment in the comment box. if have any doubt about the topic or you want to share more information about the topic. Happy Learning!

 

Leave a Reply

Your email address will not be published. Required fields are marked *