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…

  • spconfig.xml


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.


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.


14 Responses to “Example On Spring Autowiring by Constructor”
  1. Rajesh says:

    where is constructor injection happened here???

  2. Java4s says:



    – In, 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 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.

  3. still not clear on autowiring “byType”

  4. sundar says:

    I think constructor not use byType it will use byName.
    Please confirm i tested.

    • Khushwant says:

      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.

  5. 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 don’t you think in line 9 & 10 it should constructor-arg instead of property?

    Please correct me if i am wrong.

    Surendra Singh

  6. Rishabh says:

    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″).

  7. mohan says:

    its pretty clear friend .. and ur rocking !!!

  8. Azhagumuthu G says:


  9. rao says:

    thanks now i understood @Rishab

  10. Yogesh Joshi says:

    I didnt get exact difference between by Type and by Constructor. Can you describe step wise process for by Constructor example

  11. somireddi ganesh says:

    Hi sivateja

    Acutally I didnt get byType autowire please explain clrearly.

  12. Nandhini says:

    Why second bean id="Something" not Book and <constuctor-arg ref="Book"> is not used as first bean property.
    Please clarify.


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.