Saturday, March 26, 2016

Spring Autowire

Spring IoC container can autowire relationships between beans.

<bean id="owner" class="com.gvace.autowire.Owner" autowire="byName">
...
</bean>

autowire modes
  1. no
  2. byName
  3. byType
  4. constructor
  5. autodetect
  6. default
attributes
  1. primary
  2. autowire-candidate


Example

public class Owner {
 private String name;
 private Dog dog;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Dog getDog() {
  return dog;
 }
 public void setDog(Dog dog) {
  this.dog = dog;
 }
 
}
public class Dog {
 private String name;
 private int age;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
}


no
Default is no
No autowire

byName

Since owner's property name is "dog", Spring will look for bean with id "dog"


<bean id="owner" class="com.gvace.autowire.Owner" autowire="byName">
 <property name="name" value="ownerA"></property>
</bean>
<bean id="dog" class="com.gvace.autowire.Dog">
 <property name="name" value="dogB"></property>
 <property name="age" value="3"></property>
</bean> 
<bean id="dog1" class="com.gvace.autowire.Dog">
 <property name="name" value="dogA"></property>
 <property name="age" value="3"></property>
</bean> 



byType

Spring look for a bean with target wiring type or its child
But only accept one bean with this type/child in the config
More than 1 bean with the same type/child to wire will cause error.

<bean id="owner" class="com.gvace.autowire.Owner" autowire="byType">
 <property name="name" value="ownerA"></property>
</bean>
<bean id="dog" class="com.gvace.autowire.Dog">
 <property name="name" value="dogB"></property>
 <property name="age" value="3"></property>
</bean> 
<!-- byType does not allow more than 1 bean with the same type to wire
<bean id="dog1" class="com.gvace.autowire.Dog">
 <property name="name" value="dogA"></property>
 <property name="age" value="100"></property>
</bean>
-->



constructor

If Spring found there is a constructor with the property/properties
And each parameter of the constructor has a bean to map by same property type
It will pick the constructor.
The picking rule is look for the constructor with the most parameters.

Will cause error if did not found or found more than 1 bean with the same type

example:

Add a constructor to Owner

 
 public Owner(Dog dog) {
  super();
  this.dog = dog;
 }



<bean id="owner" class="com.gvace.autowire.Owner" autowire="constructor">
 <property name="name" value="ownerA"></property>
</bean>
<bean id="dog" class="com.gvace.autowire.Dog">
 <property name="name" value="dogB"></property>
 <property name="age" value="3"></property>
</bean> 



autodetect

choose one between byName, byType, and constructor

Look sequence:

constructor > byType > byName



default

Follow the config of its parent: <beans default-autowire="byName">
If <beans default-autowire="no"> is not set, it's the same as <beans default-autowire="no">



Attributes

1. primary: Default: primary="true", If only one is set to primary="true", use this as wire property
So normally we do primary="false" to those are not first choice.

2. autowire-candidate="false": never see this bean as auto-wire choice.








No comments:

Post a Comment