Example On Spring Autowiring byName

Spring » on Sep 3, 2011 { 24 Comments } By Sivateja

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

In the xml file


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…

  • spconfig.xml


Notes: We called id1 from [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 ] and inserts bk class [ jaa4s.Book ] properties into that object and then injects value “General Books” into name property of Categories class.

Finally in we used to type cast to get our output.


You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


24 Responses to “Example On Spring Autowiring byName”
  1. venkatesh says:


    Is spring web applications supports only tomcat server or all the java application deployment servers ??


  2. Thank you “Sivateja” for sharing your knowledge.

  3. sudha says:

    Hi Curser movement is excellent go on……………..

  4. Raghu says:

    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.

  5. At cursor movement, Bean in the line 2 is also closed and line 4 is also closed.
    Is the id1 is composite of db?

  6. Akila says:

    You are an awesome teacher…. Thanks so much for all this in-depth explanation !

  7. selvakumar.g says:

    Hi Siva,

    It throws java.lang.NullPointerException when no id is found.

  8. Vaibhav says:

    Hi Sivateja thanks for your efforts , your articals are very easy to understand,.thanks lot again.

  9. Ashu Ranjan says:

    love u Sir.. thnks a lot..

  10. Pramod Kumar says:

    You are explaining very neat & clean with taking some beautiful example,
    I am going to fan of your web site.

  11. Arunkumar Papena says:

    Awesome..!Sweet and simple..Thanks to Sivateja kandula..

  12. raj says:

    thanks sir for providing this tutorial in a simple way that can be easily understood by fresher also

  13. gowtham says:

    Sivateja the above example explanation was awesome.

  14. pavan kumar says:

    How auto wire byName works when Interface is used .

  15. praveen says:

    Easy to understand with cursor moment ,Keep it up

  16. Sandeep Verma says:

    Thanks for sweet and simple way of explaining.!!!!

  17. Hitesh Upreti says:

    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.

  18. Avi Iname says:

    If bean property name and bean id name didn’t match, it will throw


  19. Onkar Ketkar says:

    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?

  20. Narendra says:

    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 ???

  21. Narasimman P says:


    If we have multiple spring configuration xml files in our application then autowire will work like as per above example?

  22. prashanth says:

    is autowiring byname & bytype is only for setter injection..

  23. siva says:

    <bean id="Categories" class="com.spring.Categories" autowire="byName">
    <property name="name" value="General Books"/>
    ======> <property name="book" ref="Book"></property>
    <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


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Hibernate Spring Struts
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.