Spring auto-wiring mode byType
If you specify auto-wiring by type, then the injection will be based on data type of the bean and should be compatible with
assigning bean property data type.
In spring, auto-wiring by type allows a property to be autowired if there is exactly one bean of the property type in the
container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that
bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting the dependency-check=
"objects" attribute value specifies that an error should be thrown in this case.
package com.java2novice.beans;
public class PaymentGateway {
private Order order;
public void setOrder(Order ord){
this.order = ord;
}
public Order getOrder() {
return order;
}
@Override
public String toString(){
return "ordering "+this.order.getItem()+" | price: "+this.order.getPrice();
}
}
|
package com.java2novice.beans;
public class Order {
private String item;
private String price;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
|
Here is the xml based configuration file, which shows normal way of injecting dependencies:
<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-3.0.xsd">
<bean id="paymentGateway" class="com.java2novice.beans.PaymentGateway" >
<property name="order" ref="orderBean" />
</bean>
<bean id="orderBean" class="com.java2novice.beans.Order" >
<property name="item" value="Java Book" />
<property name="price" value="RS 225" />
</bean>
</beans>
|
Here is the xml based configuration file, which shows injection by type using auto-wiring. We are using bean property
called autowire to specify auto-wire by type:
<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-3.0.xsd">
<bean id="paymentGateway" class="com.java2novice.beans.PaymentGateway"
autowire="byType" />
<bean id="orderBean" class="com.java2novice.beans.Order" >
<property name="item" value="Java Book" />
<property name="price" value="RS 225" />
</bean>
</beans>
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.PaymentGateway;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
PaymentGateway paymentGateway = (PaymentGateway) context.getBean("paymentGateway");
System.out.println(paymentGateway.toString());
}
}
|
|