Get all updates of Java4s
Follow Us: Email Newsletter

Constructor Injection In Spring [ Full Concept ]

Spring » On Aug 15, 2011 { 20 Comments } By Sivateja

In this type of injection spring container uses constructor of the bean class for assigning the dependencies. In spring config xml, we need to inform to the spring IOC container about constructor injection by using <constructor -arg />

In spring bean class, if both constructor and setter injection applied for same property then constructor injection will be overridden by setter injection, because constructor injection will happen at the object creation time, and setter after objection right…, so setter will overrides

For Example:

In Config XML


In client application, when we call factory.getBean(“id1”), then internally spring framework executes following statements

finally value in message will be  Welcome to spring, not Welcome to java4s, as setter will over rides constructor, the reason being see i have taken primitive value [string] as dependency,  we have written injection for this property in constructor and setter method.  We all know constructor will be executed at object creation, so at the time of object creation only some value will be assigned into message property, then setter will be called so previous value will be overrides

———————————– # ———————————

In constructor injection,  if argument types  are different, then at the time of configuring of xml file we can use type attribute


In Config XML

According to above xml, DemoBean object will be created with 10 as id and 1000 as string [sname]

———————————– # ———————————

Let if we have 2 properties of same type, like user name and password


In our xml

Now bean object will be created with myuserName as username, and mypassword as password

———————————– # ———————————

Dependency in the form of object

Let us see how to work with dependency in the form of objects in this constructor injection…

In the xml

Note: Here see, directly i given ref as an attribute, so internally it meas ref-bean only not ref-local or ref-parent

———————————– # ———————————

If we have multiple constructors then..

XML file will be

In the above example, when we class factory.getBean(“id1“) from client application then spring framework creates an object of DemoBean by calling 1st constructure

If we call factory.getBean(“id2“) then spring framework crates the object of DemoBean by calling 2nd constructor automatically


This is total about this constructor injection in Spring, nothing more than that.

So mates, i don’t think you need any example program on this :-)

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 “Constructor Injection In Spring [ Full Concept ]”
  1. Manjesh says:

    it is very useful to me in many situtions.ThankQ

  2. praveen says:

    ohhh great example thank u very much

  3. The best Spring tutorial:) Thanks to team :)

  4. krishna says:

    An Excellent tutorial very nice

  5. Jegadeesan N says:


    Whoever u ppl r, now u r my lovable guruji.! :) I am addicted to ur teaching guruji.!

    I found a small mistake here.! in xml mapping file, while mentioning type of constructor argument, u spelled string as “s” in smaller case., it should be java.lang.String.. Its not a big mistake., but somebody will get trouble while attempting to copy paste code from here and try to run.!

    Keep going java4s.! Pleasure to learn here.!

  6. Amreen says:

    Thank you so much!! awesome tutorial indeed :)

  7. Ananth says:

    Nice Explanation :-) Thanks Java4s Team I Am Very Pleasure To Learn Here

  8. Nandu says:

    Its WOwwwww

  9. abhinav says:

    no doubt good tutorial… but horrible english… and lot of editing need to be done..

  10. Ranjana says:

    Very nice explaination.

  11. Harshit says:

    Thanks for the nice tutorial

    In config file, instead of java.lang.string, it should be java.lang.String

  12. sureshkumaran says:

    Be frank I am learning Spring in multiple sites but I feel you are best. Good job ! Please include some more concepts in Spring like AOP blah blah..

  13. Sai Kommu says:


    Nice explanation, can you give one example on Spring transaction Module with Simple examples.

    Thanks in Advance!.


  14. leelavathi says:


    this website was soo good… Thank u…
    can i ask u doudt??

    in constructor injection, u define xml file like

    for calling the Constructor

    public DemoBean (string uname, int id)
    { = id;
    This.uname = uname;


    how it will recognize myusername as String and 10 as int…

    i mean when we call the constructor it may call this also

    public DemoBean (int id, String uname)
    { = id;
    This.uname = uname;


    can anyone plz explain???

  15. Vinay Sharma says:

    Sir,Really Your all the Examples related to all the technology……is Excilent ,marvolous explanation …..u r great sir.

  16. praveena says:

    To be frank the way you explained the Constructor Injection is excellent

  17. charmila says:

    please explain the scenario with default constructor

  18. jaipal singh says:

    To be frank the way you explained the Constructor Injection is excellent

  19. Raj says:


    on the above example and the sample below the text “If we have multiple constructors then..”
    in the multiple constructor ther is no “ = id;” global variable found for id.

  20. Shruti says:


    I followed your tutorial it is really very good for beginners.
    Please explain thoroughly Bean Life Cycle by taking some easy examples & also explain use of Bean Post Processor.


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.