Get all updates of Java4s
Follow Us: Email Newsletter

Example On Spring Autowiring byName

Spring » On Sep 3, 2011 { 20 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 Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


20 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. JavaPassion says:

    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?


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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