title: What is the use of ref in constructor-arg in java spring beans?
body: <p>I'm new to spring beans, so I don't get the use of ref in constructor-arg. Why not use value again like in this example here, </p>
  
  <p>Here is the content of TextEditor.java file:</p>
  
  <pre><code>package com.tutorialspoint;
  
  public class TextEditor {
     private SpellChecker spellChecker;
  
     public TextEditor(SpellChecker spellChecker) {
        System.out.println("Inside TextEditor constructor." );
        this.spellChecker = spellChecker;
     }
     public void spellCheck() {
        spellChecker.checkSpelling();
     }
  }
  </code></pre>
  
  <p>Following is the content of another dependent class file SpellChecker.java:</p>
  
  <pre><code>package com.tutorialspoint;
  
  public class SpellChecker {
     public SpellChecker(){
        System.out.println("Inside SpellChecker constructor." );
     }
  
     public void checkSpelling() {
        System.out.println("Inside checkSpelling." );
     }
  
  }
  </code></pre>
  
  <p>Following is the content of the MainApp.java file:</p>
  
  <pre><code>package com.tutorialspoint;
  
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class MainApp {
     public static void main(String[] args) {
        ApplicationContext context = 
               new ClassPathXmlApplicationContext("Beans.xml");
  
        TextEditor te = (TextEditor) context.getBean("textEditor");
  
        te.spellCheck();
     }
  }
  </code></pre>
  
  <p>Following is the configuration file Beans.xml which has configuration for the constructor-based injection:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;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-3.0.xsd"&gt;
  
     &lt;!-- Definition for textEditor bean --&gt;
     &lt;bean id="textEditor" class="com.tutorialspoint.TextEditor"&gt;
        &lt;constructor-arg ref="spellChecker"/&gt;
     &lt;/bean&gt;
  
     &lt;!-- Definition for spellChecker bean --&gt;
     &lt;bean id="spellChecker" class="com.tutorialspoint.SpellChecker"&gt;
     &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here, why not do it this way where, instead of creating a bean textEditor and referencing another object spellChecker, you can directly use the spellChecker bean?</p>
  
  <p>In MainApp.java</p>
  
  <pre><code>TextEditor te = (TextEditor) context.getBean("spellChecker");
  </code></pre>
  
  <p>If we use it because both the objects are in different classes, then can we do away with ref if both are in same class?</p>
  
  <p>Also if multiple objects refer to same object, is it necessary to create a bean for each of those classes and use ref to refer to this object or use same bean but with scope=prototype?</p>