Spring Auto Wiring
Spring Auto Wiring
Autowiring is a concept of injecting a spring bean automatically without writing <ref/> tag by
programmer. Programmer does not required to write explicitly Dependency Injection.
1) XML based
2) Annotation Based
a) byName : compares the spring bean (java code) filed name (variable name) and configuration
code (XML file) bean name (bean tag name) , if they are matched then automatically those
objects will b bonded with each other using setter injection.
b) byType: It compares bean class variable Data type and spring bean class type. If both are
matched then it will do setter injection.
c) constructor: It check for parameterized constructor for creating object with reference type as
parameter. If not found then uses default constructor at last. Always checks More number of
parameters first, if not matched then next level less no of parameters constructor will be
compared, and then goes on up to default constructor.
f) autodetect (works only in older versions like 2.X) : It works like byType if default constructor
is available in spring bean, if not works like constructor if parameterized constructor is
available.
ex: consider the below example for above concept. Employee class has a Address class
Dependency.
Spring Autowiring (XML and Annotations)
Java code:
package com.app;
}
Spring Autowiring (XML and Annotations)
=====
package com.app;
@Override
public String toString() {
return "Employee [addr=" + addr + "]";
}
======
code byType:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
Spring Autowiring (XML and Annotations)
</bean>
</beans>
=========
code byName:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
</bean>
</beans>
========
constructor:
to use this option class must have at least one parameterized constructor.
Always constructor with more parameters will be compared first , if not matched then it goes to
next level more parameters constructor (like 4 parameters, 3 parameters ..) until zero/default
constructor. Even If default constructor not found then spring container throws an exception
saying that no constructor found.
Java Code:
package com.app;
public Employee() {
super();
System.out.println("In default");
}
return addr;
}
@Override
public String toString() {
return "Employee [addr=" + addr + "]";
}
}
====
package com.app;
}
====
XML Code:
Spring Autowiring (XML and Annotations)
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
</bean>
</beans>
===========
package com.app;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
}
}
======
Spring Autowiring (XML and Annotations)
Note: 1) If more than one matchi9ng found while doing constructor , then it compares names to
inject the bean: for ex:
</bean>
</beans>
in the above code addr,addr2 are two objects then it compares the names to inject. So addr will
be select to inject. If no name matched then no object will be injected.
=========
autodetect:
Java code:
package com.app;
return addrId;
}
public void setAddrId(int addrId) {
this.addrId = addrId;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Address [addrId=" + addrId + ", loc=" + loc + "]";
}
}
============
package com.app;
public Employee() {
super();
System.out.println("In default");
}
@Override
public String toString() {
return "Employee [addr=" + addr + "]";
}
}
===
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
</bean>
</beans>
=====
package com.app;
import org.springframework.context.support.AbstractApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
Spring Autowiring (XML and Annotations)
}
}
Note: if you execute the above code it will check for default constructor and if found it
performs byType Autowiring. If you comment default constructor then it uses parameterized
constructor.
output:
In default
in setter
Employee [addr=Address [addrId=9, loc=HYD9]]
output:
in Param
Employee [addr=Address [addrId=9, loc=HYD9]]
public Address() {
super();
}
@Override
public String toString() {
return "Address [hno=" + hno + "]";
}
}
package com.app;
import org.springframework.beans.factory.annotation.Autowired;
@Autowired
private Address addr;
@Override
public String toString() {
return "Employee [addr=" + addr + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-
beans.xsd
Spring Autowiring (XML and Annotations)
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<bean class="com.app.Address" name="addrObj">
</bean>
</bean>
</beans>
package com.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context=new
ClassPathXmlApplicationContext("sample.xml");
Employee emp=context.getBean("empObj", Employee.class);
System.out.println(emp);
}
Program 2) If multiple beans configured then works by name:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-
beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd
Spring Autowiring (XML and Annotations)
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<bean class="com.app.Address" name="addrObj1">
<property name="hno" value="32"/>
</bean>
<bean class="com.app.Address" name="addr">
<property name="hno" value="32"/><!-- Name is matched -->
</bean>
</bean>
</beans>
import org.springframework.beans.factory.annotation.Autowired;
@Autowired(required=false)
private Address addr;
@Override
public String toString() {
return "Employee [addr=" + addr + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
Spring Autowiring (XML and Annotations)
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-
beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
</bean>
</beans>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@Autowired(required=false)
@Qualifier("addrObj2")
private Address addr;
@Override
public String toString() {
return "Employee [addr=" + addr + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
Spring Autowiring (XML and Annotations)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-
beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<bean class="com.app.Address" name="addrObj1">
<property name="hno" value="32"/>
</bean>
<bean class="com.app.Address" name="addrObj2">
<property name="hno" value="31"/>
</bean>
</bean>
</beans>