Newsletter

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

Spring » on Aug 6, 2011 { 25 Comments } 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:

public DemoBean
{
   public SampleBean sb;
   public void setSb(SampleBean sb)
   {
      this.sb = sb;
   }

   public void m1()
   {
      sb.m2();
   }
}

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

<beans>

  <bean id="id1">
    <property name="sb" class="DemoBean">
      <ref local="id2" />
    </property>
  </bean>

  <bean id="id2" class="SampleBean">

</beans>

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

<beans>

  <bean id="id1">
    <property name="sb" class="DemoBean">
      <ref parent="id2" />
    </property>
  </bean>

</beans>

spconfig2.xml

<beans>

  <bean id="id2" class="SampleBean">

</beans>

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.

​​

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

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

    why string is immutable?

    • kasi says:

      String is a sequence of characters. But in java, string is an object that represents a sequence of characters. The java.lang.String class is used to create string object.

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

    • ankur says:

      our DemoBean class depends on other class object SampleBean and DemoBean has already been specified in the first xml file where reference of id also given and as parent is used it makes clear that there might be existing some other file thats why 2nd xml file is the parent. Correct me if I am wrong.

  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.

  11. Rohan sutar says:

    awsom tutorial

  12. Gabriel says:

    Hi java4S team,
    You ppl are awesomme I am admiring the tutorials of you’ll and the passion you show in developing these tutorials is very visible. I am prefering and recommending your tutorials to all who wish to learn.

  13. jagadeesh says:

    Hi pls correct the xml file 4 abv example:

  14. Ranjith says:

    Hello Java4s,

    Can you please provide JSP and EXT JS as well?

  15. pep says:

    You’re a Godsent for poor students all over the world who can’t affort to buy books, simply because they don’t have money. You’re blessed for doing all of these. Thank you very much Sivateja Kandula

  16. pooja says:

    nice explanation of spring

  17. Girija says:

    hi, ur tutorial is awesome for freshers. Am a fresher first i need to follow spring examples instead of going to struts and hibernates.

  18. Mohsin says:

    Dude,how will the control go to the parent .xml . We did not specify the parent .xml anywhere .If there are more than 2 .xml files then how will it search ??

  19. Anil says:

    Attribute 'class' is not allowed to appear in element 'property'
    i am getting that error.

  20. nitin says:

    this tutorial is seriously good bro.was struggling to understand the concepts from long time of spring.could
    understand it here very well.

  21. uma reddy says:

    Really superb blog…………

  22. Sridhar says:

    If you have multiple configuration xml files, in which file it will search? Will it search in all config files till it finds id2?

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.