Newsletter

Dependency Injection In Spring Framework

Spring » on Aug 4, 2011 { 34 Comments } By Sivateja

Guys, its very important concept in spring read it carefully

Dependency injection:

In this IOC, consider when our class object need to get any primitive values or it need  to access any other class objects or it may need  any collection type to access then, some external person will provide the required things (primitive values or objects or collections) to our class object.

Actually In IOC, our class is not responsible to get all these collaborating (required things like primitive values, collections bla bla) values explicitly, I mean it doesn’t perform any lookup operations for the things to access.

Come to Spring framework, here one external person who will provide [Injects] all the required things to our spring bean class is the spring ioc container

Types of dependency injections

For injecting the required things to the current spring class (spring bean) , spring ioc container will do this in different ways…

  • Setter injection
  • Construction injection
  • Interface injection

But in spring we have only setter, constructor injections but not interface injection,  In spring 2.x we have interface injection

Setter Injection

In this type of injection, the spring container uses setter method in the dependent (our) class for injecting its dependencies (primitive values..Or any).

Spring container knows whether to perform setter or constructor injection by reading the information from an external file called spring configuration file

In case of setter injection, the class must contain a setter method to get dependencies other wise spring container doesn’t inject the dependencies to dependent object

In spring we call each class as spring bean, this bean is no way related with the java bean

Spring bean and java bean both are not same because a java bean needs definitely a public default constructor but in spring bean, some time we include default constructor or sometimes we do not.

First Consider Our spring bean having the dependency in the form of primitive values, I mean our class need some primitive values…

And let us see an example on setter injection with primitive values in the next session[ before that check how to install spring framework ]

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack 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.

Comments

34 Responses to “Dependency Injection In Spring Framework”
  1. kumar says:

    Hi,
    I think this tutorial regarding about Spring 2.X.
    Before we learn spring 3,should we learn spring 2.X?
    Can we learn Spring 3 without referring Spring 2.X?

    What is better way to learn Spring ?
    Please Advice.Thank You

  2. Java4s says:

    @kumar

    You can learn spring 3.x directly with out knowing the knowledge of 2.x. there is no much difference.

    In 3.x you can learn little more 🙂 but all concepts are same, refer this blog while you are in the course.

    Enjoy spring, cheers 😉

  3. shyam says:

    present content of spring is just basics….it’s very good for freshers actually we need real time concepts like SPRING MVC 3.0 , Spring with hibernate,spring with ejb 3.0 and transaction management of spring , and mainly mixing of all modules of spring one project explanation….we hope you provide this content ASAP

  4. Java4s says:

    @Shyam

    I agree 🙂 we will provide these real time application ASAP, thank you.

  5. Vishal says:

    Hi Java4s,
    I am the new in spring.can you please tell me the exact role of the AOP in spring.

    Vishal Singh

  6. surendra says:

    one of best Web site that I haven’t seen till now with clear examples and explanation. Keep it up guys

  7. Nagendra says:

    What a site this is,even the person who doesn’t know anything about Sping,he also can learn easily by visiting this site..FLAWLESS,AWESOME..KEEP IT UP

  8. Muthu says:

    Thanks for this tutorial. Could you explain with some sample code. Then really it will helps me.

  9. Sri says:

    Hi Sivateja,
    Good to see your tutorials.
    it is very lucid.
    can you please come up with any one of the real time project explanation with any combination.
    Hope you understood the requirement.
    Thanks in advance

  10. It is very useful for basic spring learners.
    Thankful to providing …………….

  11. Azhark says:

    HI,

    i am following your tutorials since i am in to JAVA , really good Explanation , Thanks for sharing your knowledge .

  12. surya says:

    All things described here are to the point… Keep going !!!

  13. what is difference between Dependency Injection(DI) and Inversion Of Control(IoC)?

  14. manju says:

    Hello sir, ur website awesome for learning but please mention page number or give next button option in every page so that user can learn topic wise..

  15. Sudhakar says:

    Its nice site for Java Freshers & Experienced.
    But please keep the navigation buttons [previous] and [next] on the top also.

  16. raja says:

    please explain comparator and comparable in collections

  17. avinash singh says:

    Sir please u can tell me how can i chose which is best injection in spring and which sinerio we use setter and constructor injection

    thanks

  18. Nitin says:

    Your tutorials are very helpful for the beginners..One of the best website with examples and clear explanation..Thank You.!!!

  19. vinod says:

    Your tutorial is very fine for freshers .I think some more explanation.

  20. vindhyachal choubey says:

    your tutorial is very helpful for me…………..
    this is just like my the best friend………….
    all example and their explanation is very clear
    thank u………….

  21. Swapna says:

    really very helpfull beginers
    thanks java 4s
    learned lot of things from u

  22. hi saiteja ..its good for us now icleared only one point in diff between javabean and spring provide more information about this topics\

  23. Chandra Prakash says:

    Hi Sir,

    I didn’t get u. How spring been is different from java been??

    I mean even if we do not create default constructor in spring been too it will have a default constructor in it. Its a java basic feature.

    Kindly clear my this doubt.

    Thanx in advance. 🙂

  24. suhas h says:

    Hi Sivateja,
    Thanks a lot for this tutorial……its damn easy to understand

  25. Shreeketh says:

    mukleri edhe maga superrr

  26. Bandu says:

    please tell me ….
    i learn core java i am able to learn Spring

  27. Sebin Joseph says:

    Can you explain interface injection too?

  28. Harish says:

    Can you provide pdf regarding spring , spring mvc tutorial

  29. Sasmita says:

    Really, this website is awesome, i used to read all concepts from this site at least once before facing interview.

  30. vigneswari says:

    hi

    I have read the spring core module of inversion of control that example was super and I understood clearly
    I have confused about spring container and web container
    Ex:If I run my spring web application in my tomcat web server,here web container its the one of the component of web server.
    could you please give me a simple explanation for the differences

  31. AMOL G BORULE says:

    It's very Excellent Blogs
    Tqsm for providing information in very simple way

Name*
Mail*
Website



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

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