Spring Autowiring by Type Example

This mode allows a property to be autowired if exactly one bean of the property type exists in the container.In other words if a bean found with class as property type then that class object will be injected into that property by calling setter injection. If more than one exists a fatal exception is thrown, which indicates byType autowiring may not be used for that bean. If no class found then that property remains un-wired, but never throws any exception just like before.


The following example shows using byType autowiring mode.


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

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

Example On Spring Autowiring byType

Files Required.

  • Book.java
  • Author.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="byType">
		<property name="isbn" value="Effective Java" />
		<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=Effective Java, Author=Author [Name=Joshua Bloch, Address=USA, Year=2010

In the above example "Book" bean has a property with data type of "Author" class, Spring container will find the bean with same data type of class "Author" and wire it automatically via setter method. And if no matching found, just do nothing.



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