Get all updates of Java4s
Follow Us: Email Newsletter

Example On Spring Autowiring by Constructor

Spring » On Feb 8, 2012 { 10 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

Categories.java

ClientLogic.java

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.

Output

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
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.

Comments

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

    where is constructor injection happened here???

  2. Java4s says:

    @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.

  3. Sasmita Das says:

    still not clear on autowiring “byType”

  4. sundar says:

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

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

  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:

    Thanks

  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

Name*
Mail*
Website



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
Upcoming
  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.