Setter Injection With Objects, Spring Dependency In The Form Of Objects
|
Spring »
On Aug 6, 2011 | { 5 Comments }
|
Tweet
|
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.
|



why string is immutable?
@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?