Assigning dependent
values to spring bean resources is technically known as "WIRING".
There are 2 types in wiring. They are
- Explicit wiring
- Auto wiring
Explicit wiring:
The setter
injection,constructor injections are considered as explicit wiring.
In this approach programmer has to pass/provide dependent value the spring
container to inject to the reference bean.
Auto wiring:
In this approach spring
container itself choose the dependent value to assian to resource. To let
Spring container do this we use '<autowire>' attribute in bean
tag.
There are 4 types of
auto wiring,
- byName
- byType
- constructor
- autodetect
byName:
In this type of auto
wiring the spring container assigns value to the dependent bean by
name of the bean property, i.e the id of the dependent
bean object and the name of the spring bean property must
be same.
In this approach spring
container use setter injection to assign value.
Look at the following
example ...how a java.util.Date value is assigning to the bean
in this example
beans.xml (spring
configuration file)
DemoBean.java (spring
bean)
MainApp.java (client
program)
DemoBean.java
//DemoBean.java
public class DemoBean{
private String uname;
private Date date;
/**
* @return the uname
*/
public String getUname() {
return uname;
}
/**
* @param uname the uname to set
*/
public void setUname(String uname) {
this.uname = uname;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
System.out.println("date: "+date);
}
}
MainApp.java
//MainApp.java
public
class MainApp{
public static void
main(String a[]){
FileSystemResource res=new
FileSystemResource("beans.xml");
XmlBeanFactory factory=new
XmlBeanFactory(res);
Demo
beanobj=(Demo)factory.getBean("demobean");
}
}
beans.xml
//beans.xml
<?xml
version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
............................>
<bean
id="demobean" class="DemoBean" autowire="byName">
<property name="uname" value="raju"/>
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
with the autowire="byName"
the spring container look for bean object which has 'date' as id (here
date is property name) if found assigns to the resource other wise no
value will be assigned.
In this type of auto
wiring the spring container assigns value to the dependent bean by
type(data type) of the bean property, i.e the type(data
type) of the dependent bean object and the type(data
type) of the spring bean property must be same.
In this approach spring
container use setter injection to assign value.
Look at the following
example ...how a java.util.Date value is assigning to the bean
in this example
beans.xml (spring
configuration file)
DemoBean.java (spring
bean)
MainApp.java (client
program)
DemoBean.java &
MainApp.java are same,but change in beans.xml
beans.xml
//beans.xml
<?xml
version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
............................>
<bean
id="demobean" class="DemoBean" autowire="byType">
<property name="uname" value="raju"/>
</bean>
<bean
id="date" class="java.util.Date"/>
</beans>
with the autowire="byType"
the spring container look for bean object 'java.util.Date' type if
found assigns to the resource other wise no value will be assigned.
If more than one
'java.util.Date' type object found in spring configuration file Exception
will be thrown by container.
In this type of auto
wiring the spring container assigns value to the dependent bean through parameterized constructor
of bean , i.e the the spring container calls
parameterized constructor
to assign dependent values to the resource.This is similar to byTtype mode.
Look at the following
example ...how a java.util.Date value is assigning to the bean
in this example
beans.xml (spring
configuration file)
DemoBean.java (spring
bean)
MainApp.java (client
program)
in this example
MainApp.java is same and changes are needed in beans.xml,
DemoBean.xml.
beans.xml
//beans.xml
<?xml
version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
............................>
<bean
id="demobean" class="DemoBean" autowire="constructor">
<property name="uname" value="raju"/>
</bean>
<bean
id="date" class="java.util.Date"/>
</beans>
DemoBean.java
//DemoBean.java
public class DemoBean{
private String uname;
private Date date;
/**
* @return the uname
*/
public String getUname() {
return uname;
}
/**
* @param uname the uname to set
*/
public void setUname(String uname) {
this.uname = uname;
System.out.println("uname:
"+uname);
}
/**
*constructor
**/
public DemoBean(Date d){
date=d;
System.out.println("date: "+date);
}
}
with the autowire="constructor"
the spring container look for bean object 'java.util.Date' type if
found assigns to the resource otherwise Exception will be thrown by container.
If more than one
'java.util.Date' type object found in spring configuration file Exception
will be thrown by container.
In this mode of auto
wiring spring container first try to assigns a value to dependent bean
using constructor mode if this mode is not possible then
container go to byType mode, i.e if we place a constructor in
bean class container choose constructor mode else you place setter
methods container use byType mode.
Lets take the same
example
beans.xml (spring
configuration file)
DemoBean.java (spring
bean)
MainApp.java (client
program)
in this example
MainApp.java is same and changes are needed in beans.xml,
DemoBean.xml.
beans.xml
//beans.xml
<?xml
version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
............................>
<bean
id="demobean" class="DemoBean" autowire="autodetect">
<property name="uname" value="raju"/>
</bean>
<bean
id="date" class="java.util.Date"/>
</beans>
DemoBean.java
//DemoBean.java
public class DemoBean{
private String uname;
private Date date;
/**
* @return the uname
1
*/
public String getUname() {
return uname;
}
/**
* @param uname the uname to set
*/
public void setUname(String uname) {
this.uname = uname;
System.out.println("uname:
"+uname);
}
/**
*constructor
**/
public DemoBean(Date d){
date=d;
System.out.println("constructor autowiring: "+date);
}
}
DemoBean.java
//DemoBean.java
public class DemoBean{
private String uname;
private Date date;
2
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param date the
date to set
*/
public void setDate(Date
date) {
System.out.println("setter method
autodetect: "+date);
this.date = date;
}
/**
* @return the uname
*/
public String getUname() {
return uname;
}
/**
* @param uname the uname to set
*/
public void setUname(String uname) {
this.uname = uname;
System.out.println("uname:
"+uname);
}
}
with 1 'DemoBean.java' spring container performs constructor based
auto wiring and
with 2 'DemoBean.java' spring container performs byType(setter
injection) based auto wiring.
if neither constructor
nor setter method found container don't assign a value.
Limitations of
Autowiring:
- Autowiring is possible only on reference type Bean
Propertied(we can inject only object,simple values can not be injected).
- There is chance of getting ambiguity.
- Kills readability of spring configuration file.