Share on Google+Share on Google+

The idref in Spring, The idref element in spring framework

In this tutorial you will learn about the idref element in spring framework.

The idref element in spring framework

The idref element allow you to pass the bean id (which is the string value not the reference) of another bean in the container to a <property/> or <constructor-arg/>. In the given example it is clearly shown how pass the bean id to another bean and also display the bean id.

package idref.example;

public class FirstBean {

  private String message = null;

  public String getMessage() {
    return message;

  public void setMessage(String message) {
    this.message = message;


package idref.example;

public class AnotherBean {
  private String amessage = null;

  public String getAmessage() {
    return amessage;

  public void setAmessage(String amessage) {
    this.amessage = amessage;

  public void display() {

package idref.example;

import org.springframework.beans.factory.BeanFactory;

public class IDREFMain {
  public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext(
    AnotherBean bean = (AnotherBeanbeanfactory.getBean("another");


<bean id="first" class="idref.example.FirstBean">
    <property name="message" value="Spring is simple."/>

<bean id="another" class="idref.example.AnotherBean">
    <property name="amessage" >
      <idref bean="first"/>

When you run this application it will display output as shown below:


Download this example code


Posted on: August 12, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null