Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Setter Injection With Objects, Spring Dependency In The Form Of Objects

Spring » On Aug 6, 2011 By Sivateja

In previous example, we have seen that our spring bean class object depends on the string primitive, and now will see what if our class is depends on other class object, i mean dependency in the form of object.

  • While constructing spring beans, if one spring bean is depends on another spring bean class for performing some logic, this process of dependency is called object dependency
  • If object dependency is there then in spring framework, the spring IOC container is responsible for creating that required object and injecting into the dependent class

For spring configuration xml, we have 2 ways to inform to the spring container about this object dependency

  • By using inner beans
  • Using <ref /> element

Actually with inner beans we have some disadvantages and its not the way to use in the real time projects so am not going to explain about it.

So we will see about <ref  /> element, and i forgot to tell you actually in previous example we have one spring configuration file right [ spconfig.xml ], in spring we can write multiple configuration xmls, i will tell you how its going to work in the following example

 

Using <ref /> element

Syntax will be

<ref local/parent/bean=”id of collaborator bean”>

Actually we used to write either local or parent or bean, means as i told you earlier, we can write any number of spring configuration xmls for the spring application.  Our collaborator bean may be in same xml or other xml so spring has given these 3 options, we will see one by one.

<ref local=”id value” />

If we use the local attribute in the <ref /> element then the spring IOC container will verify for the collaborator bean with in same container [ i mean in same xml ]

In general we try to configure all spring beans into a single spring configuration xml only.  But its not mandatory we can create multiple configure files too right.

Exmple:

Note: See am calling m2() method in SamepleBean so now let us see how the xml file will be

So our DemoBean class depends on other class object SampleBean,  see in the xml line number 5 i have specified ref tag with local attribute and given that required class id, why local…? because that required (collaborator) class also i have configured in the same xml file  [ line number 9 ], so spring container will check only in this xml only

<ref parent=”id value” />

But we can also configure the collaborator class in other xml like…

spconfig1.xml

spconfig2.xml

In this case, we have to write parent as attribute, see spconfig1.xml  line number 5.  As we specified parent, spring container will only checks in the parent i mean spconfig2.xml only

If we give attribute as bean, then first it will checks at local xml file, then parent if its not available at local.  Hey you will be able to understand once you got through the example…, let us see the program on this in the next session.






By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja@java4s.com


Comments

11 Responses to “Setter Injection With Objects, Spring Dependency In The Form Of Objects”
  1. bibhu says:

    why string is immutable?

  2. Hi,

    What I can say is 1,00,000 times thank you for providing such a nice and step by step tutorial on spring. Your chapters are framed in such a good manner that after reading these it seems that any one can can learn online very easily. Really no need of regular classes or to pay high amount of money. It was very tough to me to suppose to learn spring before coming to this site.

    Thank you very much.
    My best Regards for you.

  3. Kundan Kumar says:

    Hi,

    Thanks for providing such kind of tutorial.
    I want to know how to integrate spring with hibernate, so
    pls provide it ASAP.

  4. srinu says:

    can you please explain me how spconfig1.xml can understood it’s parent is spconfig2.xml?

  5. Srikanth S says:

    Hi Java4s team,
    Can you please explain how the spconfig1.xml can understood it’s parent is spconfig2.xml?

  6. Sonal says:

    Thanks for this tutorial..its really very helpful to me :)

  7. Madhuri says:

    Awesome tutorial very easy to understand for beginners thank you it helped me a lot :)

  8. Palanikumar says:

    Dear Friend,

    can you please explain all the concept in real time. see for example you explain any one concept before explain what is the problem? why we use this concept? where we are going to apply and implement in our project? when we include this concept. what kind of situation this concept need and so on… These things am expecting. but everybody saying simple definition , differences and program this is not use for beginning learner and me also.Don’t mistake me these things i am expecting from you. and also please post output also

  9. Guruputra K M says:

    First of all, thanks for the Java4s team.

    i have tried this springs example on Springs 3.1, but in the Xml file ,there is no element like could please tell me how to achive this element in springs 3.1

    Advance in thanks
    Regards
    Guruputra K m

  10. Santhosh says:

    The class attribute is only applicable for , if we have it in tag, it is not able to locate the bean class and throws “Attribute “class” must be declared for element type “property”.” exception.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
JSP, Web Services [ In Progress ]
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.