Newsletter |
Setter Injection With Objects, Spring Dependency In The Form Of Objects
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 .:: | ||
why string is immutable?
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.
@bibhu
Check this link, to understand about string immutable.
http://answers.java4s.com/182/difference-between-immutable-and-final-in-java
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.
Hi,
Thanks for providing such kind of tutorial.
I want to know how to integrate spring with hibernate, so
pls provide it ASAP.
can you please explain me how spconfig1.xml can understood it’s parent is spconfig2.xml?
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.
Hi Java4s team,
Can you please explain how the spconfig1.xml can understood it’s parent is spconfig2.xml?
Thanks for this tutorial..its really very helpful to me 🙂
Awesome tutorial very easy to understand for beginners thank you it helped me a lot 🙂
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
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
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.
awsom tutorial
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.
Hi pls correct the xml file 4 abv example:
Hello Java4s,
Can you please provide JSP and EXT JS as well?
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
nice explanation of spring
hi, ur tutorial is awesome for freshers. Am a fresher first i need to follow spring examples instead of going to struts and hibernates.
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 ??
Attribute 'class' is not allowed to appear in element 'property'
i am getting that error.
this tutorial is seriously good bro.was struggling to understand the concepts from long time of spring.could
understand it here very well.
Really superb blog…………
If you have multiple configuration xml files, in which file it will search? Will it search in all config files till it finds id2?