Spring Autowiring by Constructor Example
Autowiring by constructor is similar to byType,but in byType we used setter injection here we have to use constructor injection.The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.
The following example shows using constructor autowiring mode.
Autowiring by constructor is similar to byType,but in byType we used setter injection here we have to use constructor injection.The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.
The following example shows using constructor autowiring mode.
You can enable this feature via autowire="constructor" like below :
<bean id="employee" class="com.jwt.spring.Employee" autowire="constructor"/>
Example On Spring Autowiring by Constructor
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 Book(Author author) { this.author = author; } 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="constructor"> <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=1478589684, Author=Author [Name=Joshua Bloch, Address=USA, Year=2010
In the above example since the data type of author bean is same as the constructor argument data type in book bean's property(Author) object, Spring auto wires it via constructor public Book(Author author).
You can download the source code of the example by clicking on the Download link below.
Related Articles