Spring Autowiring by Name Example

In Autowiring by Name mode, spring framework will try to find out a bean in the configuration file, whose id is matching with the property name to be wired. If a bean found with id as property name then that class object will be injected into that property by calling setter injection. If no id is found then that property remains un-wired without throwing any exception.


The following example shows using byName autowiring mode.


You can enable this feature via autowire="byName" like below :

<bean id="employee" class="com.jwt.spring.Employee" autowire="byName"/>

Example On Spring Autowiring byName

Files Required.

  • Employee.java
  • Address.java
  • spring-config.xml
  • Test.java

Book.java

package com.jwt.spring;

public class Book {
	private Author author;
	private String isbn;
	private String year;

	public Author getAuthor() {
		return author;
	}

	public void setAuthor(Author author) {
		this.author = author;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String toString() {
		return "Book [ISBN=" + isbn + ", Author=" + author + ", Year=" + year;
	}

}

Author.java

package com.jwt.spring;

public class Author {
	private String name;
	private String address;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String toString() {
		return "Author [Name=" + name + ", Address=" + address;
	}

}



spring-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<bean id="book" class="com.jwt.spring.Book" autowire="byName">
		<property name="isbn" value="1478589684" />
		<property name="year" value="2010" />
	</bean>
	<bean id="author" class="com.jwt.spring.Author">
		<property name="name" value="Joshua Bloch" />
		<property name="Address" value="USA" />
	</bean>
</beans>

Test.java

package com.jwt.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String as[]) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
		Book book = (Book) context.getBean("book");
		System.out.println(book);
		
	}

}


Output:

Book [ISBN=1478589684, Author=Author [Name=Joshua Bloch, Address=USA, Year=2010

In this case, since the name of author bean is same with the name of the book bean's property [author], so Spring auto wires it via the setter method i,e, setAuthor(Author author).



You can download the source code of the example by clicking on the Download link below.
Download this example(src+lib) developed in eclipse





comments powered by Disqus