0% found this document useful (0 votes)
127 views

Using IDRef Spring

This document discusses using IDRef in Spring to inject the ID of one bean into another bean. It explains that using a simple value for the ID would cause issues if the ID is changed. Instead, it recommends using the <idref> tag to reference the ID of another bean. This avoids problems and ensures consistency if bean IDs are modified. The example shows configuring the engine ID of a Car bean using <idref> to safely reference the ID of the HondaEngine bean.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Using IDRef Spring

This document discusses using IDRef in Spring to inject the ID of one bean into another bean. It explains that using a simple value for the ID would cause issues if the ID is changed. Instead, it recommends using the <idref> tag to reference the ID of another bean. This avoids problems and ensures consistency if bean IDs are modified. The example shows configuring the engine ID of a Car bean using <idref> to safely reference the ID of the HondaEngine bean.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Using IDRef

SpringTutors January 10, 2016 Features Leave a comment 76 Views

Related Articles
Spring MVC Flow
February 22, 2016

Servlet And Struts Project Architecture


February 17, 2016

AOP
January 29, 2016

Annotation Vs Xml
January 11, 2016

Introduction To Spring Framework


January 10, 2016

Using IDRef
In some cases a bean wants to use the id of another bean, so how to inject id of a bean into another
bean, here we need to use idref.
In spring every time dependency injection will not give us required results, because injection of bean or
value to another bean will give us only static value. It means when we want any value at run time
(dynamically) it will not possible through dependency injection. To get dynamic value we have to pull the
value.
For example Car needs an Engine to run, to use Engine inside Car we can inject Engine ito the Car, but
we dont want to inject rather we want to pull, to pull we need the id of the Engine inside the car. so we
are injecting the id of the Engine into the Car as shown belowpackage com.idref.bean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Car {
private IEngine engine;
private String engineId;
public void setEngineId(String engineId) {
this.engineId = engineId;

}
public void drive()
{
BeanFactory factory=new XmlBeanFactory(new ClassPathResource("com/idr/common/applicationcontext.xml"));
engine=(IEngine) factory.getBean(engineId);
engine.start();
}
}
package com.idref.bean;
public interface IEngine {
void start();
}
package com.idref.bean;
public class HondaEngine implements IEngine {
public void start() {
System.out.println("Honda Engine is Started");
}
}
package com.idref.bean;
public class FiatEngine implements IEngine {
public void start() {
System.out.println("FiatEngine Is Started");
}
}

Spring Bean Configuration file:application-context.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.idref.bean.Car">
<property name="engineId" value="hondaEngine"/>
</bean>
<bean id="hondaEngine" class="com.idref.bean.HondaEngine"/>
<bean id="fiatEngine" class="com.idref.bean.FiatEngine"/>
</beans>
package com.idref.bean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.idr.bean.Car;
public class IDRTest

{
public static void main(String[] args) {
BeanFactory factory=new XmlBeanFactory(new ClassPathResource("com/idref/common/applicationcontext.xml"));
Car c=(Car)factory.getBean("car");
c.drive();
}
}

In the above Spring bean configuration file for property engineId we have passed the value as
hondaEngine which will work by injecting the HondaEngine bean into Car. But, there is a problem with
the above piece of code. The problem here is we are passing the engineId hondaEngine as a value into
the car, if the bean id has been changed lets say hondaEngine to hondaEngine1 then it would be
difficult to track and change the value as the property is configured as value.
In real time application developer will not be able to understand upon looking into configuration whether to
modify or not as it looks like a simple value. By mistake if developer change the property value which
looks like as general value without considering the bean Id of other beans then IOC container will give run
time exception.
In this case Spring will try to create the IOC container even the configuration is inconsistent, which will
results in an runtime exception while getting the Engine using factory.getBean(engineId).
To avoid the above problems it is recommended to pass the id of another bean as value using <idref> tag.
ref means reference to an id of another bean. Below configuration shows the sameapplication-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.idr.bean.Car">
<property name="engineId" ><idref bean="hondaEngine"/></property>
</bean>
<bean id="hondaEngine" class="com.idref.bean.HondaEngine"/>
<bean id="fiatEngine" class="com.idref.bean.FiatEngine"/>
</beans>

With the above Spring bean configuration file while creating the Car bean in the IOC container, it checks if
any bean with id as hondaEngine exists if found then only creates the Car object and inject the value
HondaEngine into the car. Otherwise will stop creating the object and throws an exception

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy