Share on Google+Share on Google+

Spring Autowiring, Spring Autowire Tutorial

In this example you will learn about autowiring in spring framework.

Spring Autowiring, Autowiring Collaborators

Spring have many collaborating bean, the autowire help in making the relationship between them. Autowiring reduces the effort of writing properties or constructor arguments. The autowiring in specified at the autowire attribute inside <bean/> element. The functionality of autowiring seen in five mode.

 Mode Explanation
no It is default which define no autowiring.
byName Autowiring is done by property name.
byType Autowiring is done by matching data type of property name.
constructor Autowiring is done by matching data type of property name with the property constructor argument.
autodetect When default constructor with no argument, it auto-wire by data type or it auto-wire by constructor.

You will see example step by step on all different mode defined above.

Lets see an example of no autowiring mode which is the default mode. Bean references must be defined via a ref element at the property.

package spring.noautowiring.mode;

import org.springframework.beans.factory.annotation.Autowired;

public class Student {
  private String age;
  private String name;
  private String address;

  public String getAge() {
    return age;

  public void setAge(String age) {
    this.age = age;

  public String getName() {
    return name;

  public void setName(String name) { = name;

  public String getAddress() {
    return address;

  public void setAddress(String address) {
    this.address = address;
  public String toString() {
    return "Student [name=" + name + ", age=" + age + 
                          ",address=" + address + "]";


package spring.noautowiring.mode;

import org.springframework.beans.factory.annotation.Autowired;

public class College {
  private Student student;
  private String registration;
  private String year;

  public void setStudent(Student student) {
    this.student = student;

  public String getRegistration() {
    return registration;

  public void setRegistration(String registration) {
    this.registration = registration;

  public String getYear() {
    return year;

  public void setYear(String year) {
    this.year = year;
  public String toString() {
    return "College [registration=" + registration + ", Student=" + student + 
                          ",year=" + year + "]";


package spring.noautowiring.mode;

import org.springframework.beans.factory.BeanFactory;

public class AutowireMain {
  public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext(
    College coll = (Collegebeanfactory.getBean("college");


<beans xmlns=""
 <!-- no auto-Wiring mode -->
  <bean id="college" 
="spring.noautowiring.mode.College" autowire="no">
    <property name="student" ref="student" />
    <property name="registration" value="BL001" />
    <property name="year" value="2001" />

  <bean id="student" class="spring.noautowiring.mode.Student">
    <property name="address" value="Delhi" />
    <property name="age" value="22" />
    <property name="name" value="Raj" />
<!-- End -->


When you run this application it will display output as shown below:

College [registration=BL001, Student=Student [name=Raj, age=22,address=Delhi],year=2001]

Download this example code



Posted on: August 25, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null