Newsletter |
Example On Spring Autowiring byName
In this case, spring framework attempts 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, but never throws any exception.
Example On Autowiring byName
public class MyBean { private DemoBean db; public void setDb(DemoBean db) { this.db=db; } }
In the xml file
<beans> <bean id="id1" class="MyBean" autowire="byName" /> <bean id="db" class="DemoBean" /> </beans>
Explanation:
See line number 3 in MyBean, our class depends on DemoBean class object right, now see in the xml file line number 2 we have given autowire=”byName“, means when ever spring container notice autowire=”byName” then it will verifies whether the id in xml file is matching with the property name in the MyBean or not, if yes it will wired automatically else unwired
Am giving one figure to make you understand better 🙂
Complete Example
Files required…
- Book.java
- Categories.java
- ClientLogic.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 String getName() { return name; } public void setName(String name) { this.name = name; } public Book getBk() { return bk; } public void setBk(Book bk) { this.bk = bk; } 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="byName"> <property name="name" value="General Books" /> </bean> <bean id="bk" class="java4s.Book"> <property name="bookname" value="The Kids" /> <property name="bookprice" value="300" /> </bean> </beans>
Notes: We called id1 from ClientLogic.java [line number 15], and in spconfig.xml we have written autowire=byName, so first it will checks for the class with id name bk [as we have written private Book bk in Categories.java ] and inserts bk class [ jaa4s.Book ] properties into that object and then injects value “General Books” into name property of Categories class.
Finally in ClientLogic.java we used to type cast to get our output.
Output
You Might Also Like
::. About the Author .:: | ||
Hi,
Is spring web applications supports only tomcat server or all the java application deployment servers ??
thanks,
venkatesh
Thank you “Sivateja” for sharing your knowledge.
Hi Curser movement is excellent go on……………..
Excellent shiva!!!. I was very much confused with autowiring concept.
After reading your example i got clear idea on Autowiring.
Thanks a lot for sharing knowledge.
At cursor movement, Bean in the line 2 is also closed and line 4 is also closed.
Is the id1 is composite of db?
You are an awesome teacher…. Thanks so much for all this in-depth explanation !
great job sir
Hi Siva,
It throws java.lang.NullPointerException when no id is found.
Hi Sivateja thanks for your efforts , your articals are very easy to understand,.thanks lot again.
love u Sir.. thnks a lot..
Thanku,
You are explaining very neat & clean with taking some beautiful example,
I am going to fan of your web site.
Awesome..!Sweet and simple..Thanks to Sivateja kandula..
thanks sir for providing this tutorial in a simple way that can be easily understood by fresher also
Hi,
Sivateja the above example explanation was awesome.
Thanks..
How auto wire byName works when Interface is used .
Easy to understand with cursor moment ,Keep it up
Thanks for sweet and simple way of explaining.!!!!
Hi shiva, I will be very thankful if you add some examples of autowiring using annotations because in real projects we mainly use annotations and i am not clear about the autowiring with annotations in java bean class.
If bean property name and bean id name didn’t match, it will throw
java.lang.NullPointerException
What if I need to define one more book information which if of different category. I can’t create one more bean with same id as ‘bk’. How to resolve that?
in the above example we are defining
autowire="byName" for been id="id1" class="java4s.Categories". Then how it will search "byName" for
bead id = "bk"? will you please explain ???
Hi,
If we have multiple spring configuration xml files in our application then autowire will work like as per above example?
Hi Narasim,
Yes it should work. And additionally we should add ref attribute with value as "parent". More than more XML files are for out readability and comfortable, Not more than tnat.
is autowiring byname & bytype is only for setter injection..
<bean id="Categories" class="com.spring.Categories" autowire="byName">
<property name="name" value="General Books"/>
======> <property name="book" ref="Book"></property>
</bean>
<bean id="Book" class="com.spring.Book">
<property name="bname" value="The Kids"/>
<property name="price" value="300"/>
i have doubt. y u not mention ref tag in "id1". isn't it required or not. please give me solution to my doubt
Wowwwww Awesome explanation out of the box!!!
Thank you very good explanation…Once again Thank you…