Spring Framework : A Framework from SpringSource :: Spring Bean Wiring
What is Bean Wiring? |
Bean Wirings :
Bean wiring is the process of combining beans with Spring container. The required beans are to be informed to the container and how the container should use dependency injection to tie them together, at the time of wiring the beans.
There are two types of wiring
- Explicit Wiring
- Auto-wiring
Explicit Wiring |
Wiring one class object with the other class object explicitly is called explicit wiring.
Item.Java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
Supplier.java
package com.javaskool;
public class Supplier {
int supplierId;
String supplierName;
Item item;
public int getSupplierId() {
return supplierId;
}
public void setSupplierId(int supplierId) {
this.supplierId = supplierId;
}
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iobj" class="com.javaskool.Item">
<property name="itemId" value="20001"/>
<property name="itemName" value="Mouse"/>
<property name="qty" value="500"/>
</bean>
<bean id="sobj" class="com.javaskool.Supplier">
<property name="supplierId" value="1001"/>
<property name="supplierName" value="Logitech"/>
<property name="item" ref="iobj"></property>
</bean>
</beans>
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class TestDrive {
public static void main(String[] args)
{
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory beans=new XmlBeanFactory(res);
Supplier s=(Supplier)beans.getBean("sobj"); //getting Supplier Object from BeanFactory
System.out.println("SupplierId : "+s.getSupplierId());
System.out.println("SupplierName : "+s.getSupplierName());
System.out.println("ItemId : "+s.getItem().getItemId());
System.out.println("ItemName : "+s.getItem().getItemName());
System.out.println("Qty : "+s.getItem().getQty());
}
}
Output
SupplierId : 1001
SupplierName : Logitech
ItemId : 20001
ItemName : Mouse
Qty : 500
Auto Wiring |
AUTO WIRING
- We defined the relationships between the beans explicitly in terms of their names. There is an alternative. Some Spring bean factories are capable of something known as autowiring.
- Here a bean’s dependencies are not explicitly stated. Instead, the container will automatically determine the appropriate dependencies to inject.
- This sounds pretty clever, but in fact the mechanism used to determine which beans to inject into which dependency is quite straightforward.
- The properties available to be set on a bean can be determined by using normal Java reflection at runtime.
- You can choose to perform the autowiring on the basis of the property name, in which case a property called session would be populated from a bean definition also called session.
- You can alternatively choose to perform the autowiring on the basis of the property type-in which case a method called setFoo that took a Session property would be assigned the first bean defined as having type Session
- It really is that simple. An additional mode is defined that first tries to set the dependencies by name, and then resorts to setting them by type if no suitably named bean can be found.
If u want to achieve auto-wiring then go for an attribute called autowire in the <bean>tag.
There are four types of autowiring
- byType : If the autowire is byType then IOC will look what is the data-type of the derived attributes, then it will look for a matching bean id inside configuration file which is having the class value as the data-type of the derived attribute.
- byName : If the autowire is byName then IOC will look for a bean id which is matching the value with POJO class attribute name.
- Constructor : If the autowire type is constructor then IOC will look for a matching constructor inside the POJO class which is taking the derived data type as and attribute.
- autodetect : If the autowire type is autodetect then IOC will detect automatically for the next wiring class.
- no : by default
Auto Wiring : byType |
If the autowire is byType then IOC will look what is the data-type of the derived attributes, then it will look for a matching bean id inside configuration file which is having the class value as the data-type of the derived attribute.
Item.Java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
Supplier.java
package com.javaskool;
public class Supplier {
int supplierId;
String supplierName;
Item item;
public int getSupplierId() {
return supplierId;
}
public void setSupplierId(int supplierId) {
this.supplierId = supplierId;
}
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iobj" class="com.javaskool.Item">
<property name="itemId" value="20001"/>
<property name="itemName" value="Mouse"/>
<property name="qty" value="500"/>
</bean>
<bean id="sobj" class="com.javaskool.Supplier" autowire="byType">
<property name="supplierId" value="1001"/>
<property name="supplierName" value="Logitech"/>
</bean>
</beans>
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class TestDrive {
public static void main(String[] args)
{
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory beans=new XmlBeanFactory(res);
Supplier s=(Supplier)beans.getBean("sobj"); //getting Supplier Object from BeanFactory
System.out.println("SupplierId : "+s.getSupplierId());
System.out.println("SupplierName : "+s.getSupplierName());
System.out.println("ItemId : "+s.getItem().getItemId());
System.out.println("ItemName : "+s.getItem().getItemName());
System.out.println("Qty : "+s.getItem().getQty());
}
}
Output
SupplierId : 1001
SupplierName : Logitech
ItemId : 20001
ItemName : Mouse
Qty : 500
Auto Wiring : ByName |
If the autowire is byName then IOC will look for a bean id which is matching the value with POJO class attribute name.
Item.Java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
Supplier.java
package com.javaskool;
public class Supplier {
int supplierId;
String supplierName;
Item item;
public int getSupplierId() {
return supplierId;
}
public void setSupplierId(int supplierId) {
this.supplierId = supplierId;
}
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- id name should match with property name-->
<bean id="item" class="com.javaskool.Item">
<property name="itemId" value="20001"/>
<property name="itemName" value="Mouse"/>
<property name="qty" value="500"/>
</bean>
<bean id="sobj" class="com.javaskool.Supplier" autowire="byName">
<property name="supplierId" value="1001"/>
<property name="supplierName" value="Logitech"/>
</bean>
</beans>
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class TestDrive {
public static void main(String[] args)
{
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory beans=new XmlBeanFactory(res);
Supplier s=(Supplier)beans.getBean("sobj"); //getting Supplier Object from BeanFactory
System.out.println("SupplierId : "+s.getSupplierId());
System.out.println("SupplierName : "+s.getSupplierName());
System.out.println("ItemId : "+s.getItem().getItemId());
System.out.println("ItemName : "+s.getItem().getItemName());
System.out.println("Qty : "+s.getItem().getQty());
}
}
Output
SupplierId : 1001
SupplierName : Logitech
ItemId : 20001
ItemName : Mouse
Qty : 500
Auto Wiring : Constructor |
If the autowire type is constructor then IOC will look for a matching constructor inside the POJO class which is taking the derived data type as and attribute.
Item.Java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
Supplier.java
package com.javaskool;
public class Supplier {
int supplierId;
String supplierName;
Item item;
public Supplier(Item item) {
super();
this.item = item;
}
public int getSupplierId() {
return supplierId;
}
public void setSupplierId(int supplierId) {
this.supplierId = supplierId;
}
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iobj" class="com.javaskool.Item">
<property name="itemId" value="20001"/>
<property name="itemName" value="Mouse"/>
<property name="qty" value="500"/>
</bean>
<bean id="sobj" class="com.javaskool.Supplier" autowire="constructor">
<property name="supplierId" value="1001"/>
<property name="supplierName" value="Logitech"/>
</bean>
</beans>
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class TestDrive {
public static void main(String[] args)
{
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory beans=new XmlBeanFactory(res);
Supplier s=(Supplier)beans.getBean("sobj"); //getting Supplier Object from BeanFactory
System.out.println("SupplierId : "+s.getSupplierId());
System.out.println("SupplierName : "+s.getSupplierName());
System.out.println("ItemId : "+s.getItem().getItemId());
System.out.println("ItemName : "+s.getItem().getItemName());
System.out.println("Qty : "+s.getItem().getQty());
}
}
Output
SupplierId : 1001
SupplierName : Logitech
ItemId : 20001
ItemName : Mouse
Qty : 500
Auto Wiring : autodetect |
If the autowire type is autodetect then IOC will detect automatically for the next wiring class.
Item.Java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
Supplier.java
package com.javaskool;
public class Supplier {
int supplierId;
String supplierName;
Item item;
public int getSupplierId() {
return supplierId;
}
public void setSupplierId(int supplierId) {
this.supplierId = supplierId;
}
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iobj" class="com.javaskool.Item">
<property name="itemId" value="20001"/>
<property name="itemName" value="Mouse"/>
<property name="qty" value="500"/>
</bean>
<bean id="sobj" class="com.javaskool.Supplier" autowire="autodetect">
<property name="supplierId" value="1001"/>
<property name="supplierName" value="Logitech"/>
</bean>
</beans>
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class TestDrive {
public static void main(String[] args)
{
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory beans=new XmlBeanFactory(res);
Supplier s=(Supplier)beans.getBean("sobj"); //getting Supplier Object from BeanFactory
System.out.println("SupplierId : "+s.getSupplierId());
System.out.println("SupplierName : "+s.getSupplierName());
System.out.println("ItemId : "+s.getItem().getItemId());
System.out.println("ItemName : "+s.getItem().getItemName());
System.out.println("Qty : "+s.getItem().getQty());
}
}
Output
SupplierId : 1001
SupplierName : Logitech
ItemId : 20001
ItemName : Mouse
Qty : 500
Click Below to download the Examples
Recent Comments