Newsletter |
Example On Spring Autowiring by Constructor
Spring » on Feb 8, 2012 { 14 Comments } By Sivateja
Actually Spring Autowiring by constructor is similar to spring autowiring byType [ internally it will considers as byType only ] but with little difference, in byType we used setter injection here we have to use constructor injection 🙂 nothing more than that.
Friends am using same application to show the difference between byName, byType, constructor, autodetect to avoid confusions [ Out put is same for all these 4 applications 😉 ]
Example On Spring Autowiring Constructor
Files required…
- Book.java
- ClientLogic.java
- Categories.java
- spconfig.xml
Book.java
package java4s; public class Book { private String bookname; private int bookprice; public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public int getBookprice() { return bookprice; } public void setBookprice(int bookprice) { this.bookprice = bookprice; } }
Categories.java
package java4s; public class Categories { private String name; private Book bk; public Categories(Book bk) { this.bk=bk; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void show() { System.out.println("Categories name :"+name); System.out.println("Book name :"+bk.getBookname()+" and Book Price :"+bk.getBookprice()); } }
ClientLogic.java
package java4s; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String[] args) { Resource res = new ClassPathResource("spconfig.xml"); BeanFactory factory = new XmlBeanFactory(res); Object o = factory.getBean("id1"); Categories wb = (Categories)o; wb.show(); } }
spconfig.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="id1" class="java4s.Categories" autowire="constructor"> <property name="name" value="General Books" /> </bean> <bean id="SomeThing" class="java4s.Book"> <property name="bookname" value="The Kids" /> <property name="bookprice" value="300" /> </bean> </beans>
Notes: Friends am not explaining this program, as i told you earlier Spring Autowiring constructor is similar to Spring Autowiring byType but here we use constructor injection, please refer byType if you still have any doubts.
Output
You Might Also Like
::. About the Author .:: | ||
Comments
14 Responses to “Example On Spring Autowiring by Constructor”
where is constructor injection happened here???
@Rajesh
Steps:
– In ClientLogic.java, line number 15 we have called id1
– So controller comes to spconfig.xml line number 5, there it will inserts ‘general books‘ into ‘name‘ normally
Now your doubt__ ?
– In spconfig.xml line number 5, we have given autowire=”constructor”, hence check Categories.java(line number 7), we have property bk of type Book, so in spconfig.xml controller verifies class attribute contains ‘Book’ which is at line number 8, and bla bla..
Hope you are clear 🙂 Please refer previous concepts.
still not clear on autowiring “byType”
I think constructor not use byType it will use byName.
Please confirm i tested.
its very late response but you are right , If constructor argument is matching with bean id then it goes for byName. But if id is not matching then it will check for byType.
Here in spconfig.xml line no 9 & 10 you are using property injection and in your above explanation you have mentioned that autowire by constructor using injection type is constructor not property.so don’t you think in line 9 & 10 it should constructor-arg instead of property?
Please correct me if i am wrong.
Surendra Singh
Hi all,
From my understanding I try to clear it.
In the xml file controllers goes to the next bean(id=SomeThing) and check its type -(Line no -8)
The type is of Book type.
Now It will go to the Categories class since for this class U have set the auto wire =constructor and will try to find if this class has any constructor which must have Book type parameter.
It find the Book type parameter constructor and hence now the properties of the id=SomeThing would be automatically wired with the first bean(bean id=”id1″).
its pretty clear friend .. and ur rocking !!!
Thanks
thanks now i understood @Rishab
I didnt get exact difference between by Type and by Constructor. Can you describe step wise process for by Constructor example
Hi sivateja
Acutally I didnt get byType autowire please explain clrearly.
Hi
Why second bean id="Something" not Book and <constuctor-arg ref="Book"> is not used as first bean property.
Please clarify.
Thanks
Thanks